<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">Concentrado de Pagos de Luz - Cámara -
    <?= $year?>
</div>
<div class="print-date">Fecha de generación:
    <?= date('d/m/Y H:i')?>
</div>

<table>
    <thead>
        <tr>
            <th width="10%">Casa</th>
            <th width="10%">Estado</th>
            <?php foreach ($periods as $period):
    $config = $electricityBills[$period] ?? [];
    $amountPerHouse = $config['amount_per_house'] ?? 0;
?>
            <th>
                <?= $period?><br><small>$
                    <?= number_format($amountPerHouse, 2)?>
                </small>
            </th>
            <?php
endforeach; ?>
            <th width="15%">Total</th>
        </tr>
    </thead>
    <tbody>
        <?php
$grandTotal = 0;
$periodTotals = array_fill_keys($periods, 0);

foreach ($matrix['houses'] as $house):
    // Filtrar solo casas permitidas (aunque el controlador ya debió filtrar)
    if (!Auth::canViewHouse($house['id']))
        continue;

    $houseTotal = 0;
?>
        <tr>
            <td><strong>
                    <?= $house['number']?>
                </strong></td>
            <td>
                <?= $house['status'] == 'activa' ? 'Activa' : 'Deshabitada'?>
            </td>
            <?php foreach ($periods as $period):
        $payment = $matrix['payments'][$period][$house['id']] ?? null;
        $amount = $payment['amount'] ?? 0;
        $periodTotals[$period] += $amount;
        $houseTotal += $amount;

        $config = $electricityBills[$period] ?? [];
        $expected = $config['amount_per_house'] ?? 0;

        $bg_color = '#FFFFFF';
        // Lógica de colores idéntica a la vista web para consistencia
        if ($amount > 0) {
            if ($expected > 0 && $amount >= $expected) {
                $bg_color = '#d4edda'; // Verde (paid)
            }
            else {
                $bg_color = '#fff3cd'; // Amarillo (partial)
            }
        }
        else {
            if ($expected > 0) {
                $bg_color = '#f8d7da'; // Rojo (pending)
            }
            elseif ($house['status'] == 'deshabitada') {
                $bg_color = '#e2e3e5'; // Gris (inactive)
            }
        }
?>
            <td style="background-color: <?= $bg_color?>;">
                <?= $amount > 0 ? '$' . number_format($amount, 2) : '-'?>
            </td>
            <?php
    endforeach; ?>

            <td><strong>$
                    <?= number_format($houseTotal, 2)?>
                </strong></td>
        </tr>
        <?php
    $grandTotal += $houseTotal;
endforeach;
?>
        <tr style="background-color: #bee5eb;">
            <td colspan="2" style="text-align: right; font-weight: bold;">TOTALES:</td>
            <?php foreach ($periods as $period): ?>
            <td style="text-align: center; font-weight: bold;">
                $
                <?= number_format($periodTotals[$period], 2)?>
            </td>
            <?php
endforeach; ?>
            <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</span>
    <span style="background-color: #fff3cd; padding: 2px 8px; margin: 2px; border: 1px solid #ccc;">Amarillo =
        Parcial</span>
    <span style="background-color: #f8d7da; padding: 2px 8px; margin: 2px; border: 1px solid #ccc;">Rojo =
        Pendiente</span>
    <span style="background-color: #e2e3e5; padding: 2px 8px; margin: 2px; border: 1px solid #ccc;">Gris =
        Inactivo</span>
</div>