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

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

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

<section class="stat-grid compact-stats">
    <article class="stat-card"><span>Depenses globales</span><strong><?= number_format((float) $total, 2, ',', ' ') ?></strong></article>
    <article class="stat-card"><span>Nombre lignes</span><strong><?= (int) ($pagination['total_items'] ?? count($expenses)) ?></strong></article>
</section>

<form class="filter-panel reservation-filter" method="get" action="<?= url('/expenses') ?>">
    <label>Du <input type="date" name="from" value="<?= e($from) ?>"></label>
    <label>Au <input type="date" name="to" value="<?= e($to) ?>"></label>
    <label>Categorie <select name="category"><option value="">Toutes</option><?php foreach (expense_categories() as $key => $label): ?><option value="<?= e($key) ?>" <?= $category === $key ? 'selected' : '' ?>><?= e($label) ?></option><?php endforeach; ?></select></label>
    <label>Bien <select name="property_id"><option value="">Tous</option><?php foreach ($properties as $property): ?><option value="<?= (int) $property['id'] ?>" <?= $propertyId === (int) $property['id'] ? 'selected' : '' ?>><?= e($property['title']) ?></option><?php endforeach; ?></select></label>
    <button type="submit">Filtrer</button>
</form>

<section class="stats-layout">
    <article class="table-panel">
        <h2 class="panel-title">Depenses par jour</h2>
        <table><thead><tr><th>Date</th><th>Total</th></tr></thead><tbody><?php foreach ($dailyExpenses as $row): ?><tr><td><?= e($row['label']) ?></td><td><?= number_format((float) $row['total'], 2, ',', ' ') ?></td></tr><?php endforeach; ?><?php if (!$dailyExpenses): ?><tr><td colspan="2">Aucune donnee.</td></tr><?php endif; ?></tbody></table>
    </article>
    <article class="table-panel">
        <h2 class="panel-title">Par appartement</h2>
        <table><thead><tr><th>Bien</th><th>Total</th></tr></thead><tbody><?php foreach ($propertyExpenses as $row): ?><tr><td><?= e($row['label']) ?></td><td><?= number_format((float) $row['total'], 2, ',', ' ') ?></td></tr><?php endforeach; ?><?php if (!$propertyExpenses): ?><tr><td colspan="2">Aucune donnee.</td></tr><?php endif; ?></tbody></table>
    </article>
</section>

<section class="stats-layout">
    <article class="table-panel"><h2 class="panel-title">Par mois</h2><table><thead><tr><th>Mois</th><th>Total</th></tr></thead><tbody><?php foreach ($monthlyExpenses as $row): ?><tr><td><?= e($row['label']) ?></td><td><?= number_format((float) $row['total'], 2, ',', ' ') ?></td></tr><?php endforeach; ?></tbody></table></article>
    <article class="table-panel"><h2 class="panel-title">Par annee</h2><table><thead><tr><th>Annee</th><th>Total</th></tr></thead><tbody><?php foreach ($yearlyExpenses as $row): ?><tr><td><?= e((string) $row['label']) ?></td><td><?= number_format((float) $row['total'], 2, ',', ' ') ?></td></tr><?php endforeach; ?></tbody></table></article>
</section>

<section class="table-panel">
    <table>
        <thead><tr><th>Date</th><th>Categorie</th><th>Bien</th><th>Montant</th><th>Description</th><th>Actions</th></tr></thead>
        <tbody>
            <?php foreach ($expenses as $expense): ?>
                <tr>
                    <td><?= e($expense['expense_date']) ?></td>
                    <td><?= e(expense_categories()[$expense['category']] ?? $expense['category']) ?></td>
                    <td><?= e($expense['property_title'] ?: '-') ?></td>
                    <td><?= number_format((float) $expense['amount'], 2, ',', ' ') ?></td>
                    <td><?= e($expense['description']) ?></td>
                    <td class="actions">
                        <?php if (Auth::can('expenses.edit')): ?><a href="<?= url('/expenses/edit?id=' . $expense['id']) ?>">Modifier</a><?php endif; ?>
                        <?php if (Auth::can('expenses.delete')): ?><form method="post" action="<?= url('/expenses/delete') ?>" onsubmit="return confirm('Supprimer cette depense ?');"><input type="hidden" name="_token" value="<?= csrf_token() ?>"><input type="hidden" name="id" value="<?= (int) $expense['id'] ?>"><button class="link-danger" type="submit">Supprimer</button></form><?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            <?php if (!$expenses): ?><tr><td colspan="6">Aucune depense trouvee.</td></tr><?php endif; ?>
        </tbody>
    </table>
</section>

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

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