<x-app-layout title="Pharmacy queue">
    <h1 class="text-xl font-semibold text-slate-900">Pharmacy queue</h1>
    <p class="mt-1 text-sm text-slate-500">Active prescriptions awaiting dispensing</p>

    @if (($canSwitchBranch ?? false) && isset($branches) && $branches->count() > 1)
        <form method="GET" class="mt-4 flex flex-wrap gap-3 rounded-2xl border border-slate-200 bg-white p-4">
            <select name="branch_id" class="rounded-lg border-slate-300 text-sm">
                @foreach ($branches as $branch)
                    <option value="{{ $branch->id }}" @selected((int) $branchId === (int) $branch->id)>{{ $branch->name }}</option>
                @endforeach
            </select>
            <button type="submit" class="btn-primary">Filter</button>
        </form>
    @elseif (isset($branches) && $branches->isNotEmpty())
        <p class="mt-4 text-sm text-slate-500">{{ $branches->first()->name }}</p>
    @endif

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

    <div class="mt-4 space-y-3">
        @forelse ($queue as $rx)
            <div class="rounded-2xl border border-slate-200 bg-white p-4">
                <div class="flex items-start justify-between gap-4">
                    <div class="min-w-0 flex-1 space-y-2">
                        @if (! empty($queueIntegration['enabled']) && $rx->queue_ticket_number)
                            @include('care.partials.queue-ticket', [
                                'ticketNumber' => $rx->queue_ticket_number,
                                'ticketStatus' => $rx->queue_ticket_status,
                            ])
                        @endif
                        <div class="min-w-0">
                            <p class="truncate text-base font-semibold text-slate-900">{{ $rx->patient->fullName() }}</p>
                            <p class="mt-0.5 truncate text-xs text-slate-500">{{ $rx->patient->patient_number }} · {{ $rx->visit->branch?->name }}</p>
                        </div>
                        <ul class="space-y-1 text-sm text-slate-700">
                            @foreach ($rx->items as $item)
                                <li>{{ $item->is_procedure ? 'Procedure' : 'Med' }}: {{ $item->name }} {{ $item->dosage }} {{ $item->frequency }}</li>
                            @endforeach
                        </ul>
                        @if ($canDispense)
                            <div class="mt-3 flex flex-wrap gap-2">
                                @if (! empty($queueIntegration['enabled']) && $rx->queue_ticket_uuid && ($rx->queue_ticket_status ?? '') !== 'serving')
                                    @php
                                        $pointUuid = (string) ($rx->queue_service_point_uuid ?? '');
                                        $mine = empty($memberCounterUuid)
                                            || $pointUuid === ''
                                            || $pointUuid === (string) $memberCounterUuid;
                                    @endphp
                                    @if ($mine)
                                        <form method="POST" action="{{ route('care.prescriptions.serve', $rx) }}">
                                            @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
                                @endif
                            </div>
                            <form method="POST" action="{{ route('care.prescriptions.dispense', $rx) }}" class="mt-4 border-t border-slate-100 pt-4">
                                @csrf
                                @php $allocIndex = 0; @endphp
                                @foreach ($rx->items as $item)
                                    @if (! $item->is_procedure)
                                        <div class="mt-2 grid gap-2 sm:grid-cols-[1fr_auto_auto] sm:items-end">
                                            <div>
                                                <label class="text-xs text-slate-500">Stock for {{ $item->name }}</label>
                                                <select name="allocations[{{ $allocIndex }}][drug_batch_id]" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                                                    <option value="">Skip stock deduction</option>
                                                    @foreach ($drugs as $drug)
                                                        @foreach ($drug->batches as $batch)
                                                            <option value="{{ $batch->id }}">{{ $drug->name }} · {{ $batch->batch_number }} ({{ $batch->quantity_on_hand }} {{ $drug->unit }})</option>
                                                        @endforeach
                                                    @endforeach
                                                </select>
                                            </div>
                                            <input type="hidden" name="allocations[{{ $allocIndex }}][prescription_item_id]" value="{{ $item->id }}">
                                            <div>
                                                <label class="text-xs text-slate-500">Qty</label>
                                                <input type="number" name="allocations[{{ $allocIndex }}][quantity]" value="1" min="1" class="mt-1 w-20 rounded-lg border-slate-300 text-sm">
                                            </div>
                                            @php $allocIndex++; @endphp
                                        </div>
                                    @endif
                                @endforeach
                                <div class="mt-3 flex gap-2">
                                    <button type="submit" class="rounded-lg bg-emerald-600 px-3 py-1.5 text-xs font-medium text-white hover:bg-emerald-700">
                                        {{ ! empty($queueIntegration['enabled']) ? 'Complete & dispense' : 'Dispense' }}
                                    </button>
                                    <a href="{{ route('care.prescriptions.show', $rx) }}" class="text-sm text-sky-600">View details</a>
                                </div>
                            </form>
                        @endif
                    </div>
                    @unless ($canDispense)
                        <a href="{{ route('care.prescriptions.show', $rx) }}" class="text-sm text-sky-600">View</a>
                    @endunless
                </div>
            </div>
        @empty
            <p class="rounded-2xl border border-dashed border-slate-200 bg-white p-8 text-center text-sm text-slate-500">No prescriptions in queue.</p>
        @endforelse
    </div>
</x-app-layout>
