<x-app-layout title="Lab queue">
    <div class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
        <div>
            <h1 class="text-xl font-semibold text-slate-900">Lab work queue</h1>
            <p class="mt-1 text-sm text-slate-500">Pending samples and investigations in progress</p>
        </div>
        <div class="flex gap-2">
            <a href="{{ route('care.lab.requests.index') }}" class="rounded-lg border border-slate-200 px-4 py-2 text-sm">All requests</a>
            @if (app(\App\Services\Care\CarePermissions::class)->can(auth()->user() ? app(\App\Services\Care\OrganizationResolver::class)->memberFor(auth()->user(), $organization) : null, 'lab.catalog.manage'))
                <a href="{{ route('care.lab.admin.index') }}" class="rounded-lg border border-slate-200 px-4 py-2 text-sm">Lab admin</a>
                <a href="{{ route('care.lab.catalog.index') }}" class="rounded-lg border border-slate-200 px-4 py-2 text-sm">Catalog</a>
            @endif
        </div>
    </div>

    <form method="GET" class="mt-4 flex flex-wrap gap-3 rounded-2xl border border-slate-200 bg-white p-4">
        @if (($canSwitchBranch ?? false) && isset($branches) && $branches->count() > 1)
            <select name="branch_id" class="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>
        @elseif (isset($branches) && $branches->isNotEmpty())
            <input type="hidden" name="branch_id" value="{{ $branchId }}">
            <span class="flex items-center text-sm font-medium text-slate-700">{{ $branches->first()->name }}</span>
        @endif
        <select name="status" class="rounded-lg border-slate-300 text-sm">
            <option value="">All active</option>
            @foreach ($statuses as $value => $label)
                @if (in_array($value, \App\Models\InvestigationRequest::activeStatuses()))
                    <option value="{{ $value }}" @selected($status === $value)>{{ $label }}</option>
                @endif
            @endforeach
        </select>
        <button type="submit" class="btn-primary">Filter</button>
    </form>

    <div class="mt-4">
    @include('care.partials.queue-ops', [
        'queueIntegration' => $queueIntegration ?? null,
        'queueCallNextRoute' => 'care.lab.queue.call-next',
        'queueCallNextParams' => [],
        'branchId' => $branchId,
        'variant' => 'bar',
    ])
    </div>

    <div class="mt-4 space-y-3">
        @forelse ($queue as $item)
            <div class="flex items-start justify-between gap-4 rounded-2xl border border-slate-200 bg-white p-4">
                <div class="min-w-0 flex-1 space-y-2">
                    @if (! empty($queueIntegration['enabled']) && $item->queue_ticket_number)
                        @include('care.partials.queue-ticket', [
                            'ticketNumber' => $item->queue_ticket_number,
                            'ticketStatus' => $item->queue_ticket_status,
                        ])
                    @endif
                    <div class="min-w-0">
                        <p class="truncate text-base font-semibold text-slate-900">{{ $item->patient->fullName() }}</p>
                        <p class="mt-0.5 truncate text-sm text-slate-700">{{ $item->investigationType->name }}</p>
                        <p class="mt-0.5 truncate text-xs text-slate-500">{{ $statuses[$item->status] ?? $item->status }} · {{ $item->priority }} · {{ $item->created_at->diffForHumans() }}</p>
                    </div>
                </div>
                <div class="flex shrink-0 items-center gap-2">
                    @if (! empty($queueIntegration['enabled']) && $item->queue_ticket_uuid && ($item->queue_ticket_status ?? '') !== 'serving')
                        <form method="POST" action="{{ route('care.lab.queue.serve', $item) }}">
                            @csrf
                            <button type="submit" class="rounded-lg bg-sky-600 px-3 py-1.5 text-xs font-medium text-white hover:bg-sky-700">Serve</button>
                        </form>
                    @endif
                    <a href="{{ route('care.lab.requests.show', $item) }}" class="text-sm font-medium text-sky-600">Open</a>
                </div>
            </div>
        @empty
            <p class="rounded-2xl border border-dashed border-slate-200 bg-white p-8 text-center text-sm text-slate-500">Queue is empty.</p>
        @endforelse
    </div>
</x-app-layout>
