<x-app-layout title="Handovers · {{ $unit->name }}">
    <div class="space-y-6">
        <div class="flex flex-wrap items-start justify-between gap-4">
            <div>
                <p class="text-sm text-slate-500">
                    @include('care.nursing.partials.unit-crumb', ['unit' => $unit])
                    <span class="text-slate-300">/</span>
                    Ward handovers
                </p>
                <h1 class="mt-1 text-2xl font-semibold text-slate-900">Shift handovers</h1>
                <p class="mt-1 text-sm text-slate-500">SBAR handovers scoped to this care unit.</p>
            </div>
            @if ($canWrite)
                <a href="{{ route('care.care-units.handovers.create', $unit) }}" class="btn-primary">New handover</a>
            @endif
        </div>

        <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white">
            <table class="min-w-full text-sm">
                <thead class="bg-slate-50 text-left text-xs uppercase text-slate-500">
                    <tr>
                        <th class="px-4 py-3">When</th>
                        <th class="px-4 py-3">Shifts</th>
                        <th class="px-4 py-3">Status</th>
                        <th class="px-4 py-3"></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-50">
                    @forelse ($handovers as $handover)
                        <tr>
                            <td class="px-4 py-3 whitespace-nowrap">
                                {{ ($handover->handed_over_at ?? $handover->created_at)?->format('d M Y H:i') }}
                            </td>
                            <td class="px-4 py-3">
                                {{ $shiftCodes[$handover->from_shift] ?? ($handover->from_shift ?: '—') }}
                                →
                                {{ $shiftCodes[$handover->to_shift] ?? ($handover->to_shift ?: '—') }}
                            </td>
                            <td class="px-4 py-3">{{ $statuses[$handover->status] ?? $handover->status }}</td>
                            <td class="px-4 py-3 text-right">
                                <a href="{{ route('care.care-units.handovers.show', [$unit, $handover]) }}" class="text-sky-600">Open</a>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="4" class="px-4 py-8 text-center text-slate-500">No handovers yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</x-app-layout>
