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

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

<?php if (!empty($notice)): ?>
    <div class="alert alert-success"><?= e($notice) ?></div>
<?php endif; ?>

<form class="filter-panel reservation-filter" method="get" action="<?= url('/reservations') ?>">
    <label>
        Du
        <input type="date" name="from" value="<?= e($from) ?>">
    </label>
    <label>
        Au
        <input type="date" name="to" value="<?= e($to) ?>">
    </label>
    <label>
        Ville
        <input type="text" name="city" value="<?= e($city) ?>" placeholder="Casablanca">
    </label>
    <label>
        Quartier
        <input type="text" name="district" value="<?= e($district) ?>" placeholder="Maarif">
    </label>
    <label>
        Statut
        <select name="status">
            <option value="">Tous</option>
            <?php foreach (reservation_statuses() as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= $status === $key ? 'selected' : '' ?>><?= e($label) ?></option>
            <?php endforeach; ?>
        </select>
    </label>
    <label>
        Source
        <select name="source">
            <option value="">Toutes</option>
            <?php foreach (reservation_sources() as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= $source === $key ? 'selected' : '' ?>><?= e($label) ?></option>
            <?php endforeach; ?>
        </select>
    </label>
    <button type="submit">Filtrer</button>
</form>

<section class="table-panel">
    <table>
        <thead>
            <tr>
                <th>Bien</th>
                <th>Client</th>
                <th>Dates</th>
                <th>Montant</th>
                <th>Statut</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($reservations as $reservation): ?>
                <tr>
                    <td><?= e($reservation['property_title']) ?></td>
                    <td><?= e($reservation['first_name'] . ' ' . $reservation['last_name']) ?></td>
                    <td><?= e($reservation['start_date'] . ' - ' . $reservation['end_date']) ?></td>
                    <td><?= number_format((float) $reservation['total_amount'], 2, ',', ' ') ?></td>
                    <td>
                        <span class="status-pill reservation-<?= e($reservation['status']) ?>">
                            <?= e(reservation_statuses()[$reservation['status']] ?? $reservation['status']) ?>
                        </span>
                    </td>
                    <td class="actions">
                        <a href="<?= url('/reservations/show?id=' . $reservation['id']) ?>">Voir</a>
                        <?php if (Auth::can('reservations.edit')): ?>
                            <a href="<?= url('/reservations/edit?id=' . $reservation['id']) ?>">Modifier</a>
                        <?php endif; ?>
                        <?php if (Auth::can('reservations.delete')): ?>
                            <form method="post" action="<?= url('/reservations/delete') ?>" onsubmit="return confirm('Supprimer cette reservation ?');">
                                <input type="hidden" name="_token" value="<?= csrf_token() ?>">
                                <input type="hidden" name="id" value="<?= (int) $reservation['id'] ?>">
                                <button class="link-danger" type="submit">Supprimer</button>
                            </form>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($reservations)): ?>
                <tr>
                    <td colspan="6">Aucune reservation trouvee.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</section>

<?php require dirname(__DIR__) . '/partials/pagination.php'; ?>

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