@extends('layouts.epelcon')
@section('title', 'Bookings')

@section('content')
<div x-data="{ create: false }">
    <div class="mb-5 flex flex-wrap items-center justify-between gap-3">
        <div>
            <h1 class="text-xl font-extrabold text-slate-900 lg:text-2xl">Reception · Bookings</h1>
            <p class="text-sm text-slate-500">Reservations, check-ins and check-outs.</p>
        </div>
        <div class="flex gap-2">
            <a href="{{ route('epelcon.bookings.calendar') }}" class="rounded-xl bg-white px-4 py-2.5 text-sm font-bold text-slate-700 shadow-sm ring-1 ring-slate-200 hover:bg-slate-50">📅 Calendar</a>
            <button @click="create = true" class="rounded-xl bg-indigo-600 px-4 py-2.5 text-sm font-bold text-white shadow-lg shadow-indigo-600/25 hover:bg-indigo-700">+ New booking</button>
        </div>
    </div>

    {{-- Status tabs --}}
    <div class="no-scrollbar mb-4 flex gap-2 overflow-x-auto">
        @foreach (['' => 'All (' . $counts['all'] . ')', 'pending' => 'Pending (' . $counts['pending'] . ')', 'confirmed' => 'Confirmed (' . $counts['confirmed'] . ')', 'checked_in' => 'In-house (' . $counts['checked_in'] . ')', 'checked_out' => 'Checked out (' . $counts['checked_out'] . ')'] as $value => $label)
            <a href="{{ route('epelcon.bookings.index', array_filter(['status' => $value, 'q' => request('q')])) }}"
               class="shrink-0 rounded-full px-4 py-2 text-xs font-bold {{ request('status', '') === $value ? 'bg-slate-900 text-white' : 'bg-white text-slate-600 ring-1 ring-slate-200' }}">{{ $label }}</a>
        @endforeach
    </div>

    <form method="GET" class="mb-4">
        <input type="hidden" name="status" value="{{ request('status') }}">
        <input name="q" value="{{ request('q') }}" placeholder="Search code, guest name or phone…"
               class="w-full rounded-xl border-0 bg-white px-4 py-3 text-sm shadow-sm ring-1 ring-slate-200 placeholder:text-slate-400 focus:ring-2 focus:ring-indigo-500 sm:max-w-md">
    </form>

    {{-- List --}}
    <div class="space-y-3">
        @forelse ($bookings as $booking)
            <a href="{{ route('epelcon.bookings.show', $booking) }}" class="block rounded-2xl bg-white p-4 shadow-sm ring-1 ring-slate-200 transition hover:ring-indigo-300">
                <div class="flex flex-wrap items-center gap-3">
                    <div class="flex h-11 w-11 shrink-0 items-center justify-center rounded-xl bg-indigo-50 font-bold text-indigo-700">{{ $booking->room->number ?? '—' }}</div>
                    <div class="min-w-0 flex-1">
                        <p class="font-bold text-slate-900">{{ $booking->customer->name }} <span class="ml-1 font-mono text-xs font-semibold text-slate-400">{{ $booking->code }}</span></p>
                        <p class="text-xs text-slate-500">{{ $booking->roomType->name ?? '' }} · {{ $booking->check_in->format('j M') }} → {{ $booking->check_out->format('j M') }} · {{ $booking->adults }}A{{ $booking->children ? ' ' . $booking->children . 'C' : '' }}</p>
                    </div>
                    <div class="text-right">
                        <p class="font-extrabold text-slate-900">@money($booking->total_amount)</p>
                        @if ($booking->balance() > 0)
                            <p class="text-[11px] font-bold text-rose-500">@money($booking->balance()) due</p>
                        @else
                            <p class="text-[11px] font-bold text-emerald-600">Fully paid</p>
                        @endif
                    </div>
                    @php $badge = ['pending'=>'amber','confirmed'=>'sky','checked_in'=>'emerald','checked_out'=>'slate','cancelled'=>'rose','no_show'=>'rose'][$booking->status] ?? 'slate'; @endphp
                    <span class="rounded-full bg-{{ $badge }}-100 px-2.5 py-1 text-[10px] font-bold uppercase tracking-wide text-{{ $badge }}-700">{{ str_replace('_', ' ', $booking->status) }}</span>
                </div>
            </a>
        @empty
            <div class="rounded-2xl bg-white p-10 text-center text-slate-400 shadow-sm ring-1 ring-slate-200">No bookings found.</div>
        @endforelse
    </div>

    <div class="mt-5">{{ $bookings->links() }}</div>

    {{-- Create modal --}}
    <div x-cloak x-show="create" class="fixed inset-0 z-50 flex items-end justify-center sm:items-center">
        <div class="absolute inset-0 bg-slate-900/50 backdrop-blur-sm" @click="create = false"></div>
        <div x-show="create" x-transition class="relative max-h-[92vh] w-full max-w-2xl overflow-y-auto rounded-t-3xl bg-white p-6 shadow-2xl sm:rounded-3xl">
            <h2 class="mb-4 text-lg font-extrabold text-slate-900">New booking</h2>
            <form method="POST" action="{{ route('epelcon.bookings.store') }}" class="grid grid-cols-2 gap-4">
                @csrf
                <label class="col-span-2 block">
                    <span class="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">Existing customer (optional)</span>
                    <select name="customer_id" class="w-full rounded-xl border-slate-200 text-sm focus:border-indigo-500 focus:ring-indigo-500">
                        <option value="">— New / walk-in guest —</option>
                        @foreach ($customers as $customer)
                            <option value="{{ $customer->id }}">{{ $customer->name }} {{ $customer->phone ? '(' . $customer->phone . ')' : '' }}</option>
                        @endforeach
                    </select>
                </label>
                <label class="col-span-2 sm:col-span-1"><span class="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">Guest name</span>
                    <input name="customer_name" class="w-full rounded-xl border-slate-200 text-sm" placeholder="For new guests"></label>
                <label class="col-span-2 sm:col-span-1"><span class="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">Phone</span>
                    <input name="customer_phone" class="w-full rounded-xl border-slate-200 text-sm"></label>
                <label class="col-span-2"><span class="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">Room</span>
                    <select name="room_id" required class="w-full rounded-xl border-slate-200 text-sm">
                        @foreach ($roomTypes as $type)
                            <optgroup label="{{ $type->name }} — {{ $CUR }}{{ number_format($type->base_price) }}/night">
                                @foreach ($type->rooms as $room)
                                    <option value="{{ $room->id }}">Room {{ $room->number }} ({{ $room->status }})</option>
                                @endforeach
                            </optgroup>
                        @endforeach
                    </select></label>
                <label><span class="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">Check-in</span>
                    <input type="date" name="check_in" required value="{{ today()->toDateString() }}" class="w-full rounded-xl border-slate-200 text-sm"></label>
                <label><span class="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">Check-out</span>
                    <input type="date" name="check_out" required value="{{ today()->addDay()->toDateString() }}" class="w-full rounded-xl border-slate-200 text-sm"></label>
                <label><span class="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">Adults</span>
                    <input type="number" name="adults" min="1" value="1" required class="w-full rounded-xl border-slate-200 text-sm"></label>
                <label><span class="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">Children</span>
                    <input type="number" name="children" min="0" value="0" class="w-full rounded-xl border-slate-200 text-sm"></label>
                <label class="col-span-2"><span class="mb-1 block text-xs font-bold uppercase tracking-wide text-slate-500">Notes</span>
                    <textarea name="notes" rows="2" class="w-full rounded-xl border-slate-200 text-sm"></textarea></label>
                <div class="col-span-2 flex justify-end gap-2 pt-1">
                    <button type="button" @click="create = false" class="rounded-xl px-4 py-2.5 text-sm font-bold text-slate-500 hover:bg-slate-100">Cancel</button>
                    <button class="rounded-xl bg-indigo-600 px-5 py-2.5 text-sm font-bold text-white hover:bg-indigo-700">Create booking</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection
