<x-app-layout title="Edit practitioner">
    <div class="mx-auto max-w-lg">
        <h1 class="text-xl font-semibold text-slate-900">Edit practitioner</h1>

        <form method="POST" action="{{ route('care.practitioners.update', $practitioner) }}" class="mt-6 space-y-4 rounded-2xl border border-slate-200 bg-white p-6">
            @csrf
            @method('PUT')
            <div>
                <label class="block text-sm font-medium">Name</label>
                <input type="text" name="name" value="{{ old('name', $practitioner->name) }}" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
            </div>
            <div>
                <label class="block text-sm font-medium">Specialty (optional)</label>
                <select name="specialty" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    <option value="">None</option>
                    @foreach ($specialties as $specialty)
                        <option value="{{ $specialty }}" @selected(old('specialty', $practitioner->specialty) === $specialty)>{{ $specialty }}</option>
                    @endforeach
                </select>
                @error('specialty')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
            </div>
            <fieldset>
                <legend class="block text-sm font-medium">Branches</legend>
                <p class="mt-1 text-xs text-slate-500">Select every site this clinician may serve (including telemedicine). Nothing is selected by default.</p>
                @php
                    $selectedBranchIds = collect(old('branch_ids', $practitioner->assignedBranchIds()));
                @endphp
                <div class="mt-2 space-y-2 rounded-lg border border-slate-200 p-3">
                    @forelse ($branches as $branch)
                        <label class="flex items-center gap-2 text-sm text-slate-700">
                            <input type="checkbox" name="branch_ids[]" value="{{ $branch->id }}"
                                   @checked($selectedBranchIds->contains($branch->id))
                                   class="rounded border-slate-300 text-sky-600">
                            {{ $branch->name }}
                        </label>
                    @empty
                        <p class="text-sm text-slate-500">Create a branch first under Settings → Branches.</p>
                    @endforelse
                </div>
                @error('branch_ids')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                @error('branch_ids.*')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
            </fieldset>
            <div>
                <label class="block text-sm font-medium">Department (optional)</label>
                <select name="department_id" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    <option value="">None</option>
                    @foreach ($departments as $department)
                        <option value="{{ $department->id }}" @selected(old('department_id', $practitioner->department_id) == $department->id)>{{ $department->labelForSelect() }}</option>
                    @endforeach
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium">Link team member (optional)</label>
                <select name="member_id" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    <option value="">No login linked</option>
                    @foreach ($members as $member)
                        <option value="{{ $member->id }}" @selected(old('member_id', $practitioner->member_id) == $member->id)>
                            {{ $member->user_ref }} · {{ config('care.roles.'.$member->role, $member->role) }}
                        </option>
                    @endforeach
                </select>
            </div>
            <label class="flex items-center gap-2 text-sm">
                <input type="checkbox" name="is_active" value="1" @checked(old('is_active', $practitioner->is_active))>
                Active
            </label>
            <button type="submit" class="btn-primary w-full">Save changes</button>
        </form>

        <form method="POST" action="{{ route('care.practitioners.destroy', $practitioner) }}" class="mt-4"
              onsubmit="return confirm('Remove this practitioner from Care? Existing appointments keep their history.')">
            @csrf
            @method('DELETE')
            <button type="submit" class="btn-secondary w-full text-rose-700">Remove practitioner</button>
        </form>
    </div>
</x-app-layout>
