<x-app-layout :title="'Start assessment · '.$patient->fullName()">
    <x-care.consultation-return :consultation="$returnConsultation ?? null" />

    <div>
        <p class="text-xs font-medium uppercase tracking-wide text-slate-500">Start assessment</p>
        <h1 class="text-2xl font-semibold text-slate-900">{{ $patient->fullName() }}</h1>
        <p class="mt-1 text-sm text-slate-500">
            <a href="{{ route('care.patients.show', $patient) }}" class="text-sky-600 hover:text-sky-700">{{ $patient->patient_number }}</a>
        </p>
    </div>

    <section class="mt-6 rounded-2xl border border-slate-200 bg-white p-6">
        @if ($templates->isEmpty())
            <p class="text-sm text-slate-500">No forms are available for your role right now. Ask an administrator to confirm clinical forms are enabled in Settings.</p>
        @else
            <form method="POST" action="{{ ($returnConsultation ?? null)
                ? route('care.consultations.assessments.store', $returnConsultation)
                : route('care.assessments.store', $patient) }}" class="space-y-4">
                @csrf
                @if ($consultationUuid)
                    <input type="hidden" name="consultation_uuid" value="{{ $consultationUuid }}">
                @endif
                @if ($visitUuid)
                    <input type="hidden" name="visit_uuid" value="{{ $visitUuid }}">
                @endif
                @if (($returnConsultation ?? null)?->uuid)
                    <input type="hidden" name="from_consultation" value="{{ $returnConsultation->uuid }}">
                @endif

                <div>
                    <label class="block text-sm font-medium text-slate-700">Template</label>
                    <select name="template_code" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                        <option value="">Select…</option>
                        @foreach ($templates as $template)
                            <option value="{{ $template->code }}" @selected(old('template_code') === $template->code)>
                                {{ $template->name }}
                                ({{ $categories[$template->category] ?? $template->category }} · v{{ $template->version }})
                            </option>
                        @endforeach
                    </select>
                    @error('template_code')
                        <p class="mt-1 text-xs text-red-600">{{ $message }}</p>
                    @enderror
                </div>

                <div class="flex gap-3">
                    <button type="submit" class="btn-primary">Start</button>
                    <a href="{{ ($returnConsultation ?? null)
                        ? route('care.consultations.assessments.index', $returnConsultation)
                        : route('care.assessments.index', $patient) }}" class="rounded-lg border border-slate-200 px-4 py-2 text-sm text-slate-700 hover:bg-slate-50">Cancel</a>
                </div>
            </form>
        @endif
    </section>
</x-app-layout>
