@php
    $stateClass = [
        'due' => 'bg-amber-50 text-amber-800',
        'overdue' => 'bg-red-50 text-red-800',
        'given' => 'bg-emerald-50 text-emerald-800',
        'held' => 'bg-slate-100 text-slate-700',
        'refused' => 'bg-orange-50 text-orange-800',
        'missed' => 'bg-red-50 text-red-700',
        'prn_due' => 'bg-sky-50 text-sky-800',
        'prn_given' => 'bg-emerald-50 text-emerald-700',
    ];
@endphp

<x-app-layout title="MAR · {{ $unit->name }}">
    <div class="space-y-6">
        <div class="flex flex-wrap items-start justify-between gap-4">
            <div>
                <p class="text-sm text-slate-500">
                    @include('care.nursing.partials.unit-crumb', ['unit' => $unit])
                    <span class="text-slate-300">/</span>
                    Medication round
                </p>
                <h1 class="mt-1 text-2xl font-semibold text-slate-900">MAR board</h1>
                <p class="mt-1 text-sm text-slate-500">Due doses for patients placed on this unit.</p>
            </div>
            <form method="GET" class="flex items-end gap-2">
                <div>
                    <label class="block text-xs font-medium uppercase text-slate-500">Date</label>
                    <input type="date" name="date" value="{{ $day->toDateString() }}" class="mt-1 rounded-lg border-slate-300 text-sm">
                </div>
                <button type="submit" class="btn-secondary">Go</button>
            </form>
        </div>

        <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">Time</th>
                        <th class="px-4 py-3">Patient / bed</th>
                        <th class="px-4 py-3">Drug</th>
                        <th class="px-4 py-3">Dose / route</th>
                        <th class="px-4 py-3">Status</th>
                        <th class="px-4 py-3"></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-50">
                    @forelse ($rows as $row)
                        @php
                            $order = $row['order'];
                            $state = $row['state'];
                            $scheduled = $row['scheduled_for'];
                            $done = in_array($state, ['given', 'held', 'refused', 'missed', 'prn_given'], true);
                        @endphp
                        <tr>
                            <td class="px-4 py-3 whitespace-nowrap font-medium">
                                {{ $scheduled?->format('H:i') ?? 'PRN' }}
                            </td>
                            <td class="px-4 py-3">
                                {{ $row['patient']?->fullName() }}
                                <div class="text-xs text-slate-400">{{ $row['bed']?->label ?? 'No bed' }}</div>
                            </td>
                            <td class="px-4 py-3">
                                {{ $order->drug_name }}
                                <div class="text-xs text-slate-400">{{ strtoupper($order->frequency_code) }}</div>
                            </td>
                            <td class="px-4 py-3">
                                {{ $order->dosage ?: '—' }}
                                <div class="text-xs text-slate-400">{{ $order->route ?: '—' }}</div>
                            </td>
                            <td class="px-4 py-3">
                                <span class="inline-flex rounded-full px-2 py-0.5 text-xs font-medium {{ $stateClass[$state] ?? 'bg-slate-100 text-slate-700' }}">
                                    {{ $stateLabels[$state] ?? $state }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-right whitespace-nowrap">
                                <a href="{{ route('care.visits.mar', $row['visit']) }}" class="text-sky-600 hover:text-sky-800">Chart</a>
                                @if ($canAdminister && ! $done)
                                    <form method="POST" action="{{ route('care.mar.administer', $order) }}" class="mt-2 inline-flex flex-wrap items-center justify-end gap-1">
                                        @csrf
                                        <input type="hidden" name="redirect_to" value="{{ url()->full() }}">
                                        @if ($scheduled)
                                            <input type="hidden" name="scheduled_for" value="{{ $scheduled->toDateTimeString() }}">
                                        @endif
                                        <button name="status" value="given" class="rounded border border-emerald-200 px-2 py-1 text-xs text-emerald-700 hover:bg-emerald-50">Given</button>
                                        <button name="status" value="held" class="rounded border border-slate-200 px-2 py-1 text-xs text-slate-600 hover:bg-slate-50" onclick="this.form.reason.value=prompt('Reason for hold')||''; return !!this.form.reason.value;">Hold</button>
                                        <input type="hidden" name="reason" value="">
                                    </form>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="6" class="px-4 py-8 text-center text-slate-500">No MAR doses for this date. Place patients and ensure active prescriptions exist.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</x-app-layout>
