<?php
/**
 * Rendered to HTML then passed through dompdf — same pairing and the
 * same table-based-layout constraint as invoices/folio_invoice.php (see
 * that file's doc comment for why: dompdf's float support is unreliable).
 */
$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; }
    .status-badge { display: inline-block; padding: 3px 10px; border-radius: 10px; background: <?= $primaryColor ?>; color: #fff; font-size: 11px; }
</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%;">
            Reservation Confirmation<br>
            BK-<?= sprintf('%04d', $booking->id) ?><br>
            <span class="status-badge"><?= esc(ucfirst(str_replace('_', ' ', $booking->status))) ?></span>
        </td>
    </tr>
</table>

<table class="layout-table guest-table">
    <tr>
        <td style="width: 50%;">
            <strong>Guest</strong><br>
            <?= esc($booking->guest_name) ?><br>
            <?= esc($booking->guest_phone) ?><br>
            <?= esc($booking->guest_email ?? '') ?>
        </td>
        <td style="width: 50%;">
            <strong>Stay</strong><br>
            Room <?= esc($booking->room_number) ?> — <?= esc($booking->room_type_name) ?><br>
            <?= esc(date('d M Y', strtotime($booking->check_in_date))) ?> &rarr; <?= esc(date('d M Y', strtotime($booking->check_out_date))) ?> (<?= $nights ?> night<?= $nights > 1 ? 's' : '' ?>)<br>
            <?= (int) $booking->adults ?> Adult<?= (int) $booking->adults !== 1 ? 's' : '' ?><?= $booking->children > 0 ? ', ' . (int) $booking->children . ' Child' . ($booking->children !== 1 ? 'ren' : '') : '' ?>
        </td>
    </tr>
</table>

<?php if (! empty($serviceItems)): ?>
<table class="data-table">
    <thead><tr><th>Service</th><th class="text-end">Qty</th><th class="text-end">Total</th></tr></thead>
    <tbody>
        <?php foreach ($serviceItems as $item): ?>
            <tr>
                <td><?= esc($item->service_name) ?></td>
                <td class="text-end"><?= (int) $item->quantity ?></td>
                <td class="text-end"><?= format_money(money_multiply($item->unit_price, $item->quantity)) ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<?php endif; ?>

<table class="totals-table">
    <tr><td class="label">Room Rate</td><td class="text-end"><?= format_money($roomTotal) ?></td></tr>
    <tr><td class="label">Services</td><td class="text-end"><?= format_money($servicesTotal) ?></td></tr>
    <?php if ((float) $taxRate > 0): ?><tr><td class="label">Taxes (<?= rtrim(rtrim($taxRate, '0'), '.') ?>%)</td><td class="text-end"><?= format_money($taxAmount) ?></td></tr><?php endif; ?>
    <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(date('d M Y, g:i a')) ?>
</div>

</body>
</html>
