@php
    $title = 'Бронирования — Hotel Pineta';
    $heading = 'Бронирования';
    $subheading = 'Журнал бронирований новой Laravel-базы';
@endphp

@extends('layouts.admin')

@section('content')
    <div class="card">
        <div class="actions-row" style="justify-content: space-between; margin-bottom: 14px;">
            <h3 style="margin:0;">Фильтры</h3>
            <a href="{{ route('admin.bookings.create') }}" class="primary">Новая бронь</a>
        </div>

        <form method="get" class="filters">
            <div class="field">
                <label for="status">Статус</label>
                <select id="status" name="status">
                    <option value="">Все</option>
                    @foreach ($statuses as $value => $label)
                        <option value="{{ $value }}" @selected($filters['status'] === $value)>{{ $label }}</option>
                    @endforeach
                </select>
            </div>

            <div class="field">
                <label for="source">Источник</label>
                <select id="source" name="source">
                    <option value="">Все</option>
                    @foreach ($sources as $value => $label)
                        <option value="{{ $value }}" @selected($filters['source'] === $value)>{{ $label }}</option>
                    @endforeach
                </select>
            </div>

            <div class="field">
                <label for="room_id">Комната</label>
                <select id="room_id" name="room_id">
                    <option value="">Все</option>
                    @foreach ($rooms as $room)
                        <option value="{{ $room->id }}" @selected($filters['room_id'] === (string) $room->id)>№ {{ $room->code }} — {{ $room->title }}</option>
                    @endforeach
                </select>
            </div>

            <div class="field">
                <label for="arrival_date">Дата заезда</label>
                <input id="arrival_date" type="date" name="arrival_date" value="{{ $filters['arrival_date'] }}">
            </div>

            <div class="field">
                <label for="search">Поиск</label>
                <input id="search" type="text" name="search" value="{{ $filters['search'] }}" placeholder="Имя, телефон, e-mail, номер брони">
            </div>

            <div class="field-inline" style="grid-column: 1 / -1;">
                <div class="help">Фильтры уже работают по статусу, источнику, комнате, дате заезда и поисковой строке.</div>
                <div class="actions-row">
                    <a href="{{ route('admin.bookings.index') }}" class="secondary">Сбросить</a>
                    <button type="submit" class="primary">Применить</button>
                </div>
            </div>
        </form>
    </div>

    <div class="card" style="margin-top: 18px;">
        <h3>Журнал бронирований</h3>

        @if ($bookings->isEmpty())
            <div class="empty">
                Пока бронирований нет. Создайте первую запись через админскую форму.
            </div>
        @else
            <div class="table-wrap">
                <table>
                    <thead>
                    <tr>
                        <th>Номер брони</th>
                        <th>Гость</th>
                        <th>Контакт</th>
                        <th>Комната</th>
                        <th>Заезд / выезд</th>
                        <th>Статус</th>
                        <th>Источник</th>
                        <th>Оценка</th>
                        <th>Сумма</th>
                        <th>Создано</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach ($bookings as $booking)
                        <tr>
                            <td><a href="{{ route('admin.bookings.show', $booking) }}">{{ $booking->booking_number }}</a></td>
                            <td>{{ $booking->guest_name }}</td>
                            <td>
                                <div>{{ $booking->phone ?: '—' }}</div>
                                <div class="muted">{{ $booking->email ?: '—' }}</div>
                            </td>
                            <td>№ {{ $booking->room?->code }} — {{ $booking->room?->title }}</td>
                            <td>
                                <div>{{ $booking->arrival_date?->format('d.m.Y') ?: '—' }}</div>
                                <div class="muted">{{ $booking->departure_date?->format('d.m.Y') ?: '—' }}</div>
                            </td>
                            <td><span class="badge {{ $booking->status }}">{{ \App\Models\Booking::statusOptions()[$booking->status] ?? $booking->status }}</span></td>
                            <td>{{ \App\Models\Booking::sourceOptions()[$booking->source] ?? $booking->source }}</td>
                            <td>{{ $booking->guest_rating ?: ($booking->guest?->admin_rating ?: '—') }}</td>
                            <td>{{ number_format((float) $booking->full_amount, 2, '.', ' ') }} €</td>
                            <td>{{ $booking->created_at?->format('d.m.Y H:i') }}</td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>

            <div class="admin-pagination">
                <div class="admin-pagination-summary">
                    Показано с {{ $bookings->firstItem() }} по {{ $bookings->lastItem() }} из {{ $bookings->total() }} записей
                </div>

                @if ($bookings->hasPages())
                    <div class="admin-pagination-actions">
                        @if ($bookings->onFirstPage())
                            <span class="secondary disabled">Назад</span>
                        @else
                            <a class="secondary" href="{{ $bookings->previousPageUrl() }}" rel="prev">Назад</a>
                        @endif

                        <span class="admin-pagination-page">Страница {{ $bookings->currentPage() }} из {{ $bookings->lastPage() }}</span>

                        @if ($bookings->hasMorePages())
                            <a class="secondary" href="{{ $bookings->nextPageUrl() }}" rel="next">Вперёд</a>
                        @else
                            <span class="secondary disabled">Вперёд</span>
                        @endif
                    </div>
                @endif
            </div>
        @endif
    </div>
@endsection
