<x-app-layout title="Unit assignments">
    <div class="space-y-6">
        <x-care.page-hero
            badge="Primary · Temporary · Specialty"
            title="Unit assignments"
            description="Where staff belong — department, care unit, and post over a date range. Use the duty roster for day/evening/night shift assignments."
            :stats="[
                ['value' => number_format($heroStats['total']), 'label' => 'Assignments'],
                ['value' => number_format($heroStats['active']), 'label' => 'Effective today'],
                ['value' => number_format($heroStats['primary']), 'label' => 'Primary'],
                ['value' => number_format($heroStats['temporary']), 'label' => 'Temporary'],
            ]">
            <x-slot name="actions">
                <a href="{{ route('care.staff-assignments.create') }}" class="btn-primary">Add unit assignment</a>
            </x-slot>
        </x-care.page-hero>

        <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">Staff</th>
                        <th class="px-4 py-3">Kind</th>
                        <th class="px-4 py-3">Post</th>
                        <th class="px-4 py-3">Unit / Dept</th>
                        <th class="px-4 py-3">Dates</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 ($assignments as $assignment)
                        <tr>
                            <td class="px-4 py-3 font-medium">{{ $memberLabels[$assignment->member_id] ?? ('#'.$assignment->member_id) }}</td>
                            <td class="px-4 py-3">{{ $kinds[$assignment->kind] ?? $assignment->kind }}</td>
                            <td class="px-4 py-3">{{ $assignmentRoles[$assignment->assignment_role] ?? ($assignment->assignment_role ?: '—') }}</td>
                            <td class="px-4 py-3">
                                {{ $assignment->careUnit?->name ?? '—' }}
                                @if ($assignment->department)
                                    <div class="text-xs text-slate-400">{{ $assignment->department->name }}</div>
                                @endif
                            </td>
                            <td class="px-4 py-3 whitespace-nowrap">
                                {{ $assignment->starts_on->format('Y-m-d') }}
                                →
                                {{ $assignment->ends_on?->format('Y-m-d') ?? 'open' }}
                            </td>
                            <td class="px-4 py-3">
                                {{ $statuses[$assignment->status] ?? $assignment->status }}
                                @if ($assignment->isEffectiveOn())
                                    <span class="ml-1 text-xs text-emerald-600">today</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-right whitespace-nowrap">
                                <a href="{{ route('care.staff-assignments.edit', $assignment) }}" class="text-sky-600">Edit</a>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="7" class="px-4 py-6 text-center text-slate-500">No unit assignments yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</x-app-layout>
