@php
    $issue = $bloodBankIssue;
    $payload = $issue?->payload ?? [];
    $requestPayload = $bloodBankRequest?->payload ?? [];
    $canManage = $canManageClinical ?? $canConsult ?? false;
    $defaults = [
        'product' => $payload['product'] ?? ($requestPayload['product'] ?? ''),
        'units_issued' => $payload['units_issued'] ?? ($requestPayload['units'] ?? ''),
        'bag_numbers' => $payload['bag_numbers'] ?? '',
        'issued_to' => $payload['issued_to'] ?? '',
        'issued_at' => $payload['issued_at'] ?? now()->format('Y-m-d H:i'),
        'notes' => $payload['notes'] ?? '',
    ];
@endphp

<section class="rounded-2xl border border-slate-200 bg-white p-5">
    <div class="flex flex-wrap items-start justify-between gap-3">
        <div>
            <h3 class="text-sm font-semibold text-slate-900">Product issue</h3>
            <p class="mt-0.5 text-xs text-slate-500">Confirm units issued and optionally bill Blood Bank catalog services.</p>
        </div>
        @if ($issue)
            <span class="rounded-lg bg-emerald-50 px-2.5 py-1 text-xs font-semibold text-emerald-800">Issued recorded</span>
        @endif
    </div>

    @if ($canManage)
        <form method="POST" action="{{ route('care.specialty.blood-bank.issue', $workspaceVisit) }}" class="mt-4 space-y-4">
            @csrf
            <input type="hidden" name="tab" value="issue">

            <div class="grid gap-3 sm:grid-cols-2">
                <div>
                    <label class="block text-xs font-medium text-slate-600">Product</label>
                    <select name="payload[product]" required class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                        <option value="">Select…</option>
                        @foreach (['Whole blood', 'Packed RBC', 'Platelets', 'FFP', 'Cryoprecipitate'] as $option)
                            <option value="{{ $option }}" @selected(old('payload.product', $defaults['product']) === $option)>{{ $option }}</option>
                        @endforeach
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-slate-600">Units issued</label>
                    <input type="number" name="payload[units_issued]" min="1" required value="{{ old('payload.units_issued', $defaults['units_issued']) }}" 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">Bag / unit numbers</label>
                    <input type="text" name="payload[bag_numbers]" value="{{ old('payload.bag_numbers', $defaults['bag_numbers']) }}" 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">Issued to (ward / clinician)</label>
                    <input type="text" name="payload[issued_to]" value="{{ old('payload.issued_to', $defaults['issued_to']) }}" 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">Issued at</label>
                    <input type="text" name="payload[issued_at]" value="{{ old('payload.issued_at', $defaults['issued_at']) }}" class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                </div>
            </div>

            <div>
                <label class="block text-xs font-medium text-slate-600">Notes</label>
                <textarea name="payload[notes]" rows="2" class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">{{ old('payload.notes', $defaults['notes']) }}</textarea>
            </div>

            <div class="flex flex-wrap gap-4 text-sm">
                <label class="inline-flex items-center gap-2">
                    <input type="checkbox" name="bill_product" value="1" checked class="rounded border-slate-300">
                    Bill product units (bb.*)
                </label>
                <label class="inline-flex items-center gap-2">
                    <input type="checkbox" name="bill_crossmatch" value="1" class="rounded border-slate-300">
                    Bill cross-match
                </label>
            </div>

            <button type="submit" class="btn-primary">Confirm issue</button>
        </form>
    @elseif ($issue)
        <dl class="mt-4 grid gap-3 text-sm sm:grid-cols-2">
            <div>
                <dt class="text-slate-500">Product</dt>
                <dd class="font-medium text-slate-900">{{ $payload['product'] ?? '—' }}</dd>
            </div>
            <div>
                <dt class="text-slate-500">Units issued</dt>
                <dd class="font-medium text-slate-900">{{ $payload['units_issued'] ?? '—' }}</dd>
            </div>
            <div>
                <dt class="text-slate-500">Bag numbers</dt>
                <dd class="font-medium text-slate-900">{{ $payload['bag_numbers'] ?? '—' }}</dd>
            </div>
            <div>
                <dt class="text-slate-500">Issued to</dt>
                <dd class="font-medium text-slate-900">{{ $payload['issued_to'] ?? '—' }}</dd>
            </div>
        </dl>
    @else
        <p class="mt-3 text-sm text-slate-500">No issue recorded yet.</p>
    @endif
</section>
