<?php
/**
 * Monthly statement PDF for a corporate account — aggregates every
 * folio_line_item charged to folios tagged with this account, dated
 * within the statement month (see CorporateAccountModel::chargesForMonth()).
 *
 * Table-based layout throughout — see folio_invoice.php's comment on why
 * float is avoided (dompdf doesn't clear it reliably; text overlaps).
 *
 * safe_hex_color(), not esc(..., 'css') — see app/Helpers/theme_helper.php.
 */
$primaryColor = safe_hex_color($tenant->primary_color ?? null, '#0d6efd');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
    body { font-family: Helvetica, Arial, sans-serif; font-size: 12px; color: #222; }
    table { width: 100%; border-collapse: collapse; }
    .layout-table td { border: none; padding: 0; vertical-align: top; }
    .header-table { border-bottom: 3px solid <?= $primaryColor ?>; padding-bottom: 12px; margin-bottom: 20px; }
    .header-table .hotel { font-size: 20px; font-weight: bold; color: <?= $primaryColor ?>; }
    .header-table .meta { text-align: right; font-size: 11px; color: #555; }
    .data-table { margin-bottom: 16px; }
    .data-table th, .data-table td { padding: 6px 8px; text-align: left; border-bottom: 1px solid #ddd; font-size: 11px; }
    .data-table th { background: #f4f4f4; text-transform: uppercase; font-size: 10px; letter-spacing: 0.5px; }
    .text-end { text-align: right; }
    .totals-table { width: 300px; margin-left: auto; }
    .totals-table td { border: none; padding: 3px 8px; }
    .totals-table .label { text-align: right; color: #555; }
    .totals-table .grand { font-size: 14px; font-weight: bold; border-top: 2px solid #333; }
    .guest-block { margin-bottom: 20px; }
    .footer { margin-top: 30px; font-size: 10px; color: #888; border-top: 1px solid #ddd; padding-top: 8px; }
</style>
</head>
<body>

<table class="layout-table header-table">
    <tr>
        <td style="width: 60%;"><span class="hotel"><?= esc($tenant->hotel_name) ?></span></td>
        <td class="meta" style="width: 40%;">
            Monthly Statement &mdash; <?= esc($monthLabel) ?><br>
            Issued: <?= esc($issuedAt) ?>
        </td>
    </tr>
</table>

<div class="guest-block">
    <strong>Billed To</strong><br>
    <?= esc($account->company_name) ?><br>
    <?php if ($account->contact_name): ?><?= esc($account->contact_name) ?><br><?php endif; ?>
    <?php if ($account->billing_address): ?><?= nl2br(esc($account->billing_address)) ?><br><?php endif; ?>
    <?php if ($account->email): ?><?= esc($account->email) ?><?php endif; ?>
</div>

<table class="data-table">
    <thead>
        <tr><th>Date</th><th>Folio</th><th>Guest</th><th>Description</th><th>Category</th><th class="text-end">Amount</th></tr>
    </thead>
    <tbody>
        <?php if (empty($charges)): ?>
            <tr><td colspan="6" style="text-align:center; color:#888;">No charges this month.</td></tr>
        <?php endif; ?>
        <?php foreach ($charges as $charge): ?>
            <tr>
                <td><?= esc($charge->item_date) ?></td>
                <td>#<?= esc($charge->folio_id) ?></td>
                <td><?= esc($charge->guest_name) ?></td>
                <td><?= esc($charge->description) ?></td>
                <td><?= esc(ucfirst(str_replace('_', ' ', $charge->category))) ?></td>
                <td class="text-end"><?= format_money($charge->amount) ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<table class="totals-table">
    <tr class="grand"><td class="label">Total for <?= esc($monthLabel) ?></td><td class="text-end"><?= format_money($totalAmount) ?></td></tr>
</table>

<?php if (! empty($aiSummary)): ?>
<table class="layout-table" style="margin-top: 16px; border-top: 1px solid #ddd; padding-top: 10px;">
    <tr><td style="font-size: 10px; color: #888; text-transform: uppercase; letter-spacing: 0.5px; padding-bottom: 4px;">Trend Note</td></tr>
    <tr><td style="font-size: 11px; color: #444;"><?= esc($aiSummary) ?></td></tr>
</table>
<?php endif; ?>

<div class="footer">
    <?= esc($tenant->hotel_name) ?> &mdash; Generated by MaxYields PMS on <?= esc($issuedAt) ?>
</div>

</body>
</html>
