<?php
/**
 * Rendered to HTML then passed through dompdf (see ReportController::
 * exportPdf()) — NOT a normal app screen, doesn't extend layouts/main.
 * Table-based layout throughout — see invoices/folio_invoice.php's doc
 * comment for why (dompdf's float support is unreliable).
 */
$primaryColor = safe_hex_color($tenant->primary_color ?? null, '#0d6efd');

$catLabels = [
    'room_charge' => 'Rooms',
    'restaurant'  => 'F&B',
    'event'       => 'Events',
    'other'       => 'Other',
];
?>
<!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; }
    .kpi-table { margin-bottom: 20px; }
    .kpi-table td { text-align: center; border: 1px solid #ddd; padding: 10px; }
    .kpi-table .label { display: block; font-size: 10px; text-transform: uppercase; color: #888; margin-bottom: 4px; }
    .kpi-table .value { display: block; font-size: 16px; font-weight: bold; }
    .data-table { margin-bottom: 20px; }
    .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; }
    h2 { font-size: 13px; margin: 0 0 8px; color: #333; }
    .footer { margin-top: 20px; 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><br>
            Performance Report
        </td>
        <td class="meta" style="width: 40%;">
            <?= esc(date('d M Y', strtotime($dateFrom))) ?> &ndash; <?= esc(date('d M Y', strtotime($dateTo))) ?><br>
            Generated: <?= esc($issuedAt) ?>
        </td>
    </tr>
</table>

<table class="kpi-table">
    <tr>
        <td><span class="label">Avg Occupancy</span><span class="value"><?= $kpis['occupancy'] ?>%</span></td>
        <td><span class="label">ADR</span><span class="value"><?= format_money($kpis['adr']) ?></span></td>
        <td><span class="label">RevPAR</span><span class="value"><?= format_money($kpis['revpar']) ?></span></td>
        <td><span class="label">Total Revenue</span><span class="value"><?= format_money($kpis['revenue']) ?></span></td>
    </tr>
    <tr>
        <td><span class="label">Repeat Guest Rate</span><span class="value"><?= $kpis['repeatGuestRate'] ?>%</span></td>
        <td><span class="label">Avg Spend / Guest</span><span class="value"><?= format_money($kpis['avgSpendPerGuest']) ?></span></td>
        <td></td>
        <td></td>
    </tr>
</table>

<h2>Daily Occupancy &amp; Revenue</h2>
<table class="data-table">
    <thead><tr><th>Date</th><th class="text-end">Occupancy %</th><th class="text-end">Revenue</th></tr></thead>
    <tbody>
        <?php foreach ($occupancyTrend as $day => $pct): ?>
            <tr>
                <td><?= esc(date('d M Y', strtotime($day))) ?></td>
                <td class="text-end"><?= $pct ?>%</td>
                <td class="text-end"><?= format_money($revenueTrend[$day] ?? '0.00') ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<h2>Revenue by Category</h2>
<table class="data-table">
    <thead><tr><th>Category</th><th class="text-end">Amount</th></tr></thead>
    <tbody>
        <?php foreach ($catLabels as $key => $label): ?>
            <tr>
                <td><?= esc($label) ?></td>
                <td class="text-end"><?= format_money($categoryTotals[$key] ?? '0.00') ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<h2>Top Room Types by Booked Revenue</h2>
<table class="data-table">
    <thead><tr><th>Room Type</th><th class="text-end">Booked Revenue</th></tr></thead>
    <tbody>
        <?php if (empty($roomTypeRevenue)): ?>
            <tr><td colspan="2">No bookings in this range.</td></tr>
        <?php endif; ?>
        <?php foreach ($roomTypeRevenue as $type => $amount): ?>
            <tr>
                <td><?= esc($type) ?></td>
                <td class="text-end"><?= format_money($amount) ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

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

</body>
</html>
