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

<?php
$unpaid = (int) $summary['unpaid_months'];
$partial = (int) $summary['partial_months'];
$remaining = (float) $summary['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';
}
?>

<section class="page-heading">
    <div>
        <p class="eyebrow">Situation du locataire</p>
        <h1><?= e($tenant['full_name']) ?></h1>
    </div>
    <a class="button-link secondary" href="<?= url('/long-rentals/situations') ?>">Retour</a>
</section>

<?php require __DIR__ . '/nav.php'; ?>

<section class="stat-grid compact-stats">
    <article class="stat-card"><span>Mois payes</span><strong><?= (int) $summary['paid_months'] ?></strong></article>
    <article class="stat-card"><span>Mois impayes</span><strong><?= $unpaid ?></strong></article>
    <article class="stat-card"><span>Mois partiels</span><strong><?= $partial ?></strong></article>
    <article class="stat-card"><span>Total paye</span><strong><?= number_format((float) $summary['total_paid'], 2, ',', ' ') ?></strong></article>
    <article class="stat-card"><span>Total restant</span><strong><?= number_format($remaining, 2, ',', ' ') ?></strong></article>
    <article class="stat-card"><span>Dernier paiement</span><strong class="small-stat"><?= e($summary['last_payment_date'] ?: '-') ?></strong></article>
    <article class="stat-card"><span>Situation globale</span><strong class="small-stat"><span class="status-pill <?= $statusClass ?>"><?= e($statusLabel) ?></span></strong></article>
</section>

<section class="work-panel">
    <h2>Coordonnees</h2>
    <p><?= e($tenant['phone'] ?: '-') ?> | <?= e($tenant['email'] ?: '-') ?> | <?= e($tenant['identity_number'] ?: '-') ?></p>
    <p><?= e($tenant['address'] ?: '-') ?></p>
</section>

<section class="table-panel spaced-panel">
    <table>
        <thead><tr><th>Bien</th><th>Ville</th><th>Debut</th><th>Fin</th><th>Loyer</th><th>Caution</th><th>Statut</th></tr></thead>
        <tbody>
            <?php foreach ($contracts as $contract): ?>
                <tr>
                    <td><?= e($contract['property_title']) ?></td>
                    <td><?= e($contract['property_city']) ?></td>
                    <td><?= e($contract['start_date']) ?></td>
                    <td><?= e($contract['end_date'] ?: '-') ?></td>
                    <td><?= number_format((float) $contract['monthly_rent'], 2, ',', ' ') ?></td>
                    <td><?= number_format((float) $contract['deposit_amount'], 2, ',', ' ') ?></td>
                    <td><?= e(long_contract_statuses()[$contract['status']] ?? $contract['status']) ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (!$contracts): ?><tr><td colspan="7">Aucun contrat.</td></tr><?php endif; ?>
        </tbody>
    </table>
</section>

<section class="table-panel spaced-panel">
    <table>
        <thead><tr><th>Mois</th><th>Contrat</th><th>Bien</th><th>Loyer</th><th>Paye</th><th>Restant</th><th>Date paiement</th><th>Statut</th></tr></thead>
        <tbody>
            <?php foreach ($payments as $payment): ?>
                <tr>
                    <td><?= e($payment['paid_month']) ?></td>
                    <td>#<?= (int) $payment['contract_number'] ?></td>
                    <td><?= e($payment['property_title']) ?></td>
                    <td><?= number_format((float) $payment['due_amount'], 2, ',', ' ') ?></td>
                    <td><?= number_format((float) $payment['amount_paid'], 2, ',', ' ') ?></td>
                    <td><?= number_format((float) $payment['amount_remaining'], 2, ',', ' ') ?></td>
                    <td><?= e($payment['payment_date'] ?: '-') ?></td>
                    <td><?= e(long_payment_statuses()[$payment['status']] ?? $payment['status']) ?></td>
                </tr>
            <?php endforeach; ?>
            <?php if (!$payments): ?><tr><td colspan="8">Aucun paiement.</td></tr><?php endif; ?>
        </tbody>
    </table>
</section>

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