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

<section class="page-heading">
    <div>
        <p class="eyebrow">Analyse</p>
        <h1>Statistiques</h1>
    </div>
</section>

<form class="filter-panel client-filter" method="get" action="<?= url('/statistics') ?>">
    <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($summary['paidRevenue'], 2, ',', ' ') ?></strong>
    </article>
    <article class="stat-card">
        <span>Paiements en attente</span>
        <strong><?= number_format($summary['pendingRevenue'], 2, ',', ' ') ?></strong>
    </article>
    <article class="stat-card">
        <span>Reservations</span>
        <strong><?= $summary['reservations'] ?></strong>
    </article>
    <article class="stat-card">
        <span>Taux occupation</span>
        <strong><?= number_format($summary['occupancyRate'], 2, ',', ' ') ?>%</strong>
    </article>
</section>

<section class="stats-layout">
    <article class="table-panel">
        <h2 class="panel-title">Revenus par bien</h2>
        <table>
            <thead>
                <tr>
                    <th>Bien</th>
                    <th>Revenus</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($propertyRevenues as $row): ?>
                    <tr>
                        <td><?= e($row['title']) ?></td>
                        <td><?= number_format((float) $row['revenue'], 2, ',', ' ') ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($propertyRevenues)): ?>
                    <tr><td colspan="2">Aucun bien trouve.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </article>

    <article class="table-panel">
        <h2 class="panel-title">Revenus par jour</h2>
        <table>
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Revenus</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($dailyRevenues as $row): ?>
                    <tr>
                        <td><?= e($row['payment_date']) ?></td>
                        <td><?= number_format((float) $row['revenue'], 2, ',', ' ') ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($dailyRevenues)): ?>
                    <tr><td colspan="2">Aucun paiement paye sur cette periode.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </article>
</section>

<section class="stats-layout">
    <article class="table-panel">
        <h2 class="panel-title">Revenus par semaine</h2>
        <div class="bar-list">
            <?php $maxWeekly = max(1, ...array_map(static fn ($row) => (float) $row['revenue'], $weeklyRevenues ?: [['revenue' => 0]])); ?>
            <?php foreach ($weeklyRevenues as $row): ?>
                <div class="bar-row">
                    <span>Semaine <?= e($row['period_key']) ?></span>
                    <div><i style="width: <?= max(3, ((float) $row['revenue'] / $maxWeekly) * 100) ?>%"></i></div>
                    <strong><?= number_format((float) $row['revenue'], 2, ',', ' ') ?></strong>
                </div>
            <?php endforeach; ?>
            <?php if (empty($weeklyRevenues)): ?>
                <p class="muted">Aucun revenu par semaine.</p>
            <?php endif; ?>
        </div>
    </article>

    <article class="table-panel">
        <h2 class="panel-title">Revenus par mois</h2>
        <div class="bar-list">
            <?php $maxMonthly = max(1, ...array_map(static fn ($row) => (float) $row['revenue'], $monthlyRevenues ?: [['revenue' => 0]])); ?>
            <?php foreach ($monthlyRevenues as $row): ?>
                <div class="bar-row">
                    <span><?= e($row['period_key']) ?></span>
                    <div><i style="width: <?= max(3, ((float) $row['revenue'] / $maxMonthly) * 100) ?>%"></i></div>
                    <strong><?= number_format((float) $row['revenue'], 2, ',', ' ') ?></strong>
                </div>
            <?php endforeach; ?>
            <?php if (empty($monthlyRevenues)): ?>
                <p class="muted">Aucun revenu par mois.</p>
            <?php endif; ?>
        </div>
    </article>
</section>

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