@php
    $canManageAppointments = app(\App\Services\Care\CarePermissions::class)->can(
        auth()->user() ? app(\App\Services\Care\OrganizationResolver::class)->memberFor(auth()->user(), $organization) : null,
        'appointments.manage'
    );
@endphp

<x-app-layout title="Appointments">
    <div class="space-y-6">
        <x-care.page-hero
            badge="Schedule · Walk-ins · Check-in"
            title="Appointments"
            description="Schedule visits, register walk-ins, and track patient flow from booking through consultation."
            :stats="[
                ['value' => number_format($heroStats['today']), 'label' => 'Today'],
                ['value' => number_format($heroStats['scheduled']), 'label' => 'Scheduled'],
                ['value' => number_format($heroStats['waiting']), 'label' => 'Waiting'],
            ]">
            @if ($canManageAppointments)
                <x-slot name="actions">
                    <a href="{{ route('care.appointments.walk-in.create') }}" class="btn-secondary">Walk-in</a>
                    <a href="{{ route('care.appointments.create') }}" class="btn-primary">Book appointment</a>
                </x-slot>
            @endif
        </x-care.page-hero>

        <form method="GET" class="flex flex-wrap gap-3 rounded-2xl border border-slate-200 bg-white p-4">
            <input type="date" name="date" value="{{ request('date') }}" class="rounded-lg border-slate-300 text-sm">
            <select name="status" class="rounded-lg border-slate-300 text-sm">
                <option value="">All statuses</option>
                @foreach ($statuses as $value => $label)
                    <option value="{{ $value }}" @selected(request('status') === $value)>{{ $label }}</option>
                @endforeach
            </select>
            @unless ($lockToPractitioner ?? false)
                <select name="practitioner_id" class="rounded-lg border-slate-300 text-sm">
                    <option value="">All practitioners</option>
                    @foreach ($practitioners as $practitioner)
                        <option value="{{ $practitioner->id }}" @selected((string) request('practitioner_id') === (string) $practitioner->id)>{{ $practitioner->name }}</option>
                    @endforeach
                </select>
            @endunless
            <button type="submit" class="btn-primary">Filter</button>
            @if (request()->hasAny(['date', 'status', 'practitioner_id']))
                <a href="{{ route('care.appointments.index') }}" class="rounded-lg border border-slate-200 px-4 py-2 text-sm text-slate-600">Clear</a>
            @endif
        </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">Patient</th>
                        <th class="px-4 py-3">Scheduled</th>
                        <th class="px-4 py-3">Practitioner</th>
                        <th class="px-4 py-3">Type</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 ($appointments as $appointment)
                        <tr>
                            <td class="px-4 py-3">
                                <p class="font-medium text-slate-900">{{ $appointment->patient->fullName() }}</p>
                                <p class="text-xs text-slate-500">{{ $appointment->patient->patient_number }}</p>
                            </td>
                            <td class="px-4 py-3">{{ $appointment->scheduled_at?->format('d M Y H:i') ?? '—' }}</td>
                            <td class="px-4 py-3">{{ $appointment->practitioner?->name ?? '—' }}</td>
                            <td class="px-4 py-3 capitalize">{{ str_replace('_', ' ', $appointment->type) }}</td>
                            <td class="px-4 py-3">
                                <span class="rounded-full bg-slate-100 px-2 py-0.5 text-xs font-medium text-slate-700">
                                    {{ $statuses[$appointment->status] ?? $appointment->status }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-right">
                                <a href="{{ route('care.appointments.show', $appointment) }}" class="text-sky-600 hover:text-sky-700">View</a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="px-4 py-8 text-center text-slate-500">No appointments found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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