@php
    $teethMap = $dentalTeethMap ?? [];
    $selected = old('tooth_code', request('tooth', ($dentalRows['upper'][0] ?? '16')));
    $selectedData = $teethMap[$selected] ?? ['status' => 'present', 'surfaces' => [], 'conditions' => [], 'notes' => null];
    $dentition = $dentalChart->dentition ?? 'adult';
    $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">Odontogram (FDI)</h3>
            <p class="mt-0.5 text-xs text-slate-500">
                {{ $canManage ? 'Click a tooth to chart status, surfaces, and conditions.' : 'View tooth chart status, surfaces, and conditions.' }}
            </p>
        </div>
        <div class="flex flex-wrap items-center gap-3">
            @if ($canManage)
                <form method="POST" action="{{ route('care.specialty.dentistry.dentition', $workspaceVisit) }}" class="flex items-center gap-2">
                    @csrf
                    <label class="text-xs font-medium text-slate-600">Dentition</label>
                    <select name="dentition" onchange="this.form.submit()" class="rounded-lg border border-slate-200 px-2 py-1.5 text-sm">
                        @foreach ($dentalDentitions ?? [] as $value => $label)
                            <option value="{{ $value }}" @selected($dentition === $value)>{{ $label }}</option>
                        @endforeach
                    </select>
                </form>
            @else
                <p class="text-xs text-slate-600">
                    Dentition:
                    <span class="font-medium text-slate-900">{{ ($dentalDentitions ?? [])[$dentition] ?? $dentition }}</span>
                </p>
            @endif
            <a href="{{ route('care.specialty.dentistry.print', $workspaceVisit) }}" target="_blank" class="text-sm font-medium text-indigo-600">Print</a>
        </div>
    </div>

    <div class="mt-4 space-y-2">
        @foreach (['upper' => 'Upper', 'lower' => 'Lower'] as $rowKey => $rowLabel)
            <div>
                <p class="mb-1 text-[10px] font-semibold uppercase tracking-wide text-slate-400">{{ $rowLabel }}</p>
                <div class="flex flex-wrap gap-1">
                    @foreach (($dentalRows[$rowKey] ?? []) as $code)
                        @php
                            $t = $teethMap[$code] ?? ['status' => 'present', 'surfaces' => [], 'conditions' => []];
                            $baseClass = \App\Services\Care\Dentistry\DentalCatalog::toothCssClass($t['status'] ?? 'present', $t['conditions'] ?? []);
                            $surfaces = $t['surfaces'] ?? [];
                        @endphp
                        <a
                            href="{{ route('care.specialty.workspace', ['module' => 'dentistry', 'visit' => $workspaceVisit, 'tab' => 'odontogram', 'tooth' => $code]) }}"
                            class="inline-flex flex-col items-center justify-center rounded-lg px-1 py-1 text-xs font-semibold tabular-nums transition {{ $code === $selected ? 'ring-2 ring-indigo-500' : '' }} {{ $baseClass }}"
                            title="Tooth {{ $code }}"
                        >
                            <span>{{ $code }}</span>
                            @if (! empty($surfaces))
                                <span class="mt-0.5 flex gap-0.5">
                                    @foreach (['M','O','D','B','L','I'] as $s)
                                        <span @class([
                                            'h-1.5 w-1.5 rounded-full',
                                            'bg-indigo-600' => in_array($s, $surfaces, true),
                                            'bg-slate-300/60' => ! in_array($s, $surfaces, true),
                                        ])></span>
                                    @endforeach
                                </span>
                            @endif
                        </a>
                    @endforeach
                </div>
            </div>
        @endforeach
    </div>

    @if ($canManage)
        <form method="POST" action="{{ route('care.specialty.dentistry.tooth', $workspaceVisit) }}" class="mt-6 grid gap-4 border-t border-slate-100 pt-4 sm:grid-cols-2">
            @csrf
            <input type="hidden" name="tooth_code" value="{{ $selected }}">
            <div>
                <label class="block text-xs font-medium text-slate-600">Selected tooth</label>
                <p class="mt-1 text-lg font-semibold tabular-nums text-slate-900">{{ $selected }}</p>
            </div>
            <div>
                <label class="block text-xs font-medium text-slate-600">Status</label>
                <select name="status" class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                    @foreach ($dentalStatuses as $value => $label)
                        <option value="{{ $value }}" @selected(($selectedData['status'] ?? 'present') === $value)>{{ $label }}</option>
                    @endforeach
                </select>
            </div>
            <div class="sm:col-span-2">
                <p class="text-xs font-medium text-slate-600">Surfaces</p>
                <div class="mt-2 flex flex-wrap gap-3">
                    @foreach ($dentalSurfaces as $surface)
                        <label class="inline-flex items-center gap-1.5 text-sm text-slate-700">
                            <input type="checkbox" name="surfaces[]" value="{{ $surface }}" class="rounded border-slate-300 text-indigo-600"
                                   @checked(in_array($surface, $selectedData['surfaces'] ?? [], true))>
                            {{ $surface }}
                        </label>
                    @endforeach
                </div>
            </div>
            <div class="sm:col-span-2">
                <p class="text-xs font-medium text-slate-600">Conditions</p>
                <div class="mt-2 flex flex-wrap gap-3">
                    @foreach ($dentalConditions as $value => $label)
                        <label class="inline-flex items-center gap-1.5 text-sm text-slate-700">
                            <input type="checkbox" name="conditions[]" value="{{ $value }}" class="rounded border-slate-300 text-indigo-600"
                                   @checked(in_array($value, $selectedData['conditions'] ?? [], true))>
                            {{ $label }}
                        </label>
                    @endforeach
                </div>
            </div>
            <div class="sm:col-span-2">
                <label class="block text-xs font-medium text-slate-600">Notes</label>
                <textarea name="notes" rows="2" class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">{{ $selectedData['notes'] ?? '' }}</textarea>
            </div>
            <div class="sm:col-span-2">
                <button type="submit" class="btn-primary">Save tooth</button>
            </div>
        </form>
    @else
        <dl class="mt-6 grid gap-3 border-t border-slate-100 pt-4 text-sm sm:grid-cols-2">
            <div>
                <dt class="text-slate-500">Selected tooth</dt>
                <dd class="mt-0.5 text-lg font-semibold tabular-nums text-slate-900">{{ $selected }}</dd>
            </div>
            <div>
                <dt class="text-slate-500">Status</dt>
                <dd class="mt-0.5 font-medium text-slate-900">{{ ($dentalStatuses ?? [])[$selectedData['status'] ?? 'present'] ?? ($selectedData['status'] ?? '—') }}</dd>
            </div>
            <div class="sm:col-span-2">
                <dt class="text-slate-500">Surfaces</dt>
                <dd class="mt-0.5 font-medium text-slate-900">{{ ! empty($selectedData['surfaces']) ? implode(', ', $selectedData['surfaces']) : '—' }}</dd>
            </div>
            <div class="sm:col-span-2">
                <dt class="text-slate-500">Conditions</dt>
                <dd class="mt-0.5 font-medium text-slate-900">
                    @php
                        $conditionLabels = collect($selectedData['conditions'] ?? [])
                            ->map(fn ($c) => ($dentalConditions ?? [])[$c] ?? $c)
                            ->filter()
                            ->all();
                    @endphp
                    {{ $conditionLabels !== [] ? implode(', ', $conditionLabels) : '—' }}
                </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">{{ $selectedData['notes'] ?: '—' }}</dd>
            </div>
        </dl>
    @endif
</section>
