<?php require dirname(__DIR__) . '/layouts/app.php'; ?>

<section class="page-heading">
    <div>
        <p class="eyebrow">Planning</p>
        <h1>Calendrier des reservations</h1>
    </div>
    <div class="heading-actions">
        <?php if (Auth::can('reservations.create')): ?>
            <a class="button-link" href="<?= url('/reservations/create') ?>">Ajouter</a>
        <?php endif; ?>
        <a class="button-link secondary" href="<?= url('/reservations') ?>">Liste</a>
    </div>
</section>

<section class="calendar-toolbar">
    <div class="segmented">
        <a class="<?= $view === 'day' ? 'active' : '' ?>" href="<?= url('/reservations/calendar?view=day&date=' . e($date)) ?>">Jour</a>
        <a class="<?= $view === 'week' ? 'active' : '' ?>" href="<?= url('/reservations/calendar?view=week&date=' . e($date)) ?>">Semaine</a>
        <a class="<?= $view === 'month' ? 'active' : '' ?>" href="<?= url('/reservations/calendar?view=month&date=' . e($date)) ?>">Mois</a>
    </div>

    <div class="calendar-nav">
        <a href="<?= url('/reservations/calendar?view=' . $view . '&date=' . $previousDate) ?>">Precedent</a>
        <form method="get" action="<?= url('/reservations/calendar') ?>">
            <input type="hidden" name="view" value="<?= e($view) ?>">
            <input type="date" name="date" value="<?= e($date) ?>">
            <button type="submit">Ouvrir</button>
        </form>
        <a href="<?= url('/reservations/calendar?view=' . $view . '&date=' . $nextDate) ?>">Suivant</a>
    </div>
</section>

<section class="calendar-grid <?= $view === 'day' ? 'one-day' : '' ?> <?= $view === 'week' ? 'one-week' : '' ?>">
    <?php foreach ($days as $day): ?>
        <?php $dayKey = $day->format('Y-m-d'); ?>
        <article class="calendar-day <?= $day->format('m') !== $rangeStart->format('m') && $view === 'month' ? 'outside-month' : '' ?>">
            <header>
                <span><?= e($day->format('d/m')) ?></span>
                <strong><?= e($day->format('D')) ?></strong>
            </header>

            <div class="calendar-events">
                <?php foreach ($reservationsByDay[$dayKey] as $reservation): ?>
                    <a class="calendar-event reservation-<?= e($reservation['status']) ?>" href="<?= url('/reservations/show?id=' . $reservation['id']) ?>">
                        <strong><?= e($reservation['property_title']) ?></strong>
                        <span><?= e($reservation['first_name'] . ' ' . $reservation['last_name']) ?></span>
                    </a>
                <?php endforeach; ?>

                <?php if (empty($reservationsByDay[$dayKey])): ?>
                    <span class="calendar-empty">Libre</span>
                <?php endif; ?>
            </div>
        </article>
    <?php endforeach; ?>
</section>

<?php require dirname(__DIR__) . '/layouts/app_footer.php'; ?>
