@php $money = fn ($minor) => config('care.billing.currency').' '.number_format($minor / 100, 2); @endphp
<x-app-layout :title="$drug->name">
    <div class="flex items-center justify-between">
        <div>
            <h1 class="text-2xl font-semibold text-slate-900">{{ $drug->name }}</h1>
            <p class="text-sm text-slate-500">Stock: {{ $drug->stockOnHand() }} {{ $drug->unit }} · {{ $money($drug->unit_price_minor) }}</p>
        </div>
        @if ($canManage)<a href="{{ route('care.pharmacy.drugs.edit', $drug) }}" class="rounded-lg border border-slate-200 px-4 py-2 text-sm">Edit</a>@endif
    </div>
    @if ($canManage)
        <section class="mt-6 rounded-2xl border border-slate-200 bg-white p-6">
            <h2 class="text-sm font-semibold uppercase text-slate-500">Receive stock</h2>
            <form method="POST" action="{{ route('care.pharmacy.drugs.batches.store', $drug) }}" class="mt-4 grid gap-3 sm:grid-cols-2">
                @csrf
                <input type="text" name="batch_number" required placeholder="Batch number" class="rounded-lg border-slate-300 text-sm">
                <input type="date" name="expiry_date" class="rounded-lg border-slate-300 text-sm">
                <input type="number" name="quantity_on_hand" required min="1" placeholder="Quantity" class="rounded-lg border-slate-300 text-sm">
                <input type="number" name="cost_minor" min="0" placeholder="Cost (minor)" class="rounded-lg border-slate-300 text-sm">
                <button type="submit" class="btn-primary sm:col-span-2">Receive batch</button>
            </form>
        </section>
    @endif
    <section class="mt-6 rounded-2xl border border-slate-200 bg-white p-6">
        <h2 class="text-sm font-semibold uppercase text-slate-500">Batches</h2>
        <div class="mt-4 space-y-2 text-sm">
            @forelse ($drug->batches as $batch)
                <div class="flex justify-between rounded-lg border border-slate-100 bg-slate-50 px-3 py-2 {{ $batch->isExpired() ? 'border-red-200 bg-red-50' : '' }}">
                    <span>{{ $batch->batch_number }} · {{ $batch->quantity_on_hand }} on hand</span>
                    <span class="text-slate-500">{{ $batch->expiry_date?->format('Y-m-d') ?? 'No expiry' }}</span>
                </div>
            @empty
                <p class="text-slate-400">No batches received.</p>
            @endforelse
        </div>
    </section>
</x-app-layout>
