@php
    $record = $entProcedure;
    $payload = $record?->payload ?? [];
    $canManage = $canManageClinical ?? $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">Procedure</h3>
            <p class="mt-0.5 text-xs text-slate-500">Completed procedures can close the ENT visit.</p>
        </div>
        <a href="{{ route('care.specialty.ent.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.ent.procedure', $workspaceVisit) }}" class="mt-4 space-y-4">
            @csrf
            <input type="hidden" name="tab" value="procedure">
            <div class="grid gap-3 sm:grid-cols-2">
                @foreach ($clinicalFields ?? [] as $field)
                    @php
                        $name = $field['name'];
                        $value = old('payload.'.$name, $payload[$name] ?? '');
                        $type = $field['type'] ?? 'text';
                    @endphp
                    <div @class(['sm:col-span-2' => in_array($type, ['textarea'], true)])>
                        <label class="block text-xs font-medium text-slate-600">
                            {{ $field['label'] }}
                            @if (! empty($field['required'])) <span class="text-rose-600">*</span> @endif
                        </label>
                        @if ($type === 'textarea')
                            <textarea name="payload[{{ $name }}]" rows="{{ $field['rows'] ?? 3 }}" @required(! empty($field['required']))
                                class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">{{ $value }}</textarea>
                        @elseif ($type === 'select')
                            <select name="payload[{{ $name }}]" @required(! empty($field['required']))
                                class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                                <option value="">Select…</option>
                                @foreach ($field['options'] ?? [] as $option)
                                    <option value="{{ $option }}" @selected((string) $value === (string) $option)>{{ $option }}</option>
                                @endforeach
                            </select>
                        @elseif ($type === 'boolean')
                            <label class="mt-2 inline-flex items-center gap-2 text-sm text-slate-700">
                                <input type="checkbox" name="payload[{{ $name }}]" value="1" @checked(old('payload.'.$name, ! empty($payload[$name])))>
                                Yes
                            </label>
                        @else
                            <input type="{{ $type === 'number' ? 'number' : 'text' }}" name="payload[{{ $name }}]" value="{{ $value }}"
                                @required(! empty($field['required']))
                                class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                        @endif
                    </div>
                @endforeach
            </div>
            <button type="submit" class="btn-primary">Save procedure</button>
        </form>
    @elseif ($record)
        <dl class="mt-4 grid gap-3 text-sm sm:grid-cols-2">
            @foreach ($clinicalFields ?? [] as $field)
                <div>
                    <dt class="text-slate-500">{{ $field['label'] }}</dt>
                    <dd class="font-medium text-slate-900">{{ $payload[$field['name']] ?? '—' }}</dd>
                </div>
            @endforeach
        </dl>
    @else
        <p class="mt-4 text-sm text-slate-500">No procedure recorded yet.</p>
    @endif
</section>
