@php
    $isWalkIn = $isWalkIn ?? false;
    $patientOptions = collect($patients)->map(fn ($patient) => [
        'value' => (string) $patient->id,
        'label' => $patient->fullName().' ('.$patient->patient_number.')',
        'search' => trim($patient->fullName().' '.$patient->patient_number.' '.($patient->phone ?? '').' '.($patient->email ?? '')),
    ])->all();
@endphp

<div class="grid gap-4 sm:grid-cols-2">
    <div>
        <label class="block text-sm font-medium text-slate-700">Branch</label>
        <select name="branch_id" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
            @foreach ($branches as $branch)
                <option value="{{ $branch->id }}" @selected(old('branch_id', $defaultBranch) == $branch->id)>{{ $branch->name }}</option>
            @endforeach
        </select>
    </div>
    <div>
        <label class="block text-sm font-medium text-slate-700">Patient</label>
        <x-searchable-select
            name="patient_id"
            :options="$patientOptions"
            :selected="old('patient_id')"
            :required="true"
            placeholder="Search name, number, phone…"
            empty-label="Select patient…"
        />
    </div>
    <div>
        <label class="block text-sm font-medium text-slate-700">Practitioner</label>
        <select name="practitioner_id" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
            <option value="">Any / unassigned</option>
            @foreach ($practitioners as $practitioner)
                <option value="{{ $practitioner->id }}" @selected(old('practitioner_id') == $practitioner->id)>{{ $practitioner->labelForSelect() }}</option>
            @endforeach
        </select>
    </div>
    <div>
        <label class="block text-sm font-medium text-slate-700">Department</label>
        <select name="department_id" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
            <option value="">—</option>
            @foreach ($departments as $department)
                <option value="{{ $department->id }}" @selected(old('department_id') == $department->id)>{{ $department->labelForSelect() }}</option>
            @endforeach
        </select>
    </div>
    @unless ($isWalkIn)
        <div class="sm:col-span-2">
            <label class="block text-sm font-medium text-slate-700">Scheduled date & time</label>
            <input type="datetime-local" name="scheduled_at" value="{{ old('scheduled_at') }}" required
                   class="mt-1 w-full rounded-lg border-slate-300 text-sm">
        </div>
    @endunless
    <div class="sm:col-span-2">
        <label class="block text-sm font-medium text-slate-700">Reason for visit</label>
        <input type="text" name="reason" value="{{ old('reason') }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
    </div>
    <div class="sm:col-span-2">
        <label class="block text-sm font-medium text-slate-700">Notes</label>
        <textarea name="notes" rows="3" class="mt-1 w-full rounded-lg border-slate-300 text-sm">{{ old('notes') }}</textarea>
    </div>
</div>
