<x-app-layout title="Nursing Services">
    <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">Nursing Services</h1>
                <p class="mt-1 text-sm text-slate-500">
                    Nurse registry, today’s duty allocation, and shortcuts into ward boards.
                    @unless ($moduleEnabled)
                        <span class="text-amber-700">Module not enabled for this organization — enable under Settings → Modules.</span>
                    @endunless
                </p>
            </div>
            <div class="flex flex-wrap gap-2">
                @if ($canRoster)
                    <a href="{{ route('care.shifts.index') }}" class="btn-secondary">Shift templates</a>
                @endif
                <a href="{{ route('care.care-units.index') }}" class="btn-secondary">Care units</a>
                <a href="{{ route('care.staff-assignments.index') }}" class="btn-secondary">Unit assignments</a>
            </div>
        </div>

        <div class="grid gap-6 lg:grid-cols-3">
            <div class="rounded-2xl border border-slate-200 bg-white p-5 lg:col-span-1">
                <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Nurse registry</h2>
                <p class="mt-1 text-2xl font-semibold text-slate-900">{{ $nurses->count() }}</p>
                <ul class="mt-4 max-h-80 space-y-2 overflow-y-auto text-sm">
                    @forelse ($nurses as $nurse)
                        <li class="flex items-start justify-between gap-2 border-b border-slate-50 pb-2">
                            <span class="font-medium text-slate-800">{{ $nurseLabels[$nurse->id] ?? $nurse->user_ref }}</span>
                        </li>
                    @empty
                        <li class="text-slate-500">No nursing-role members yet.</li>
                    @endforelse
                </ul>
            </div>

            <div class="rounded-2xl border border-slate-200 bg-white p-5 lg:col-span-2">
                <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Today’s shift assignments</h2>
                <div class="mt-4 overflow-x-auto">
                    <table class="min-w-full text-sm">
                        <thead class="text-left text-xs uppercase text-slate-500">
                            <tr>
                                <th class="pb-2 pr-4">Unit</th>
                                <th class="pb-2 pr-4">Shift</th>
                                <th class="pb-2">Staff</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-slate-100">
                            @forelse ($todayRoster as $entry)
                                <tr>
                                    <td class="py-2.5 pr-4">
                                        @if ($canViewRoster)
                                            <a href="{{ route('care.care-units.roster', $entry->careUnit) }}" class="text-indigo-600 hover:text-indigo-800">
                                                {{ $entry->careUnit?->name ?? '—' }}
                                            </a>
                                        @else
                                            {{ $entry->careUnit?->name ?? '—' }}
                                        @endif
                                    </td>
                                    <td class="py-2.5 pr-4 whitespace-nowrap">
                                        <span class="mr-1 inline-block h-2 w-2 rounded-full" style="background: {{ $entry->shift?->color ?? '#94a3b8' }}"></span>
                                        {{ $entry->shift?->label ?? '—' }}
                                    </td>
                                    <td class="py-2.5">{{ $nurseLabels[$entry->member_id] ?? ($entry->member?->user_ref ?? '—') }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="3" class="py-6 text-slate-500">No shift assignments for today.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="rounded-2xl border border-slate-200 bg-white p-5">
            <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Unit assignments</h2>
            <p class="mt-1 text-sm text-slate-500">Home and float placements effective today (primary, temporary, specialty) — separate from shift duty.</p>
            <div class="mt-4 overflow-x-auto">
                <table class="min-w-full text-sm">
                    <thead class="text-left text-xs uppercase text-slate-500">
                        <tr>
                            <th class="pb-2 pr-4">Staff</th>
                            <th class="pb-2 pr-4">Unit / department</th>
                            <th class="pb-2 pr-4">Kind</th>
                            <th class="pb-2">Dates</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100">
                        @forelse ($allocations as $assignment)
                            <tr>
                                <td class="py-2.5 pr-4 font-medium">
                                    {{ $nurseLabels[$assignment->member_id] ?? ($assignment->member?->user_ref ?? '—') }}
                                </td>
                                <td class="py-2.5 pr-4">
                                    {{ $assignment->careUnit?->name ?? $assignment->department?->name ?? '—' }}
                                </td>
                                <td class="py-2.5 pr-4 capitalize">{{ $assignment->kind }}</td>
                                <td class="py-2.5 whitespace-nowrap text-slate-500">
                                    {{ $assignment->starts_on?->format('d M Y') }}
                                    –
                                    {{ $assignment->ends_on?->format('d M Y') ?? 'open' }}
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="py-6 text-slate-500">No active unit assignments today.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <div class="rounded-2xl border border-slate-200 bg-white p-5">
            <h2 class="text-sm font-semibold uppercase tracking-wide text-slate-500">Care units</h2>
            <div class="mt-4 grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
                @forelse ($units as $unit)
                    <div class="rounded-xl border border-slate-100 px-4 py-3">
                        <a href="{{ route('care.care-units.show', $unit) }}" class="font-medium text-slate-900 hover:text-indigo-700">{{ $unit->name }}</a>
                        <p class="mt-0.5 text-xs text-slate-500">{{ $unit->department?->name }} · {{ str_replace('_', ' ', $unit->kind) }}</p>
                        <div class="mt-3 flex flex-wrap gap-2 text-xs">
                            @if ($canViewRoster)
                                <a href="{{ route('care.care-units.roster', $unit) }}" class="text-indigo-600 hover:text-indigo-800">Shifts</a>
                            @endif
                            <a href="{{ route('care.care-units.mar', $unit) }}" class="text-slate-600 hover:text-slate-900">MAR</a>
                            <a href="{{ route('care.care-units.notes', $unit) }}" class="text-slate-600 hover:text-slate-900">Notes</a>
                            <a href="{{ route('care.care-units.handovers', $unit) }}" class="text-slate-600 hover:text-slate-900">Handover</a>
                            <a href="{{ route('care.care-units.assessments', $unit) }}" class="text-slate-600 hover:text-slate-900">Assess</a>
                        </div>
                    </div>
                @empty
                    <p class="text-sm text-slate-500 sm:col-span-2 lg:col-span-3">No care units yet — ask an administrator to set them up.</p>
                @endforelse
            </div>
        </div>
    </div>
</x-app-layout>
