<style>
    table {
        width: 100%;
        border-collapse: collapse;
        font-size: 8pt;
    }
    th, td {
        border: 1px solid #000;
        padding: 4px;
        text-align: center;
    }
    th {
        background-color: #eee;
    }
    .print-title {
        text-align: center;
        font-size: 14pt;
        margin-bottom: 10px;
    }
    .print-date {
        text-align: right;
        font-size: 8pt;
        margin-bottom: 10px;
    }
    .text-danger {
        color: red;
    }
    .text-success {
        color: green;
    }
</style>

<div class="print-title">Condominio IBIZA-Cto Sierra Morena 152 - Reporte de Deudores de Agua <?= $year ?></div>
<div class="print-date">Fecha de generación: <?= date('d/m/Y H:i') ?></div>

<?php
$hasFilters = !empty($waterDebtors['filters']['year']) || !empty($waterDebtors['filters']['months']) || !empty($waterDebtors['filters']['house_id']);
$filterText = [];
if (!empty($waterDebtors['filters']['year'])) {
    $filterText[] = "Año: " . $waterDebtors['filters']['year'];
}
if (!empty($waterDebtors['filters']['months'])) {
    $filterText[] = "Meses: " . implode(', ', $waterDebtors['filters']['months']);
}
if (!empty($waterDebtors['filters']['house_id'])) {
    require_once __DIR__ . '/../../models/House.php';
    $house = House::findById($waterDebtors['filters']['house_id']);
    $filterText[] = "Casa: " . ($house['number'] ?? 'N/A');
}
if ($hasFilters):
?>
<div style="font-size: 9pt; margin-bottom: 10px;">
    <strong>Filtros aplicados:</strong> <?= implode(' | ', $filterText) ?>
</div>
<?php endif; ?>

<?php if (empty($waterDebtors['debtors'])): ?>
    <p>No hay deudores de agua registrados con los filtros actuales.</p>
<?php else: ?>
    <table>
        <thead>
            <tr style="background-color: #f8d7da;">
                <th>Casa</th>
                <th>Propietario</th>
                <th>Meses Adeudados</th>
                <th>Total Debe</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($waterDebtors['debtors'] as $debtor): ?>
                <tr>
                    <td><strong><?= $debtor['house_number'] ?></strong></td>
                    <td><?= htmlspecialchars($debtor['owner_name'] ?? '-') ?></td>
                    <td>
                        <table style="width:100%; border: none;">
                            <?php foreach ($debtor['months_due'] as $month): ?>
                                <tr>
                                    <td style="border: none; text-align: left;"><?= $month['year'] ?> - <?= $month['month'] ?></td>
                                    <td style="border: none; text-align: right;">$<?= number_format($month['due'], 2) ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </table>
                    </td>
                    <td class="text-end fw-bold text-danger">$<?= number_format($debtor['total_due'], 2) ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr style="background-color: #343a40; color: #fff;">
                <th colspan="3" style="text-align: right;">TOTAL GENERAL:</th>
                <th style="text-align: right;">$<?= number_format($waterDebtors['total_due'], 2) ?></th>
            </tr>
        </tfoot>
    </table>
<?php endif; ?>
