<?php
/**
 * Rendered to HTML then passed through dompdf (see InvoiceController) —
 * NOT a normal app screen, so it does NOT extend layouts/main. Dompdf's
 * CSS support is a weak CSS2.1 subset: `float` reliably fails to clear
 * before subsequent block content (text visibly overlaps the next
 * element instead of stacking), so this layout is deliberately
 * table-based throughout rather than using float/flex/grid anywhere.
 *
 * Logo: expects $tenant->logo_path to be a path under writable/uploads/
 * (no upload UI exists yet to actually set this in Phase 1-3 — falls back
 * to a plain text hotel name header, which is what every tenant gets today).
 *
 * safe_hex_color(), not esc(..., 'css') — see app/Helpers/theme_helper.php.
 */
$primaryColor = safe_hex_color($tenant->primary_color ?? null, '#0d6efd');
$logoDataUri  = null;

if (! empty($tenant->logo_path)) {
    $logoFile = WRITEPATH . 'uploads/' . ltrim($tenant->logo_path, '/');

    if (is_file($logoFile)) {
        $mime = guess_mime_type($logoFile);
        $logoDataUri = 'data:' . $mime . ';base64,' . base64_encode(file_get_contents($logoFile));
    }
}
?>
<!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; }
    .header-table img { max-height: 50px; }
    .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-table { 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%;">
            <?php if ($logoDataUri): ?><img src="<?= $logoDataUri ?>" alt="Logo"><br><?php endif; ?>
            <span class="hotel"><?= esc($tenant->hotel_name) ?></span>
        </td>
        <td class="meta" style="width: 40%;">
            Invoice &mdash; Folio #<?= $folio->id ?><br>
            Issued: <?= esc($issuedAt) ?>
        </td>
    </tr>
</table>

<table class="layout-table guest-table">
    <tr>
        <td style="width: 50%;">
            <strong>Billed To</strong><br>
            <?= esc($guest->full_name ?? 'Guest') ?><br>
            <?= esc($guest->phone ?? '') ?><br>
            <?= esc($guest->email ?? '') ?>
        </td>
        <td style="width: 50%;">
            <strong>Folio Status</strong><br>
            <?= esc(ucfirst($folio->status)) ?><br>
            Opened: <?= esc($folio->opened_at) ?><br>
            <?php if ($folio->closed_at): ?>Closed: <?= esc($folio->closed_at) ?><br><?php endif; ?>
        </td>
    </tr>
</table>

<table class="data-table">
    <thead>
        <tr><th>Date</th><th>Description</th><th>Category</th><th class="text-end">Amount</th></tr>
    </thead>
    <tbody>
        <?php foreach ($lineItems as $item): ?>
            <tr>
                <td><?= esc($item->item_date) ?></td>
                <td><?= esc($item->description) ?></td>
                <td><?= esc(ucfirst(str_replace('_', ' ', $item->category))) ?></td>
                <td class="text-end"><?= format_money($item->amount) ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<?php if (! empty($payments)): ?>
<table class="data-table">
    <thead>
        <tr><th>Payment Date</th><th>Method</th><th class="text-end">Amount</th></tr>
    </thead>
    <tbody>
        <?php foreach ($payments as $payment): ?>
            <tr>
                <td><?= esc($payment->paid_at) ?></td>
                <td><?= esc(ucfirst(str_replace('_', ' ', $payment->method))) ?></td>
                <td class="text-end"><?= format_money($payment->amount) ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<?php endif; ?>

<table class="totals-table">
    <tr><td class="label">Total Charges</td><td class="text-end"><?= format_money($total) ?></td></tr>
    <tr><td class="label">Amount Paid</td><td class="text-end"><?= format_money($paid) ?></td></tr>
    <tr class="grand"><td class="label">Balance Due</td><td class="text-end"><?= format_money($balance) ?></td></tr>
</table>

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

</body>
</html>
