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

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

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

<section class="stat-grid compact-stats">
    <article class="stat-card">
        <span>Taux occupation global</span>
        <strong><?= number_format($globalOccupancyRate, 2, ',', ' ') ?>%</strong>
    </article>
    <article class="stat-card">
        <span>Biens analyses</span>
        <strong><?= count($profitability) ?></strong>
    </article>
    <article class="stat-card">
        <span>Sources</span>
        <strong><?= count($sources) ?></strong>
    </article>
    <article class="stat-card">
        <span>Agents</span>
        <strong><?= count($agents) ?></strong>
    </article>
</section>

<section class="table-panel">
    <h2 class="panel-title">Top appartements les plus rentables</h2>
    <table>
        <thead>
            <tr>
                <th>Bien</th>
                <th>Ville</th>
                <th>Courte duree</th>
                <th>Longue duree</th>
                <th>Depenses</th>
                <th>Benefice net</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($profitability as $row): ?>
                <?php $net = (float) $row['short_revenue'] + (float) $row['long_revenue'] - (float) $row['expenses']; ?>
                <tr>
                    <td><?= e($row['title']) ?></td>
                    <td><?= e($row['city']) ?></td>
                    <td><?= number_format((float) $row['short_revenue'], 2, ',', ' ') ?></td>
                    <td><?= number_format((float) $row['long_revenue'], 2, ',', ' ') ?></td>
                    <td><?= number_format((float) $row['expenses'], 2, ',', ' ') ?></td>
                    <td><?= number_format($net, 2, ',', ' ') ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (!$profitability): ?><tr><td colspan="6">Aucune donnee.</td></tr><?php endif; ?>
        </tbody>
    </table>
</section>

<section class="stats-layout">
    <article class="table-panel">
        <h2 class="panel-title">Analyse par ville</h2>
        <table>
            <thead><tr><th>Ville</th><th>Courte duree</th><th>Longue duree</th><th>Total</th></tr></thead>
            <tbody>
                <?php foreach ($cities as $row): ?>
                    <tr>
                        <td><?= e($row['label']) ?></td>
                        <td><?= number_format((float) $row['short_revenue'], 2, ',', ' ') ?></td>
                        <td><?= number_format((float) $row['long_revenue'], 2, ',', ' ') ?></td>
                        <td><?= number_format((float) $row['short_revenue'] + (float) $row['long_revenue'], 2, ',', ' ') ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (!$cities): ?><tr><td colspan="4">Aucune donnee.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </article>

    <article class="table-panel">
        <h2 class="panel-title">Analyse par locataire</h2>
        <table>
            <thead><tr><th>Locataire</th><th>Encaisse</th><th>Restant</th></tr></thead>
            <tbody>
                <?php foreach ($tenants as $row): ?>
                    <tr>
                        <td><?= e($row['label']) ?></td>
                        <td><?= number_format((float) $row['revenue'], 2, ',', ' ') ?></td>
                        <td><?= number_format((float) $row['remaining'], 2, ',', ' ') ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (!$tenants): ?><tr><td colspan="3">Aucun locataire.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </article>
</section>

<section class="stats-layout">
    <article class="table-panel">
        <h2 class="panel-title">Sources de reservation les plus performantes</h2>
        <table>
            <thead><tr><th>Source</th><th>Reservations</th><th>Revenus</th></tr></thead>
            <tbody>
                <?php foreach ($sources as $row): ?>
                    <tr>
                        <td><?= e(reservation_sources()[$row['label']] ?? $row['label']) ?></td>
                        <td><?= (int) $row['total'] ?></td>
                        <td><?= number_format((float) $row['revenue'], 2, ',', ' ') ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (!$sources): ?><tr><td colspan="3">Aucune source.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </article>

    <article class="table-panel">
        <h2 class="panel-title">Agents les plus performants</h2>
        <table>
            <thead><tr><th>Agent</th><th>Reservations</th><th>Revenus</th></tr></thead>
            <tbody>
                <?php foreach ($agents as $row): ?>
                    <tr>
                        <td><?= e($row['label']) ?></td>
                        <td><?= (int) $row['total'] ?></td>
                        <td><?= number_format((float) $row['revenue'], 2, ',', ' ') ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (!$agents): ?><tr><td colspan="3">Aucun agent.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </article>
</section>

<section class="table-panel">
    <h2 class="panel-title">Taux occupation par bien</h2>
    <table>
        <thead><tr><th>Bien</th><th>Nuits occupees</th><th>Taux occupation</th></tr></thead>
        <tbody>
            <?php foreach ($occupancyByProperty as $row): ?>
                <tr>
                    <td><?= e($row['title']) ?></td>
                    <td><?= (int) $row['occupied_nights'] ?></td>
                    <td><?= number_format((float) $row['occupancy_rate'], 2, ',', ' ') ?>%</td>
                </tr>
            <?php endforeach; ?>
            <?php if (!$occupancyByProperty): ?><tr><td colspan="3">Aucun bien.</td></tr><?php endif; ?>
        </tbody>
    </table>
</section>

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