@php
    $values = old('payload', $clinicalRecord?->payload ?? []);
@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">{{ $workspaceTabs[$activeTab] ?? 'Clinical' }}</h3>
            <p class="mt-0.5 text-xs text-slate-500">Structured specialty record on this visit episode.</p>
        </div>
        @if ($clinicalRecord)
            <span class="rounded-full bg-slate-100 px-2 py-0.5 text-[10px] font-semibold uppercase tracking-wide text-slate-600">
                {{ $clinicalRecord->status }}
            </span>
        @endif
    </div>

    @if (! empty($clinicalAlerts))
        <div class="mt-3 space-y-1.5">
            @foreach ($clinicalAlerts as $alert)
                <div @class([
                    'rounded-xl px-3 py-2 text-xs font-medium',
                    'bg-rose-50 text-rose-800 border border-rose-100' => ($alert['severity'] ?? '') === 'critical',
                    'bg-amber-50 text-amber-900 border border-amber-100' => ($alert['severity'] ?? '') !== 'critical',
                ])>
                    {{ $alert['message'] ?? '' }}
                </div>
            @endforeach
        </div>
    @endif

    @if (! ($canManageClinical ?? $canManageSpecialty ?? true))
        <dl class="mt-4 grid gap-3 text-sm sm:grid-cols-2">
            @forelse ($clinicalFields as $field)
                @php
                    $name = $field['name'];
                    $value = $values[$name] ?? null;
                    if (is_bool($value)) {
                        $value = $value ? 'Yes' : 'No';
                    }
                @endphp
                <div @class(['sm:col-span-2' => ($field['type'] ?? '') === 'textarea'])>
                    <dt class="text-slate-500">{{ $field['label'] }}</dt>
                    <dd class="mt-0.5 font-medium text-slate-900 whitespace-pre-wrap">{{ $value !== null && $value !== '' ? $value : '—' }}</dd>
                </div>
            @empty
                <p class="text-slate-500">No clinical record yet.</p>
            @endforelse
        </dl>
    @else
    <form method="POST" action="{{ route('care.specialty.clinical.save', ['module' => $moduleKey, 'visit' => $workspaceVisit]) }}" class="mt-4 space-y-3">
        @csrf
        <input type="hidden" name="tab" value="{{ $activeTab }}">
        <input type="hidden" name="status" value="active">

        @if (! empty($stages))
            <div>
                <label class="block text-xs font-medium text-slate-600">Current stage</label>
                <select name="stage_code" class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                    <option value="">—</option>
                    @foreach ($stages as $stage)
                        <option value="{{ $stage['code'] }}" @selected(old('stage_code', $clinicalRecord?->stage_code) === ($stage['code'] ?? ''))>
                            {{ $stage['label'] ?? $stage['code'] }}
                        </option>
                    @endforeach
                </select>
            </div>
        @endif

        @foreach ($clinicalFields as $field)
            @php
                $name = $field['name'];
                $value = $values[$name] ?? '';
                $id = 'field_'.$name;
            @endphp
            <div>
                <label for="{{ $id }}" class="block text-xs font-medium text-slate-600">
                    {{ $field['label'] }}
                    @if (! empty($field['required'])) <span class="text-rose-500">*</span> @endif
                </label>

                @if (($field['type'] ?? '') === 'textarea')
                    <textarea id="{{ $id }}" name="payload[{{ $name }}]" rows="{{ $field['rows'] ?? 3 }}"
                              @if (! empty($field['required'])) required @endif
                              class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">{{ $value }}</textarea>
                @elseif (($field['type'] ?? '') === 'select')
                    <select id="{{ $id }}" name="payload[{{ $name }}]"
                            @if (! empty($field['required'])) required @endif
                            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 (($field['type'] ?? '') === 'boolean')
                    <label class="mt-2 flex items-center gap-2 text-sm text-slate-700">
                        <input type="hidden" name="payload[{{ $name }}]" value="0">
                        <input id="{{ $id }}" type="checkbox" name="payload[{{ $name }}]" value="1" @checked(filter_var($value, FILTER_VALIDATE_BOOLEAN))
                               class="rounded border-slate-300 text-indigo-600 focus:ring-indigo-500">
                        Yes
                    </label>
                @elseif (($field['type'] ?? '') === 'number')
                    <input id="{{ $id }}" type="number" step="any" name="payload[{{ $name }}]" value="{{ $value }}"
                           @if (! empty($field['required'])) required @endif
                           class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                @else
                    <input id="{{ $id }}" type="text" name="payload[{{ $name }}]" value="{{ $value }}"
                           @if (! empty($field['required'])) required @endif
                           class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                @endif

                @error('payload.'.$name)
                    <p class="mt-1 text-xs text-rose-600">{{ $message }}</p>
                @enderror
            </div>
        @endforeach

        <div class="flex flex-wrap gap-2 pt-2">
            <button type="submit" class="btn-primary">Save record</button>
            @if ($workspaceVisit->appointment)
                <a href="{{ route('care.appointments.show', $workspaceVisit->appointment) }}" class="rounded-xl border border-slate-200 px-3 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Appointment</a>
            @endif
        </div>
    </form>
    @endif
</section>
