@php
    $patient = $patient ?? null;
@endphp

<div class="space-y-6">
    <section class="rounded-2xl border border-slate-200 bg-white p-6">
        <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Demographics</h2>
        <div class="mt-4 grid gap-4 sm:grid-cols-2">
            <div>
                <label class="block text-sm font-medium text-slate-700">First name</label>
                <input type="text" name="first_name" value="{{ old('first_name', $patient?->first_name) }}" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700">Last name</label>
                <input type="text" name="last_name" value="{{ old('last_name', $patient?->last_name) }}" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700">Other names</label>
                <input type="text" name="other_names" value="{{ old('other_names', $patient?->other_names) }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700">Gender</label>
                <select name="gender" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    <option value="">—</option>
                    @foreach ($genders as $value => $label)
                        <option value="{{ $value }}" @selected(old('gender', $patient?->gender) === $value)>{{ $label }}</option>
                    @endforeach
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700">Date of birth</label>
                <input type="date" name="date_of_birth" value="{{ old('date_of_birth', $patient?->date_of_birth?->format('Y-m-d')) }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700">Phone</label>
                <input type="tel" name="phone" value="{{ old('phone', $patient?->phone) }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700">Email</label>
                <input type="email" name="email" value="{{ old('email', $patient?->email) }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700">National ID</label>
                <input type="text" name="national_id" value="{{ old('national_id', $patient?->national_id) }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700">Branch</label>
                <select name="branch_id" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    <option value="">—</option>
                    @foreach ($branches as $branch)
                        <option value="{{ $branch->id }}" @selected(old('branch_id', $patient?->branch_id) == $branch->id)>{{ $branch->name }}</option>
                    @endforeach
                </select>
            </div>
            <div class="sm:col-span-2">
                <label class="block text-sm font-medium text-slate-700">Address</label>
                <input type="text" name="address" value="{{ old('address', $patient?->address) }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700">City</label>
                <input type="text" name="city" value="{{ old('city', $patient?->city) }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700">Region</label>
                <input type="text" name="region" value="{{ old('region', $patient?->region) }}" 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', $patient?->notes) }}</textarea>
            </div>
        </div>
    </section>

    <section class="rounded-2xl border border-slate-200 bg-white p-6">
        <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Allergies</h2>
        <div class="mt-4 space-y-3">
            @for ($i = 0; $i < 3; $i++)
                @php $allergy = old("allergies.{$i}", $patient?->allergies[$i] ?? null); @endphp
                <div class="grid gap-3 sm:grid-cols-3">
                    <input type="text" name="allergies[{{ $i }}][allergen]" value="{{ is_array($allergy) ? ($allergy['allergen'] ?? '') : ($allergy?->allergen ?? '') }}" placeholder="Allergen" class="rounded-lg border-slate-300 text-sm">
                    <select name="allergies[{{ $i }}][severity]" class="rounded-lg border-slate-300 text-sm">
                        @foreach ($allergySeverities as $value => $label)
                            <option value="{{ $value }}" @selected((is_array($allergy) ? ($allergy['severity'] ?? '') : ($allergy?->severity ?? '')) === $value)>{{ $label }}</option>
                        @endforeach
                    </select>
                    <input type="text" name="allergies[{{ $i }}][notes]" value="{{ is_array($allergy) ? ($allergy['notes'] ?? '') : ($allergy?->notes ?? '') }}" placeholder="Notes" class="rounded-lg border-slate-300 text-sm">
                </div>
            @endfor
        </div>
    </section>

    <section class="rounded-2xl border border-slate-200 bg-white p-6">
        <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Chronic conditions</h2>
        <div class="mt-4 space-y-3">
            @for ($i = 0; $i < 3; $i++)
                @php $condition = old("conditions.{$i}", $patient?->conditions[$i] ?? null); @endphp
                <div class="grid gap-3 sm:grid-cols-4">
                    <input type="text" name="conditions[{{ $i }}][condition]" value="{{ is_array($condition) ? ($condition['condition'] ?? '') : ($condition?->condition ?? '') }}" placeholder="Condition" class="rounded-lg border-slate-300 text-sm sm:col-span-2">
                    <input type="date" name="conditions[{{ $i }}][onset_date]" value="{{ is_array($condition) ? ($condition['onset_date'] ?? '') : ($condition?->onset_date?->format('Y-m-d') ?? '') }}" class="rounded-lg border-slate-300 text-sm">
                    <label class="flex items-center gap-2 text-sm"><input type="checkbox" name="conditions[{{ $i }}][is_chronic]" value="1" @checked(is_array($condition) ? ($condition['is_chronic'] ?? false) : ($condition?->is_chronic ?? false))> Chronic</label>
                </div>
            @endfor
        </div>
    </section>

    <section class="rounded-2xl border border-slate-200 bg-white p-6">
        <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Family history</h2>
        <div class="mt-4 space-y-3">
            @for ($i = 0; $i < 2; $i++)
                @php $family = old("family_history.{$i}", $patient?->familyHistory[$i] ?? null); @endphp
                <div class="grid gap-3 sm:grid-cols-2">
                    <input type="text" name="family_history[{{ $i }}][relation]" value="{{ is_array($family) ? ($family['relation'] ?? '') : ($family?->relation ?? '') }}" placeholder="Relation" class="rounded-lg border-slate-300 text-sm">
                    <input type="text" name="family_history[{{ $i }}][condition]" value="{{ is_array($family) ? ($family['condition'] ?? '') : ($family?->condition ?? '') }}" placeholder="Condition" class="rounded-lg border-slate-300 text-sm">
                </div>
            @endfor
        </div>
    </section>

    <section class="rounded-2xl border border-slate-200 bg-white p-6">
        <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Emergency contact</h2>
        @php $contact = old('emergency_contacts.0', $patient?->emergencyContacts->first()); @endphp
        <div class="mt-4 grid gap-4 sm:grid-cols-2">
            <input type="text" name="emergency_contacts[0][name]" value="{{ is_array($contact) ? ($contact['name'] ?? '') : ($contact?->name ?? '') }}" placeholder="Name" class="rounded-lg border-slate-300 text-sm">
            <input type="tel" name="emergency_contacts[0][phone]" value="{{ is_array($contact) ? ($contact['phone'] ?? '') : ($contact?->phone ?? '') }}" placeholder="Phone" class="rounded-lg border-slate-300 text-sm">
            <input type="text" name="emergency_contacts[0][relationship]" value="{{ is_array($contact) ? ($contact['relationship'] ?? '') : ($contact?->relationship ?? '') }}" placeholder="Relationship" class="rounded-lg border-slate-300 text-sm">
            <label class="flex items-center gap-2 text-sm"><input type="checkbox" name="emergency_contacts[0][is_primary]" value="1" @checked(is_array($contact) ? ($contact['is_primary'] ?? true) : ($contact?->is_primary ?? true))> Primary contact</label>
        </div>
    </section>

    <section class="rounded-2xl border border-slate-200 bg-white p-6">
        <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Insurance</h2>
        @php $insurance = old('insurance.0', $patient?->insurancePolicies->first()); @endphp
        <div class="mt-4 grid gap-4 sm:grid-cols-2">
            <input type="text" name="insurance[0][provider_name]" value="{{ is_array($insurance) ? ($insurance['provider_name'] ?? '') : ($insurance?->provider_name ?? '') }}" placeholder="Provider" class="rounded-lg border-slate-300 text-sm">
            <input type="text" name="insurance[0][policy_number]" value="{{ is_array($insurance) ? ($insurance['policy_number'] ?? '') : ($insurance?->policy_number ?? '') }}" placeholder="Policy number" class="rounded-lg border-slate-300 text-sm">
            <input type="text" name="insurance[0][coverage_type]" value="{{ is_array($insurance) ? ($insurance['coverage_type'] ?? '') : ($insurance?->coverage_type ?? '') }}" placeholder="Coverage type" class="rounded-lg border-slate-300 text-sm">
            <input type="date" name="insurance[0][expiry_date]" value="{{ is_array($insurance) ? ($insurance['expiry_date'] ?? '') : ($insurance?->expiry_date?->format('Y-m-d') ?? '') }}" class="rounded-lg border-slate-300 text-sm">
        </div>
    </section>

    <section class="rounded-2xl border border-slate-200 bg-white p-6">
        <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Documents</h2>
        <input type="file" name="attachments[]" multiple accept=".pdf,.jpg,.jpeg,.png,.webp" class="mt-4 block w-full text-sm text-slate-600 file:mr-3 file:rounded-lg file:border-0 file:bg-sky-50 file:px-3 file:py-2 file:text-sm file:font-medium file:text-sky-700">
        <p class="mt-2 text-xs text-slate-500">PDF or images, up to 10 MB each.</p>
    </section>
</div>
