<x-app-layout title="Nursing notes · {{ $unit->name }}">
    <div class="space-y-6">
        <div>
            <p class="text-sm text-slate-500">
                @include('care.nursing.partials.unit-crumb', ['unit' => $unit])
                <span class="text-slate-300">/</span>
                Nursing notes
            </p>
            <h1 class="mt-1 text-2xl font-semibold text-slate-900">Nursing notes</h1>
            <p class="mt-1 text-sm text-slate-500">Chronologic unit documentation — not overwritten like specialty forms.</p>
        </div>

        @if ($canWrite)
            <form method="POST" action="{{ route('care.care-units.notes.store', $unit) }}" class="space-y-4 rounded-2xl border border-slate-200 bg-white p-6">
                @csrf
                <div class="grid gap-4 sm:grid-cols-3">
                    <div class="sm:col-span-2">
                        <label class="block text-sm font-medium">Patient visit</label>
                        <select name="visit_id" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                            <option value="">Select…</option>
                            @foreach ($placedVisits as $visit)
                                <option value="{{ $visit->id }}">
                                    {{ $visit->patient?->fullName() }}
                                    @if ($visit->bed) · {{ $visit->bed->label }} @endif
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium">Type</label>
                        <select name="note_type" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                            @foreach ($noteTypes as $value => $label)
                                <option value="{{ $value }}" @selected(old('note_type', 'progress') === $value)>{{ $label }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="grid gap-4 sm:grid-cols-3">
                    <div>
                        <label class="block text-sm font-medium">Shift</label>
                        <select name="shift_code" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                            <option value="">—</option>
                            @foreach ($shiftCodes as $value => $label)
                                <option value="{{ $value }}">{{ $label }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="sm:col-span-2">
                        <label class="block text-sm font-medium">Note</label>
                        <textarea name="body" rows="3" required class="mt-1 w-full rounded-lg border-slate-300 text-sm" placeholder="Observations, interventions, response…">{{ old('body') }}</textarea>
                    </div>
                </div>
                <button type="submit" class="btn-primary">Save note</button>
            </form>
        @endif

        <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">Patient</th>
                        <th class="px-4 py-3">Type</th>
                        <th class="px-4 py-3">Note</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-50">
                    @forelse ($notes as $note)
                        <tr>
                            <td class="px-4 py-3 whitespace-nowrap">{{ $note->recorded_at?->format('d M H:i') }}</td>
                            <td class="px-4 py-3 font-medium">{{ $note->patient?->fullName() }}</td>
                            <td class="px-4 py-3">{{ $noteTypes[$note->note_type] ?? $note->note_type }}</td>
                            <td class="px-4 py-3 text-slate-700">{{ $note->body }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="4" class="px-4 py-8 text-center text-slate-500">No nursing notes yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</x-app-layout>
