<x-app-layout :title="'Prescription · '.$prescription->patient->fullName()">
    <x-care.consultation-return :consultation="$returnConsultation ?? null" />

    <div class="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
        <div>
            <p class="text-xs uppercase text-slate-500">{{ $statuses[$prescription->status] ?? $prescription->status }}</p>
            <h1 class="text-2xl font-semibold text-slate-900">{{ $prescription->patient->fullName() }}</h1>
            <p class="mt-1 text-sm text-slate-500">{{ $prescription->created_at->format('d M Y H:i') }}</p>
        </div>
        <div class="flex gap-2">
            @if ($canManage && $prescription->status === \App\Models\Prescription::STATUS_DRAFT)
                <form method="POST" action="{{ route('care.prescriptions.activate', $prescription) }}">@csrf<button class="btn-primary">Activate</button></form>
            @endif
            @if ($canDispense && $prescription->status === \App\Models\Prescription::STATUS_ACTIVE)
                <form method="POST" action="{{ route('care.prescriptions.dispense', $prescription) }}">@csrf<button class="btn-primary">Dispense</button></form>
            @endif
            @if ($canManage && ! in_array($prescription->status, [\App\Models\Prescription::STATUS_DISPENSED, \App\Models\Prescription::STATUS_CANCELLED]))
                <x-confirm-dialog
                    :name="'cancel-prescription-'.$prescription->id"
                    title="Cancel this prescription?"
                    message="The prescription will be cancelled and can no longer be dispensed."
                    :action="route('care.prescriptions.cancel', $prescription)"
                    confirm-label="Cancel prescription"
                >
                    <x-slot:trigger>
                        <x-btn type="button" variant="danger">Cancel</x-btn>
                    </x-slot:trigger>
                </x-confirm-dialog>
            @endif
        </div>
    </div>

    <section class="mt-6 rounded-2xl border border-slate-200 bg-white p-6">
        <h2 class="text-sm font-semibold uppercase text-slate-500">Items</h2>
        <div class="mt-4 space-y-4">
            @foreach ($prescription->items as $item)
                <div class="rounded-xl border border-slate-100 bg-slate-50 p-4 text-sm">
                    <p class="font-medium text-slate-900">{{ $item->name }} @if ($item->is_procedure)<span class="text-amber-600">(procedure)</span>@endif</p>
                    <p class="mt-1 text-slate-600">
                        @if ($item->dosage) {{ $item->dosage }} @endif
                        @if ($item->frequency) · {{ $item->frequency }} @endif
                        @if ($item->duration) · {{ $item->duration }} @endif
                        @if ($item->route) · {{ $routes[$item->route] ?? $item->route }} @endif
                        @if ($item->quantity) · Qty: {{ $item->quantity }} @endif
                    </p>
                    @if ($item->instructions)<p class="mt-1 text-slate-500">{{ $item->instructions }}</p>@endif
                </div>
            @endforeach
        </div>
        @if ($prescription->notes)<p class="mt-4 text-sm text-slate-600">{{ $prescription->notes }}</p>@endif
    </section>
</x-app-layout>
