<x-app-layout title="Lab admin">
    <div class="space-y-6">
        <x-care.page-hero
            badge="Laboratory · Catalog · Queue ops"
            title="Lab admin"
            description="Manage investigation catalog pricing, review open queue load, and jump into laboratory operations."
            :stats="[
                ['value' => number_format($activeCatalogCount), 'label' => 'Active tests'],
                ['value' => number_format($catalogCount), 'label' => 'Catalog total'],
                ['value' => number_format($openQueue), 'label' => 'Open queue'],
                ['value' => number_format($queueCounts[\App\Models\InvestigationRequest::STATUS_AWAITING_REVIEW] ?? 0), 'label' => 'Awaiting review'],
            ]">
            <x-slot name="actions">
                <a href="{{ route('care.lab.catalog.index') }}" class="btn-primary">Manage catalog</a>
                <a href="{{ route('care.lab.queue.index') }}" class="rounded-lg border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Lab queue</a>
                <a href="{{ route('care.lab.catalog.create') }}" class="rounded-lg border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Add test</a>
            </x-slot>
        </x-care.page-hero>

        @if (($canSwitchBranch ?? false) && isset($branches) && $branches->count() > 1)
            <form method="GET" class="rounded-2xl border border-slate-200 bg-white p-4">
                <label class="block text-xs font-medium text-slate-600">Branch</label>
                <select name="branch_id" class="mt-1 rounded-lg border-slate-300 text-sm" onchange="this.form.submit()">
                    @foreach ($branches as $branch)
                        <option value="{{ $branch->id }}" @selected($branchId == $branch->id)>{{ $branch->name }}</option>
                    @endforeach
                </select>
            </form>
        @elseif (isset($branches) && $branches->isNotEmpty())
            <p class="text-sm text-slate-600">Branch: <span class="font-medium text-slate-900">{{ $branches->firstWhere('id', $branchId)?->name ?? $branches->first()->name }}</span></p>
        @endif

        <div class="grid gap-4 lg:grid-cols-2">
            <section class="rounded-2xl border border-slate-200 bg-white p-5">
                <h2 class="text-sm font-semibold text-slate-900">Queue by status</h2>
                <ul class="mt-3 space-y-2 text-sm">
                    @foreach ([
                        \App\Models\InvestigationRequest::STATUS_PENDING,
                        \App\Models\InvestigationRequest::STATUS_SAMPLE_COLLECTED,
                        \App\Models\InvestigationRequest::STATUS_IN_PROGRESS,
                        \App\Models\InvestigationRequest::STATUS_AWAITING_REVIEW,
                    ] as $status)
                        <li class="flex justify-between gap-3">
                            <span>{{ $statuses[$status] ?? $status }}</span>
                            <span class="tabular-nums text-slate-600">{{ number_format($queueCounts[$status] ?? 0) }}</span>
                        </li>
                    @endforeach
                </ul>
                <div class="mt-4">
                    <a href="{{ route('care.lab.queue.index') }}" class="text-sm font-medium text-indigo-600">Open work queue →</a>
                </div>
            </section>

            <section class="rounded-2xl border border-slate-200 bg-white p-5">
                <h2 class="text-sm font-semibold text-slate-900">Catalog management</h2>
                <p class="mt-1 text-sm text-slate-500">Add or edit investigation types and prices. Technicians process samples from the queue; managers own the catalog.</p>
                <div class="mt-4 flex flex-wrap gap-2">
                    <a href="{{ route('care.lab.catalog.index') }}" class="rounded-lg bg-indigo-600 px-3 py-2 text-sm font-semibold text-white">Investigation catalog</a>
                    <a href="{{ route('care.lab.catalog.create') }}" class="rounded-lg border border-slate-200 px-3 py-2 text-sm font-medium text-slate-700">Add test</a>
                    <a href="{{ route('care.lab.requests.index') }}" class="rounded-lg border border-slate-200 px-3 py-2 text-sm font-medium text-slate-700">All requests</a>
                </div>
            </section>
        </div>

        <section class="rounded-2xl border border-slate-200 bg-white p-5">
            <h2 class="text-sm font-semibold text-slate-900">Current queue snapshot</h2>
            <div class="mt-3 overflow-hidden rounded-xl border border-slate-100">
                <table class="min-w-full text-sm">
                    <thead class="bg-slate-50 text-left text-xs uppercase text-slate-500">
                        <tr>
                            <th class="px-4 py-3">Patient</th>
                            <th class="px-4 py-3">Test</th>
                            <th class="px-4 py-3">Status</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-50">
                        @forelse ($recentQueue as $item)
                            <tr>
                                <td class="px-4 py-3 font-medium">{{ $item->patient?->fullName() ?? '—' }}</td>
                                <td class="px-4 py-3">{{ $item->investigationType?->name ?? '—' }}</td>
                                <td class="px-4 py-3">{{ $statuses[$item->status] ?? $item->status }}</td>
                                <td class="px-4 py-3 text-right">
                                    <a href="{{ route('care.lab.requests.show', $item) }}" class="text-sky-600">Open</a>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="px-4 py-8 text-center text-slate-500">No open lab work for this branch.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </section>
    </div>
</x-app-layout>
