<x-app-layout title="Blood Bank admin">
    <div class="space-y-6">
        <x-care.page-hero
            badge="Blood Bank · Operations · Inventory"
            title="Blood Bank admin"
            description="Maintain operational product stock, low-stock thresholds, and jump into clinical Blood Bank work."
            :stats="[
                ['value' => number_format($overview['total_units']), 'label' => 'Units on hand'],
                ['value' => number_format($overview['low_stock_count']), 'label' => 'Low stock'],
                ['value' => number_format($report['requests_today'] ?? 0), 'label' => 'Requests today'],
                ['value' => number_format($report['high_urgency_open'] ?? 0), 'label' => 'High urgency open'],
            ]">
            <x-slot name="actions">
                <a href="{{ route('care.specialty.show', 'blood_bank') }}" class="rounded-lg border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Clinical list</a>
                <a href="{{ route('care.specialty.blood-bank.reports') }}" class="rounded-lg border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Reports</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

        @if ($overview['low_stock']->isNotEmpty())
            <div class="rounded-xl border border-amber-200 bg-amber-50 p-4 text-sm text-amber-900">
                {{ $overview['low_stock']->count() }} product(s) at or below reorder level.
            </div>
        @endif

        <section class="rounded-2xl border border-slate-200 bg-white p-5">
            <div class="flex flex-wrap items-center justify-between gap-3">
                <div>
                    <h2 class="text-sm font-semibold text-slate-900">Operational inventory</h2>
                    <p class="mt-0.5 text-xs text-slate-500">Branch-level units on hand — separate from visit-scoped clinical inventory notes.</p>
                </div>
            </div>

            <form method="POST" action="{{ route('care.blood-bank.admin.stock.update') }}" class="mt-4 space-y-3">
                @csrf
                @if ($branchId > 0)
                    <input type="hidden" name="branch_id" value="{{ $branchId }}">
                @endif

                <div class="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">Product</th>
                                <th class="px-4 py-3">Units on hand</th>
                                <th class="px-4 py-3">Reorder level</th>
                                <th class="px-4 py-3">Notes</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-slate-50">
                            @foreach ($overview['stock'] as $i => $row)
                                <tr @class(['bg-amber-50/60' => $row->isLowStock()])>
                                    <td class="px-4 py-3 font-medium text-slate-900">
                                        {{ $row->product_label }}
                                        @if ($row->isLowStock())
                                            <span class="ml-2 text-xs font-semibold text-amber-800">Low</span>
                                        @endif
                                        <input type="hidden" name="stock[{{ $i }}][product_code]" value="{{ $row->product_code }}">
                                    </td>
                                    <td class="px-4 py-3">
                                        <input type="number" min="0" name="stock[{{ $i }}][units_on_hand]" value="{{ old('stock.'.$i.'.units_on_hand', $row->units_on_hand) }}" class="w-24 rounded-lg border-slate-300 text-sm">
                                    </td>
                                    <td class="px-4 py-3">
                                        <input type="number" min="0" name="stock[{{ $i }}][reorder_level]" value="{{ old('stock.'.$i.'.reorder_level', $row->reorder_level) }}" class="w-24 rounded-lg border-slate-300 text-sm">
                                    </td>
                                    <td class="px-4 py-3">
                                        <input type="text" name="stock[{{ $i }}][notes]" value="{{ old('stock.'.$i.'.notes', $row->notes) }}" class="w-full max-w-xs rounded-lg border-slate-300 text-sm" placeholder="Optional">
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>

                <div class="flex justify-end">
                    <button type="submit" class="btn-primary">Save inventory</button>
                </div>
            </form>
        </section>

        <section class="rounded-2xl border border-slate-200 bg-white p-5">
            <h2 class="text-sm font-semibold text-slate-900">Recent visit inventory notes</h2>
            <p class="mt-0.5 text-xs text-slate-500">Clinical notes from Blood Bank visits (not the operational register above).</p>
            <ul class="mt-3 divide-y divide-slate-50 text-sm">
                @forelse ($overview['recent_notes'] as $note)
                    <li class="flex flex-wrap items-center justify-between gap-2 py-3">
                        <div>
                            <p class="font-medium text-slate-900">
                                {{ $note->visit?->patient?->fullName() ?? 'Visit #'.$note->visit_id }}
                            </p>
                            <p class="text-xs text-slate-500">
                                {{ $note->recorded_at?->format('d M Y H:i') ?? '—' }}
                                @if (! empty($note->payload['low_stock_alert']))
                                    · <span class="text-amber-800">Low stock flagged</span>
                                @endif
                            </p>
                        </div>
                        @if ($note->visit_id)
                            <a href="{{ route('care.specialty.workspace', ['module' => 'blood_bank', 'visit' => $note->visit_id, 'tab' => 'inventory']) }}" class="text-sky-600">Open</a>
                        @endif
                    </li>
                @empty
                    <li class="py-6 text-center text-slate-500">No recent inventory notes.</li>
                @endforelse
            </ul>
        </section>
    </div>
</x-app-layout>
