<x-app-layout title="My care unit">
    <div class="space-y-6">
        <div class="flex flex-wrap items-start justify-between gap-4">
            <div>
                <h1 class="text-2xl font-semibold text-slate-900">My care unit</h1>
                <p class="mt-1 text-sm text-slate-500">
                    Patients on units you’re rostered or placed on today — chart meds, vitals, notes, and handovers.
                </p>
            </div>
            <div class="flex flex-wrap gap-2">
                <a href="{{ route('care.my-shifts') }}" class="btn-secondary">My shifts</a>
                @if ($canServicesHub)
                    <a href="{{ route('care.nursing.services') }}" class="btn-secondary">Nursing Services</a>
                @endif
            </div>
        </div>

        @if ($units->isEmpty())
            <div class="rounded-2xl border border-dashed border-slate-200 bg-white px-6 py-12 text-center">
                <p class="text-base font-medium text-slate-900">No unit assigned for today</p>
                <p class="mt-2 text-sm text-slate-500">
                    Ask a charge nurse or administrator to put you on the duty roster or a unit placement.
                    You can still check
                    <a href="{{ route('care.my-shifts') }}" class="font-medium text-indigo-600 hover:text-indigo-800">My shifts</a>.
                </p>
            </div>
        @else
            @if ($units->count() > 1)
                <div class="flex flex-wrap gap-2">
                    @foreach ($units as $unit)
                        <a
                            href="{{ route('care.nursing.station', ['unit' => $unit->id]) }}"
                            @class([
                                'rounded-lg px-3 py-1.5 text-sm font-medium transition',
                                'bg-slate-900 text-white' => $selectedUnit && (int) $selectedUnit->id === (int) $unit->id,
                                'border border-slate-200 bg-white text-slate-700 hover:bg-slate-50' => ! $selectedUnit || (int) $selectedUnit->id !== (int) $unit->id,
                            ])
                        >
                            {{ $unit->name }}
                            <span class="ml-1 tabular-nums opacity-70">{{ ($patientsByUnit[$unit->id] ?? collect())->count() }}</span>
                        </a>
                    @endforeach
                </div>
            @endif

            @php
                $unit = $selectedUnit;
                $patients = $patientsByUnit[$unit->id] ?? collect();
                $duty = $dutyByUnit[$unit->id] ?? collect();
                $placement = $placementByUnit[$unit->id] ?? null;
            @endphp

            <div class="rounded-2xl border border-slate-200 bg-white p-5">
                <div class="flex flex-wrap items-start justify-between gap-4">
                    <div>
                        <h2 class="text-lg font-semibold text-slate-900">{{ $unit->name }}</h2>
                        <p class="mt-0.5 text-sm text-slate-500">
                            {{ $unit->department?->name }}
                            @if ($unit->department?->branch)
                                · {{ $unit->department->branch->name }}
                            @endif
                        </p>
                        <div class="mt-2 flex flex-wrap gap-2 text-xs text-slate-600">
                            @foreach ($duty as $entry)
                                <span class="inline-flex items-center gap-1.5 rounded-md bg-teal-50 px-2 py-1 font-medium text-teal-800">
                                    <span class="h-1.5 w-1.5 rounded-full" style="background: {{ $entry->shift?->color ?? '#14b8a6' }}"></span>
                                    On duty · {{ $entry->shift?->label ?? 'Shift' }}
                                    @if ($entry->shift)
                                        ({{ $entry->shift->timeLabel() }})
                                    @endif
                                </span>
                            @endforeach
                            @if ($placement && $duty->isEmpty())
                                <span class="inline-flex rounded-md bg-slate-100 px-2 py-1 font-medium text-slate-700">
                                    Unit placement · {{ str_replace('_', ' ', $placement->kind) }}
                                </span>
                            @elseif ($placement && $duty->isNotEmpty())
                                <span class="inline-flex rounded-md bg-slate-100 px-2 py-1 text-slate-600">
                                    Also placed · {{ str_replace('_', ' ', $placement->kind) }}
                                </span>
                            @endif
                        </div>
                    </div>
                    <div class="flex flex-wrap gap-2">
                        @if ($canMar)
                            <a href="{{ route('care.care-units.mar', $unit) }}" class="btn-primary">MAR round</a>
                        @endif
                        @if ($canAssess || $canVitals)
                            <a href="{{ route('care.care-units.assessments', $unit) }}" class="btn-secondary">Vitals &amp; assess</a>
                        @endif
                        @if ($canNotes)
                            <a href="{{ route('care.care-units.notes', $unit) }}" class="btn-secondary">Notes</a>
                        @endif
                        @if ($canHandover)
                            <a href="{{ route('care.care-units.handovers', $unit) }}" class="btn-secondary">Handover</a>
                        @endif
                    </div>
                </div>

                <div class="mt-6 overflow-x-auto">
                    <table class="min-w-full text-sm">
                        <thead class="border-b border-slate-100 text-left text-xs uppercase tracking-wide text-slate-500">
                            <tr>
                                <th class="pb-2 pr-4">Patient</th>
                                <th class="pb-2 pr-4">Bed</th>
                                <th class="pb-2 pr-4">Placed</th>
                                <th class="pb-2 text-right">Chart</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-slate-50">
                            @forelse ($patients as $visit)
                                <tr>
                                    <td class="py-3 pr-4">
                                        <p class="font-medium text-slate-900">{{ $visit->patient?->fullName() ?? 'Patient' }}</p>
                                        @if ($visit->patient?->patient_number)
                                            <p class="text-xs text-slate-400">{{ $visit->patient->patient_number }}</p>
                                        @endif
                                    </td>
                                    <td class="py-3 pr-4 text-slate-600">{{ $visit->bed?->label ?? '—' }}</td>
                                    <td class="py-3 pr-4 whitespace-nowrap text-slate-500">
                                        {{ $visit->placed_at?->format('d M H:i') ?? '—' }}
                                    </td>
                                    <td class="py-3 text-right whitespace-nowrap">
                                        @if ($canMar)
                                            <a href="{{ route('care.visits.mar', $visit) }}" class="mr-3 font-medium text-indigo-600 hover:text-indigo-800">MAR</a>
                                        @endif
                                        @if ($canAssess || $canVitals)
                                            <a href="{{ route('care.care-units.assessments', $unit) }}#visit-{{ $visit->id }}" class="mr-3 text-slate-600 hover:text-slate-900">Vitals</a>
                                        @endif
                                        @if ($canNotes)
                                            <a href="{{ route('care.care-units.notes', $unit) }}" class="text-slate-600 hover:text-slate-900">Notes</a>
                                        @endif
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="4" class="py-10 text-center text-slate-500">
                                        No patients placed on this unit yet.
                                        @if ($canServicesHub)
                                            Placements are managed from the care unit board.
                                        @else
                                            Ask a charge nurse to place patients on this unit.
                                        @endif
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        @endif
    </div>
</x-app-layout>
