<x-app-layout title="Assessments · {{ $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>
                    Nursing assessments
                </p>
                <h1 class="mt-1 text-2xl font-semibold text-slate-900">Ward assessment board</h1>
                <p class="mt-1 text-sm text-slate-500">NEWS2, Braden, Morse, pain, and visit vitals for placed patients.</p>
            </div>
            <a href="{{ route('care.care-units.performance', $unit) }}" class="btn-secondary">Performance</a>
        </div>

        @unless ($engineEnabled)
            <div class="rounded-xl border border-amber-200 bg-amber-50 px-4 py-3 text-sm text-amber-900">
                Assessments engine is disabled for this organization. Enable it under Settings → Rollout.
            </div>
        @endunless

        <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">Patient</th>
                        <th class="px-4 py-3">Latest vitals</th>
                        @foreach ($templates as $template)
                            <th class="px-4 py-3">{{ $template->code }}</th>
                        @endforeach
                        <th class="px-4 py-3"></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-50">
                    @forelse ($rows as $row)
                        @php
                            $visit = $row['visit'];
                            $vitals = $row['vitals'];
                        @endphp
                        <tr>
                            <td class="px-4 py-3 font-medium">
                                {{ $row['patient']?->fullName() }}
                                <div class="text-xs text-slate-400">{{ $row['bed']?->label ?? 'No bed' }}</div>
                            </td>
                            <td class="px-4 py-3 text-xs text-slate-600">
                                @if ($vitals)
                                    BP {{ $vitals->bp_systolic }}/{{ $vitals->bp_diastolic }}
                                    · P {{ $vitals->pulse }}
                                    · SpO₂ {{ $vitals->spo2 }}
                                    <div class="text-slate-400">{{ $vitals->recorded_at?->format('d M H:i') }}</div>
                                @else
                                    —
                                @endif
                            </td>
                            @foreach ($templates as $template)
                                @php $assessment = $row['assessments']->get($template->code); @endphp
                                <td class="px-4 py-3">
                                    @if ($assessment)
                                        <a href="{{ route('care.assessments.show', $assessment) }}" class="text-sky-600 hover:text-sky-800">
                                            {{ $assessment->status === 'completed'
                                                ? ($assessment->score?->severity_label ?? ($assessment->score?->total_score !== null ? $assessment->score->total_score : 'Done'))
                                                : 'Draft' }}
                                        </a>
                                    @else
                                        <span class="text-slate-400">—</span>
                                    @endif
                                </td>
                            @endforeach
                            <td class="px-4 py-3 text-right">
                                @if ($canCapture && $engineEnabled)
                                    <div class="inline-flex flex-wrap justify-end gap-1">
                                        @foreach ($templates as $template)
                                            <form method="POST" action="{{ route('care.care-units.assessments.start', [$unit, $visit]) }}">
                                                @csrf
                                                <input type="hidden" name="template_code" value="{{ $template->code }}">
                                                <button type="submit" class="rounded border border-slate-200 px-2 py-1 text-xs text-slate-700 hover:bg-slate-50">{{ strtoupper($template->code) }}</button>
                                            </form>
                                        @endforeach
                                    </div>
                                @endif
                            </td>
                        </tr>
                        @if ($canVitals)
                            <tr class="bg-slate-50/60">
                                <td colspan="{{ 3 + $templates->count() }}" class="px-4 py-3">
                                    <form method="POST" action="{{ route('care.care-units.vitals.store', [$unit, $visit]) }}" class="grid gap-2 sm:grid-cols-7">
                                        @csrf
                                        <input type="number" name="bp_systolic" placeholder="Sys" class="rounded-lg border-slate-300 text-sm">
                                        <input type="number" name="bp_diastolic" placeholder="Dia" class="rounded-lg border-slate-300 text-sm">
                                        <input type="number" name="pulse" placeholder="Pulse" class="rounded-lg border-slate-300 text-sm">
                                        <input type="number" step="0.1" name="temperature" placeholder="Temp" class="rounded-lg border-slate-300 text-sm">
                                        <input type="number" name="spo2" placeholder="SpO₂" class="rounded-lg border-slate-300 text-sm">
                                        <input type="number" name="respiratory_rate" placeholder="RR" class="rounded-lg border-slate-300 text-sm">
                                        <button type="submit" class="btn-secondary text-xs">Record vitals · {{ $row['patient']?->first_name }}</button>
                                    </form>
                                </td>
                            </tr>
                        @endif
                    @empty
                        <tr><td colspan="{{ 3 + max($templates->count(), 1) }}" class="px-4 py-8 text-center text-slate-500">No patients placed on this unit.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</x-app-layout>
