<?php
/**
 * Rendered to HTML then passed through dompdf (see Admin\InvoiceController
 * ::pdf()) — NOT a normal app screen, so it does NOT extend admin/layouts/
 * main. Table-based layout throughout, same reasoning as
 * app/Views/invoices/folio_invoice.php: dompdf's float support is
 * unreliable enough to cause literal text overlap.
 *
 * This is the PLATFORM billing the hotel (fixed MaxYields branding), the
 * mirror image of folio_invoice.php (a hotel billing its own guest) —
 * so unlike that template, there's no per-tenant primary_color to read.
 */
?>
<!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 #4f46e5; padding-bottom: 12px; margin-bottom: 20px; }
    .header-table .brand { font-size: 20px; font-weight: bold; color: #4f46e5; }
    .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; }
    /* A fixed-width table with margin-left: auto (used in
       invoices/folio_invoice.php) pushed 2 blank trailing pages here —
       dompdf mis-sizing the auto margin against the page box rather than
       the containing block. Sidestepped by putting the totals in the
       right-hand cell of a full-width two-column row instead, which
       needs no auto margin at all. */
    .totals-wrap td { border: none; padding: 0; }
    .totals-table { width: 100%; }
    .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; }
    .bill-table { margin-bottom: 20px; }
    .status-pill { display: inline-block; padding: 2px 10px; border-radius: 10px; background: #eef2ff; color: #4f46e5; font-size: 10px; text-transform: uppercase; font-weight: bold; }
    .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="brand">MaxYields PMS</span><br>
            <span style="font-size: 10px; color: #888;">Hotel Property Management Platform</span>
        </td>
        <td class="meta" style="width: 40%;">
            Invoice <?= esc($invoice->invoice_number) ?><br>
            Issued: <?= esc($issuedAt) ?><br>
            <span class="status-pill"><?= esc(ucfirst($invoice->status)) ?></span>
        </td>
    </tr>
</table>

<table class="layout-table bill-table">
    <tr>
        <td style="width: 50%;">
            <strong>Billed To</strong><br>
            <?= esc($tenant->hotel_name) ?><br>
            <?= esc($tenant->contact_email ?? '') ?><br>
            <?= esc($tenant->contact_phone ?? '') ?>
        </td>
        <td style="width: 50%;">
            <strong>Details</strong><br>
            Due: <?= $invoice->due_date ? esc(date('d M Y', strtotime($invoice->due_date))) : '—' ?><br>
            <?php if ($invoice->paid_at): ?>Paid: <?= esc(date('d M Y', strtotime($invoice->paid_at))) ?><br><?php endif; ?>
        </td>
    </tr>
</table>

<table class="data-table">
    <thead>
        <tr><th>Description</th><th class="text-end">Amount</th></tr>
    </thead>
    <tbody>
        <?php foreach ($lineItems as $line): ?>
            <tr>
                <td><?= esc($line->description) ?></td>
                <td class="text-end"><?= format_money($line->amount) ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<table class="layout-table totals-wrap">
    <tr>
        <td style="width: 60%;"></td>
        <td style="width: 40%;">
            <table class="totals-table">
                <tr class="grand"><td class="label">Total Due</td><td class="text-end"><?= format_money($invoice->amount) ?></td></tr>
            </table>
        </td>
    </tr>
</table>

<div class="footer">
    MaxYields PMS &mdash; Generated on <?= esc($issuedAt) ?>. Questions about this invoice? Contact your MaxYields account manager.
</div>

</body>
</html>
