@php $money = fn ($minor) => config('care.billing.currency').' '.number_format($minor / 100, 2); @endphp

<x-app-layout title="Drug inventory">
    <div class="space-y-6">
        <x-care.page-hero
            badge="Pharmacy · Stock · Reorder alerts"
            title="Inventory"
            description="Track drug stock levels, unit pricing, and batches — with alerts when items run low or expire."
            :stats="[
                ['value' => number_format($heroStats['total']), 'label' => 'Drugs'],
                ['value' => number_format($heroStats['low_stock']), 'label' => 'Low stock'],
                ['value' => number_format($heroStats['expired']), 'label' => 'Expired batches'],
            ]">
            @if ($canManage)
                <x-slot name="actions">
                    <a href="{{ route('care.pharmacy.drugs.create') }}" class="btn-primary">Add drug</a>
                </x-slot>
            @endif
        </x-care.page-hero>

        @if ($lowStock->isNotEmpty())
            <div class="rounded-xl border border-amber-200 bg-amber-50 p-4 text-sm text-amber-900">{{ $lowStock->count() }} drug(s) below reorder level.</div>
        @endif
        @if ($expired->isNotEmpty())
            <div class="rounded-xl border border-red-200 bg-red-50 p-4 text-sm text-red-900">{{ $expired->count() }} expired batch(es) with stock on hand.</div>
        @endif

        <form method="GET" class="rounded-2xl border border-slate-200 bg-white p-4">
            <input type="search" name="q" value="{{ request('q') }}" placeholder="Search drugs…" class="w-full rounded-lg border-slate-300 text-sm sm:max-w-md">
        </form>

        <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white">
            <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">Drug</th><th class="px-4 py-3">Stock</th><th class="px-4 py-3">Unit price</th><th></th></tr></thead>
                <tbody class="divide-y divide-slate-50">
                    @forelse ($drugs as $drug)
                        <tr>
                            <td class="px-4 py-3"><p class="font-medium">{{ $drug->name }}</p>@if($drug->generic_name)<p class="text-xs text-slate-500">{{ $drug->generic_name }}</p>@endif</td>
                            <td class="px-4 py-3 {{ $drug->stockOnHand() <= $drug->reorder_level ? 'text-amber-700 font-medium' : '' }}">{{ $drug->stockOnHand() }} {{ $drug->unit }}</td>
                            <td class="px-4 py-3">{{ $money($drug->unit_price_minor) }}</td>
                            <td class="px-4 py-3 text-right"><a href="{{ route('care.pharmacy.drugs.show', $drug) }}" class="text-sky-600">View</a></td>
                        </tr>
                    @empty
                        <tr><td colspan="4" class="px-4 py-8 text-center text-slate-500">No drugs in inventory.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <div>{{ $drugs->links() }}</div>
    </div>
</x-app-layout>
