@php
    $exam = $dentalPerioExam;
    $siteMap = [];
    foreach ($exam?->sites ?? [] as $site) {
        $siteMap[$site->tooth_code][$site->surface] = $site;
    }
    $teeth = array_merge($dentalRows['upper'] ?? [], $dentalRows['lower'] ?? []);
    $probeTeeth = array_slice($teeth, 0, 8);
    $canManage = $canConsult ?? false;
@endphp

<section class="rounded-2xl border border-slate-200 bg-white p-5">
    <h3 class="text-sm font-semibold text-slate-900">Periodontal exam</h3>
    <p class="mt-0.5 text-xs text-slate-500">Record probing depths, bleeding, and plaque by tooth surface.</p>

    @if ($exam)
        <p class="mt-3 text-xs text-slate-500">Latest exam {{ $exam->exam_date?->format('d M Y') }} · {{ $exam->sites->count() }} sites</p>
    @endif

    @if ($canManage)
        <form method="POST" action="{{ route('care.specialty.dentistry.perio', $workspaceVisit) }}" class="mt-4 space-y-4">
            @csrf
            <div class="overflow-x-auto">
                <table class="min-w-full text-left text-xs">
                    <thead>
                        <tr class="border-b border-slate-200 text-slate-500">
                            <th class="py-2 pr-2">Tooth</th>
                            @foreach ($dentalPerioSurfaces ?? [] as $surface)
                                <th class="px-1 py-2">{{ $surface }} mm</th>
                                <th class="px-1 py-2">B</th>
                                <th class="px-1 py-2">P</th>
                            @endforeach
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($probeTeeth as $idx => $tooth)
                            <tr class="border-b border-slate-100">
                                <td class="py-2 pr-2 font-semibold tabular-nums">{{ $tooth }}</td>
                                @foreach ($dentalPerioSurfaces ?? [] as $sIdx => $surface)
                                    @php
                                        $existing = $siteMap[$tooth][$surface] ?? null;
                                    @endphp
                                    <input type="hidden" name="sites[{{ $idx }}_{{ $sIdx }}][tooth_code]" value="{{ $tooth }}">
                                    <input type="hidden" name="sites[{{ $idx }}_{{ $sIdx }}][surface]" value="{{ $surface }}">
                                    <td class="px-1 py-1">
                                        <input type="number" min="0" max="15" name="sites[{{ $idx }}_{{ $sIdx }}][probing_mm]"
                                               value="{{ $existing?->probing_mm }}"
                                               class="w-12 rounded border border-slate-200 px-1 py-1 tabular-nums">
                                    </td>
                                    <td class="px-1 py-1">
                                        <input type="checkbox" name="sites[{{ $idx }}_{{ $sIdx }}][bleeding]" value="1" class="rounded border-slate-300"
                                               @checked($existing?->bleeding)>
                                    </td>
                                    <td class="px-1 py-1">
                                        <input type="checkbox" name="sites[{{ $idx }}_{{ $sIdx }}][plaque]" value="1" class="rounded border-slate-300"
                                               @checked($existing?->plaque)>
                                    </td>
                                @endforeach
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            <div>
                <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">{{ $exam?->notes }}</textarea>
            </div>
            <button type="submit" class="btn-primary">Save perio exam</button>
        </form>
    @else
        <div class="mt-4 overflow-x-auto">
            <table class="min-w-full text-left text-xs">
                <thead>
                    <tr class="border-b border-slate-200 text-slate-500">
                        <th class="py-2 pr-2">Tooth</th>
                        @foreach ($dentalPerioSurfaces ?? [] as $surface)
                            <th class="px-1 py-2">{{ $surface }} mm</th>
                            <th class="px-1 py-2">B</th>
                            <th class="px-1 py-2">P</th>
                        @endforeach
                    </tr>
                </thead>
                <tbody>
                    @forelse ($probeTeeth as $tooth)
                        <tr class="border-b border-slate-100">
                            <td class="py-2 pr-2 font-semibold tabular-nums">{{ $tooth }}</td>
                            @foreach ($dentalPerioSurfaces ?? [] as $surface)
                                @php $existing = $siteMap[$tooth][$surface] ?? null; @endphp
                                <td class="px-1 py-1 tabular-nums">{{ $existing?->probing_mm ?? '—' }}</td>
                                <td class="px-1 py-1">{{ $existing?->bleeding ? '✓' : '—' }}</td>
                                <td class="px-1 py-1">{{ $existing?->plaque ? '✓' : '—' }}</td>
                            @endforeach
                        </tr>
                    @empty
                        <tr>
                            <td colspan="{{ 1 + count($dentalPerioSurfaces ?? []) * 3 }}" class="py-3 text-slate-500">No perio exam yet.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if ($exam?->notes)
            <p class="mt-3 text-sm text-slate-700 whitespace-pre-wrap">{{ $exam->notes }}</p>
        @endif
    @endif
</section>
