@php
    $summaries = old('patient_summaries', $patientSummaries ?? $handover?->patient_summaries ?? []);
@endphp

<x-app-layout title="New handover · {{ $unit->name }}">
    <div class="mx-auto max-w-3xl space-y-6">
        <div>
            <p class="text-sm text-slate-500">
                <a href="{{ route('care.care-units.handovers', $unit) }}" class="text-indigo-600 hover:text-indigo-800">Handovers</a>
                <span class="text-slate-300">/</span>
                New
            </p>
            <h1 class="mt-1 text-2xl font-semibold text-slate-900">SBAR ward handover</h1>
        </div>

        <form method="POST" action="{{ route('care.care-units.handovers.store', $unit) }}" class="space-y-5 rounded-2xl border border-slate-200 bg-white p-6">
            @csrf
            <div class="grid gap-4 sm:grid-cols-3">
                <div>
                    <label class="block text-sm font-medium">From shift</label>
                    <select name="from_shift" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                        <option value="">—</option>
                        @foreach ($shiftCodes as $value => $label)
                            <option value="{{ $value }}" @selected(old('from_shift') === $value)>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-medium">To shift</label>
                    <select name="to_shift" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                        <option value="">—</option>
                        @foreach ($shiftCodes as $value => $label)
                            <option value="{{ $value }}" @selected(old('to_shift') === $value)>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-medium">Received by</label>
                    <input type="text" name="received_by" value="{{ old('received_by') }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm" placeholder="Incoming nurse name">
                </div>
            </div>

            <div>
                <label class="block text-sm font-medium">Situation</label>
                <textarea name="situation" rows="2" class="mt-1 w-full rounded-lg border-slate-300 text-sm">{{ old('situation') }}</textarea>
            </div>
            <div>
                <label class="block text-sm font-medium">Background</label>
                <textarea name="background" rows="2" class="mt-1 w-full rounded-lg border-slate-300 text-sm">{{ old('background') }}</textarea>
            </div>
            <div>
                <label class="block text-sm font-medium">Assessment</label>
                <textarea name="assessment" rows="2" class="mt-1 w-full rounded-lg border-slate-300 text-sm">{{ old('assessment') }}</textarea>
            </div>
            <div>
                <label class="block text-sm font-medium">Recommendation</label>
                <textarea name="recommendation" rows="2" class="mt-1 w-full rounded-lg border-slate-300 text-sm">{{ old('recommendation') }}</textarea>
            </div>

            <div>
                <h2 class="text-sm font-semibold text-slate-900">Patients on unit</h2>
                <div class="mt-3 space-y-3">
                    @forelse ($summaries as $i => $summary)
                        <div class="rounded-xl border border-slate-100 bg-slate-50 p-3">
                            <input type="hidden" name="patient_summaries[{{ $i }}][visit_id]" value="{{ $summary['visit_id'] ?? '' }}">
                            <input type="hidden" name="patient_summaries[{{ $i }}][patient_name]" value="{{ $summary['patient_name'] ?? '' }}">
                            <input type="hidden" name="patient_summaries[{{ $i }}][bed]" value="{{ $summary['bed'] ?? '' }}">
                            <p class="text-sm font-medium">{{ $summary['patient_name'] ?? 'Patient' }} @if (!empty($summary['bed'])) · {{ $summary['bed'] }} @endif</p>
                            <textarea name="patient_summaries[{{ $i }}][note]" rows="2" class="mt-2 w-full rounded-lg border-slate-300 text-sm" placeholder="Handover points for this patient">{{ $summary['note'] ?? '' }}</textarea>
                        </div>
                    @empty
                        <p class="text-sm text-slate-500">No placed patients to include.</p>
                    @endforelse
                </div>
            </div>

            <div class="flex flex-wrap gap-2">
                <button type="submit" class="btn-secondary">Save draft</button>
                <button type="submit" name="complete" value="1" class="btn-primary">Complete handover</button>
            </div>
        </form>
    </div>
</x-app-layout>
