@php
    $record = $emergencyDisposition;
    $payload = old('payload', $record?->payload ?? []);
    $canManage = $canConsult ?? false;
@endphp

<section class="rounded-2xl border border-slate-200 bg-white p-5">
    <div class="flex flex-wrap items-start justify-between gap-3">
        <div>
            <h3 class="text-sm font-semibold text-slate-900">Disposition</h3>
            <p class="mt-0.5 text-xs text-slate-500">Final disposition closes the emergency visit episode.</p>
        </div>
        <a href="{{ route('care.specialty.emergency.print', $workspaceVisit) }}" target="_blank" class="text-sm font-medium text-indigo-600">Print summary</a>
    </div>

    @if ($canManage)
        <form method="POST" action="{{ route('care.specialty.emergency.disposition', $workspaceVisit) }}" class="mt-4 space-y-3">
            @csrf
            <input type="hidden" name="tab" value="disposition">

            <div class="grid gap-3 sm:grid-cols-2">
                <div>
                    <label class="block text-xs font-medium text-slate-600">Final disposition</label>
                    <select name="payload[disposition]" required class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                        <option value="">Select…</option>
                        @foreach (['Discharge home', 'Admit to ward', 'Admit to ICU', 'Transfer to facility', 'Left against advice', 'Died', 'Absconded'] as $opt)
                            <option value="{{ $opt }}" @selected(($payload['disposition'] ?? '') === $opt)>{{ $opt }}</option>
                        @endforeach
                    </select>
                    @error('payload.disposition')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="block text-xs font-medium text-slate-600">Condition</label>
                    <select name="payload[condition]" class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                        <option value="">Select…</option>
                        @foreach (['Stable', 'Improving', 'Unstable', 'Critical', 'Deceased'] as $opt)
                            <option value="{{ $opt }}" @selected(($payload['condition'] ?? '') === $opt)>{{ $opt }}</option>
                        @endforeach
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-slate-600">Destination</label>
                    <input type="text" name="payload[destination]" value="{{ $payload['destination'] ?? '' }}" class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                </div>
                <div>
                    <label class="block text-xs font-medium text-slate-600">Disposition time</label>
                    <input type="text" name="payload[time]" value="{{ $payload['time'] ?? '' }}" placeholder="e.g. 14:30" class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                </div>
                <div class="sm:col-span-2">
                    <label class="block text-xs font-medium text-slate-600">Diagnosis</label>
                    <input type="text" name="payload[diagnosis]" value="{{ $payload['diagnosis'] ?? '' }}" required class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                    @error('payload.diagnosis')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                </div>
                <div class="sm:col-span-2">
                    <label class="block text-xs font-medium text-slate-600">Advice / follow-up</label>
                    <textarea name="payload[advice]" rows="3" class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">{{ $payload['advice'] ?? '' }}</textarea>
                </div>
                <div class="sm:col-span-2">
                    <label class="block text-xs font-medium text-slate-600">Medications on discharge</label>
                    <textarea name="payload[medications]" rows="2" class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">{{ $payload['medications'] ?? '' }}</textarea>
                </div>
                <div class="sm:col-span-2">
                    <label class="block text-xs font-medium text-slate-600">Notes</label>
                    <textarea name="payload[notes]" rows="2" class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">{{ $payload['notes'] ?? '' }}</textarea>
                </div>
            </div>

            <button type="submit" class="btn-primary">Save disposition &amp; complete visit</button>
        </form>
    @else
        <dl class="mt-4 grid gap-3 text-sm sm:grid-cols-2">
            <div>
                <dt class="text-slate-500">Final disposition</dt>
                <dd class="mt-0.5 font-medium text-slate-900">{{ $payload['disposition'] ?? '—' }}</dd>
            </div>
            <div>
                <dt class="text-slate-500">Condition</dt>
                <dd class="mt-0.5 font-medium text-slate-900">{{ $payload['condition'] ?? '—' }}</dd>
            </div>
            <div>
                <dt class="text-slate-500">Destination</dt>
                <dd class="mt-0.5 font-medium text-slate-900">{{ $payload['destination'] ?? '—' }}</dd>
            </div>
            <div>
                <dt class="text-slate-500">Disposition time</dt>
                <dd class="mt-0.5 font-medium text-slate-900">{{ $payload['time'] ?? '—' }}</dd>
            </div>
            <div class="sm:col-span-2">
                <dt class="text-slate-500">Diagnosis</dt>
                <dd class="mt-0.5 font-medium text-slate-900">{{ $payload['diagnosis'] ?? '—' }}</dd>
            </div>
            <div class="sm:col-span-2">
                <dt class="text-slate-500">Advice / follow-up</dt>
                <dd class="mt-0.5 font-medium text-slate-900 whitespace-pre-wrap">{{ $payload['advice'] ?? '—' }}</dd>
            </div>
            <div class="sm:col-span-2">
                <dt class="text-slate-500">Medications on discharge</dt>
                <dd class="mt-0.5 font-medium text-slate-900 whitespace-pre-wrap">{{ $payload['medications'] ?? '—' }}</dd>
            </div>
            <div class="sm:col-span-2">
                <dt class="text-slate-500">Notes</dt>
                <dd class="mt-0.5 font-medium text-slate-900 whitespace-pre-wrap">{{ $payload['notes'] ?? '—' }}</dd>
            </div>
        </dl>
    @endif
</section>
