<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 Gastos</div>
<div class="print-date">Fecha de generación: <?= date('d/m/Y H:i') ?></div>

<?php if (empty($expenses)): ?>
    <p>No hay gastos registrados.</p>
<?php else: ?>
    <table>
        <thead>
            <tr style="background-color: #f8d7da;">
                <th>Fecha</th>
                <th>Descripción</th>
                <th>Categoría</th>
                <th>Monto</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            $totalExpenses = 0;
            foreach ($expenses as $exp):
                $totalExpenses += $exp['amount'];
            ?>
                <tr>
                    <td><?= date('d/m/Y', strtotime($exp['expense_date'])) ?></td>
                    <td style="text-align: left;"><?= htmlspecialchars($exp['description']) ?></td>
                    <td><?= htmlspecialchars($exp['category'] ?? '-') ?></td>
                    <td class="text-end">$<?= number_format($exp['amount'], 2) ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr style="background-color: #343a40; color: #fff;">
                <th colspan="3" style="text-align: right;">TOTAL GENERAL DE GASTOS:</th>
                <th style="text-align: right;">$<?= number_format($totalExpenses, 2) ?></th>
            </tr>
            <?php if (!empty($expensesByCategory)): ?>
            <tr>
                <td colspan="4" style="text-align: left; background-color: #fff; border: none; padding-top: 10px; padding-bottom: 0px;">
                    <strong style="font-size: 9pt;">Gastos por Categoría:</strong>
                </td>
            </tr>
            <?php foreach ($expensesByCategory as $cat): ?>
                <tr>
                    <td colspan="3" style="text-align: right; border: none;"><?= htmlspecialchars($cat['category'] ?? 'Sin categoría') ?>:</td>
                    <td class="text-end" style="border: none;">$<?= number_format($cat['total'], 2) ?></td>
                </tr>
            <?php endforeach; ?>
            <?php endif; ?>
        </tfoot>
    </table>
<?php endif; ?>
