<x-app-layout title="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>
                {{ $statuses[$handover->status] ?? $handover->status }}
            </p>
            <h1 class="mt-1 text-2xl font-semibold text-slate-900">
                {{ $shiftCodes[$handover->from_shift] ?? ($handover->from_shift ?: 'Shift') }}
                →
                {{ $shiftCodes[$handover->to_shift] ?? ($handover->to_shift ?: 'Shift') }}
            </h1>
            <p class="mt-1 text-sm text-slate-500">
                {{ ($handover->handed_over_at ?? $handover->created_at)?->format('d M Y H:i') }}
                @if ($handover->received_by) · Received by {{ $handover->received_by }} @endif
            </p>
        </div>

        <div class="space-y-4 rounded-2xl border border-slate-200 bg-white p-6 text-sm">
            <div>
                <h2 class="font-semibold text-slate-900">Situation</h2>
                <p class="mt-1 text-slate-700 whitespace-pre-wrap">{{ $handover->situation ?: '—' }}</p>
            </div>
            <div>
                <h2 class="font-semibold text-slate-900">Background</h2>
                <p class="mt-1 text-slate-700 whitespace-pre-wrap">{{ $handover->background ?: '—' }}</p>
            </div>
            <div>
                <h2 class="font-semibold text-slate-900">Assessment</h2>
                <p class="mt-1 text-slate-700 whitespace-pre-wrap">{{ $handover->assessment ?: '—' }}</p>
            </div>
            <div>
                <h2 class="font-semibold text-slate-900">Recommendation</h2>
                <p class="mt-1 text-slate-700 whitespace-pre-wrap">{{ $handover->recommendation ?: '—' }}</p>
            </div>
        </div>

        <div class="rounded-2xl border border-slate-200 bg-white p-6">
            <h2 class="text-sm font-semibold text-slate-900">Patient summaries</h2>
            <div class="mt-3 space-y-3">
                @forelse ($handover->patient_summaries ?? [] as $summary)
                    <div class="rounded-xl border border-slate-100 bg-slate-50 p-3 text-sm">
                        <p class="font-medium">{{ $summary['patient_name'] ?? 'Patient' }} @if (!empty($summary['bed'])) · {{ $summary['bed'] }} @endif</p>
                        <p class="mt-1 text-slate-600 whitespace-pre-wrap">{{ $summary['note'] ?: '—' }}</p>
                    </div>
                @empty
                    <p class="text-sm text-slate-500">No patient lines.</p>
                @endforelse
            </div>
        </div>

        @if ($canWrite)
            <form method="POST" action="{{ route('care.care-units.handovers.complete', [$unit, $handover]) }}">
                @csrf
                <input type="hidden" name="from_shift" value="{{ $handover->from_shift }}">
                <input type="hidden" name="to_shift" value="{{ $handover->to_shift }}">
                <input type="hidden" name="received_by" value="{{ $handover->received_by }}">
                <input type="hidden" name="situation" value="{{ $handover->situation }}">
                <input type="hidden" name="background" value="{{ $handover->background }}">
                <input type="hidden" name="assessment" value="{{ $handover->assessment }}">
                <input type="hidden" name="recommendation" value="{{ $handover->recommendation }}">
                @foreach ($handover->patient_summaries ?? [] as $i => $summary)
                    <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'] ?? '' }}">
                    <input type="hidden" name="patient_summaries[{{ $i }}][note]" value="{{ $summary['note'] ?? '' }}">
                @endforeach
                <button type="submit" class="btn-primary">Mark completed</button>
            </form>
        @endif
    </div>
</x-app-layout>
