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

<section class="page-heading">
    <div>
        <p class="eyebrow">Rapports</p>
        <h1>Rapport detaille</h1>
    </div>
    <button class="no-print" type="button" onclick="window.print()">Imprimer / PDF</button>
</section>

<form class="filter-panel client-filter no-print" method="get" action="<?= url('/reports') ?>">
    <label>
        Du
        <input type="date" name="from" value="<?= e($from) ?>">
    </label>
    <label>
        Au
        <input type="date" name="to" value="<?= e($to) ?>">
    </label>
    <button type="submit">Afficher</button>
</form>

<section class="stat-grid">
    <article class="stat-card">
        <span>Revenus payes</span>
        <strong><?= number_format((float) $summary['paid_revenue'], 2, ',', ' ') ?></strong>
    </article>
    <article class="stat-card">
        <span>Paiements en attente</span>
        <strong><?= number_format((float) $summary['pending_revenue'], 2, ',', ' ') ?></strong>
    </article>
    <article class="stat-card">
        <span>Reservations</span>
        <strong><?= (int) $summary['reservations_count'] ?></strong>
    </article>
    <article class="stat-card">
        <span>Biens</span>
        <strong><?= (int) $summary['properties_count'] ?></strong>
    </article>
</section>

<section class="stats-layout">
    <article class="table-panel">
        <h2 class="panel-title">Reservations</h2>
        <table>
            <thead>
                <tr>
                    <th>Bien</th>
                    <th>Client</th>
                    <th>Dates</th>
                    <th>Statut</th>
                    <th>Montant</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><?= e(reservation_statuses()[$reservation['status']] ?? $reservation['status']) ?></td>
                        <td><?= number_format((float) $reservation['total_amount'], 2, ',', ' ') ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($reservations)): ?>
                    <tr><td colspan="5">Aucune reservation.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </article>

    <article class="table-panel">
        <h2 class="panel-title">Paiements</h2>
        <table>
            <thead>
                <tr>
                    <th>Client</th>
                    <th>Bien</th>
                    <th>Date</th>
                    <th>Statut</th>
                    <th>Montant</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($payments as $payment): ?>
                    <tr>
                        <td><?= e($payment['first_name'] . ' ' . $payment['last_name']) ?></td>
                        <td><?= e($payment['property_title']) ?></td>
                        <td><?= e($payment['payment_date'] ?: '-') ?></td>
                        <td><?= e(payment_statuses()[$payment['status']] ?? $payment['status']) ?></td>
                        <td><?= number_format((float) $payment['amount'], 2, ',', ' ') ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($payments)): ?>
                    <tr><td colspan="5">Aucun paiement.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </article>
</section>

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