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

<section class="page-heading">
    <div>
        <p class="eyebrow">Gestion Locative Longue Duree</p>
        <h1>Situation des Locataires</h1>
    </div>
    <a class="button-link" href="<?= url('/long-rentals/payments/create') ?>">Ajouter paiement</a>
</section>

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

<form class="filter-panel client-filter" method="get">
    <label>Recherche <input name="search" value="<?= e($search) ?>" placeholder="Nom, telephone, CIN"></label>
    <button>Filtrer</button>
</form>

<section class="table-panel">
    <table>
        <thead>
            <tr>
                <th>Locataire</th>
                <th>Mois payes</th>
                <th>Mois impayes</th>
                <th>Mois partiels</th>
                <th>Total paye</th>
                <th>Total restant</th>
                <th>Dernier paiement</th>
                <th>Situation</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($situations as $situation): ?>
                <?php
                    $unpaid = (int) $situation['unpaid_months'];
                    $partial = (int) $situation['partial_months'];
                    $remaining = (float) $situation['total_remaining'];
                    $statusClass = 'status-available';
                    $statusLabel = 'A jour';
                    if ($unpaid > 0) {
                        $statusClass = 'tenant-overdue';
                        $statusLabel = 'Impayes';
                    } elseif ($partial > 0 || $remaining > 0.001) {
                        $statusClass = 'tenant-partial';
                        $statusLabel = 'Partiel';
                    }
                ?>
                <tr>
                    <td><?= e($situation['full_name']) ?></td>
                    <td><?= (int) $situation['paid_months'] ?></td>
                    <td><?= $unpaid ?></td>
                    <td><?= $partial ?></td>
                    <td><?= number_format((float) $situation['total_paid'], 2, ',', ' ') ?></td>
                    <td><?= number_format($remaining, 2, ',', ' ') ?></td>
                    <td><?= e($situation['last_payment_date'] ?: '-') ?></td>
                    <td><span class="status-pill <?= $statusClass ?>"><?= e($statusLabel) ?></span></td>
                    <td class="actions"><a href="<?= url('/long-rentals/tenants/show?id=' . $situation['id']) ?>">Details</a></td>
                </tr>
            <?php endforeach; ?>
            <?php if (!$situations): ?><tr><td colspan="9">Aucune situation locataire.</td></tr><?php endif; ?>
        </tbody>
    </table>
</section>

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