@php $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">Lab cases</h3>
    <p class="mt-0.5 text-xs text-slate-500">Track crowns, bridges, dentures, and other lab work.</p>

    <ul class="mt-4 space-y-2 text-sm">
        @forelse ($dentalLabCases ?? [] as $case)
            <li class="rounded-xl border border-slate-100 bg-slate-50 px-3 py-3">
                <div class="flex flex-wrap items-start justify-between gap-2">
                    <div>
                        <p class="font-medium text-slate-800">
                            {{ $dentalLabCaseTypes[$case->case_type] ?? $case->case_type }}
                            @if ($case->lab_name) <span class="text-slate-500">· {{ $case->lab_name }}</span> @endif
                        </p>
                        <p class="text-xs text-slate-500">
                            {{ $dentalLabCaseStatuses[$case->status] ?? $case->status }}
                            @if (! empty($case->tooth_codes)) · {{ implode(', ', $case->tooth_codes) }} @endif
                            @if ($case->due_at) · due {{ $case->due_at->format('d M Y') }} @endif
                        </p>
                    </div>
                    @if ($canManage && $case->status !== 'cancelled' && $case->status !== 'fitted')
                        <form method="POST" action="{{ route('care.specialty.dentistry.lab-cases.status', [$workspaceVisit, $case]) }}" class="flex gap-1">
                            @csrf
                            <select name="status" class="rounded-lg border border-slate-200 px-2 py-1 text-xs">
                                @foreach ($dentalLabCaseStatuses as $value => $label)
                                    <option value="{{ $value }}" @selected($case->status === $value)>{{ $label }}</option>
                                @endforeach
                            </select>
                            <button type="submit" class="rounded-lg bg-white px-2 py-1 text-xs font-semibold text-indigo-700 ring-1 ring-indigo-200">Update</button>
                        </form>
                    @endif
                </div>
            </li>
        @empty
            <li class="text-slate-500">No lab cases yet.</li>
        @endforelse
    </ul>

    @if ($canManage)
        <form method="POST" action="{{ route('care.specialty.dentistry.lab-cases', $workspaceVisit) }}" class="mt-6 space-y-3 border-t border-slate-100 pt-4">
            @csrf
            <h4 class="text-sm font-semibold text-slate-900">New lab case</h4>
            <div class="grid gap-3 sm:grid-cols-2">
                <div>
                    <label class="block text-xs font-medium text-slate-600">Type</label>
                    <select name="case_type" required class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                        @foreach ($dentalLabCaseTypes ?? [] as $value => $label)
                            <option value="{{ $value }}">{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-slate-600">Lab name</label>
                    <input type="text" name="lab_name" 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">Due date</label>
                    <input type="date" name="due_at" 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">Status</label>
                    <select name="status" class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                        <option value="draft">Draft</option>
                        <option value="ordered">Ordered</option>
                    </select>
                </div>
                <div class="sm:col-span-2">
                    <p class="text-xs font-medium text-slate-600">Teeth</p>
                    <div class="mt-2 flex max-h-24 flex-wrap gap-2 overflow-y-auto">
                        @foreach (array_merge($dentalRows['upper'] ?? [], $dentalRows['lower'] ?? []) as $code)
                            <label class="inline-flex items-center gap-1 rounded border border-slate-200 px-1.5 py-0.5 text-xs">
                                <input type="checkbox" name="tooth_codes[]" value="{{ $code }}" class="rounded border-slate-300 text-indigo-600">
                                {{ $code }}
                            </label>
                        @endforeach
                    </div>
                </div>
                <div class="sm:col-span-2">
                    <label class="block text-xs font-medium text-slate-600">Notes</label>
                    <input type="text" name="notes" class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                </div>
            </div>
            <button type="submit" class="btn-primary">Create lab case</button>
        </form>
    @endif
</section>
