<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;
    }
    .bg-success { background-color: #d4edda; }
    .bg-warning { background-color: #fff3cd; }
    .bg-danger { background-color: #f8d7da; }
    .bg-secondary { background-color: #e2e3e5; }
</style>

<div class="print-title">Concentrado de Pagos de Agua - <?= $year ?></div>
<div class="print-date">Fecha de generación: <?= date('d/m/Y H:i') ?></div>

<table>
    <thead>
        <tr>
            <th>Casa</th>
            <th>Estado</th>
            <?php foreach ($months as $month): ?>
                <th><?= substr($month, 0, 3) ?></th>
            <?php endforeach; ?>
            <th>Debe/Excedente</th>
            <th>Total</th>
        </tr>
    </thead>
    <tbody>
        <?php 
        $grandTotal = 0;
        $grandTotalExpected = 0;
        $monthTotals = array_fill_keys($months, 0);

        foreach ($houses as $house):
            $total = 0;
            $totalExpected = 0;
        ?>
            <tr>
                <td><strong><?= $house['number'] ?></strong></td>
                <td><?= $house['status'] == 'activa' ? 'Activa' : 'Deshabitada' ?></td>
                <?php foreach ($months as $month):
                    $payment = $payments[$month][$house['id']] ?? null;
                    $amount = $payment['amount'] ?? 0;
                    $monthTotals[$month] += $amount;

                    $expected = Payment::getExpectedAmount($house, $year, $month);
                    $total += $amount;
                    $totalExpected += $expected;

                    $bg_color = '#FFFFFF'; // Default white
                    if ($house['status'] == 'deshabitada') {
                        $bg_color = '#e2e3e5'; // Gris
                    } elseif ($amount > 0) {
                        if ($expected > 0 && $amount >= $expected) {
                            $bg_color = '#d4edda'; // Verde
                        } else {
                            $bg_color = '#fff3cd'; // Amarillo
                        }
                    } elseif ($amount == 0 && $expected > 0) {
                        $bg_color = '#f8d7da'; // Rojo
                    }
                ?>
                    <td style="background-color: <?= $bg_color ?>;">
                        <?= $amount > 0 ? '$' . number_format($amount, 2) : '-' ?>
                    </td>
                <?php endforeach; ?>
                <?php
                    $difference = $total - $totalExpected;
                    $diffColor = $difference < 0 ? 'red' : 'green';
                    $diffText = $difference == 0 ? '$0.00' : '$' . number_format($difference, 2);
                    $grandTotal += $total;
                    $grandTotalExpected += $totalExpected;
                ?>
                <td style="color: <?= $diffColor ?>;"><?= $diffText ?></td>
                <td><strong>$<?= number_format($total, 2) ?></strong></td>
            </tr>
        <?php endforeach; ?>
            <tr style="background-color: #bee5eb;">
                <td colspan="2" style="text-align: right; font-weight: bold;">SUMA MENSUAL:</td>
                <?php foreach ($months as $month): ?>
                    <td style="text-align: center; font-weight: bold;">
                        $<?= number_format($monthTotals[$month], 2) ?>
                    </td>
                <?php endforeach; ?>
                <td colspan="2"></td>
            </tr>
        <?php
            $grandDifference = $grandTotal - $grandTotalExpected;
            $grandDiffClass = $grandDifference < 0 ? 'text-danger' : 'text-success';
            $grandDiffText = $grandDifference == 0 ? '$0.00' : '$' . number_format($grandDifference, 2);
        ?>
            <tr>
                <td colspan="<?= count($months) + 2 ?>" style="text-align: right; font-weight: bold;">TOTALES:</td>
                <td style="text-align: center; font-weight: bold; color: <?= $grandDiffClass == 'text-danger' ? 'red' : 'green' ?>;"><?= $grandDiffText ?></td>
                <td style="text-align: center; font-weight: bold;">$<?= number_format($grandTotal, 2) ?></td>
            </tr>
    </tbody>
</table>

<div style="margin-top: 20px; font-size: 8pt; page-break-inside: avoid;">
    <strong>Leyenda:</strong>
    <span style="background-color: #d4edda; padding: 2px 8px; margin: 2px; border: 1px solid #ccc;">Verde = Pagado completo</span>
    <span style="background-color: #fff3cd; padding: 2px 8px; margin: 2px; border: 1px solid #ccc;">Amarillo = Pago parcial</span>
    <span style="background-color: #f8d7da; padding: 2px 8px; margin: 2px; border: 1px solid #ccc;">Rojo = Sin pago</span>
    <span style="background-color: #e2e3e5; padding: 2px 8px; margin: 2px; border: 1px solid #ccc;">Gris = Casa deshabitada</span>
</div>