<?php
/** @var array $quote */
/** @var array $tenant */
/** @var array $items */
$c1 = $tenant['brand_color_1'] ?? '#0b2545';
$c2 = $tenant['brand_color_2'] ?? '#1a4d8c';
$companyName = \Core\Settings::get('company_name') ?: APP_NAME;
$logo = url('/public/static/img/logo.png');
// Convert logo to base64 for PDF embedding
$logoData = '';
$logoPath = ROOT . '/public/static/img/logo.png';
if (file_exists($logoPath)) {
    $logoData = 'data:image/png;base64,' . base64_encode(file_get_contents($logoPath));
}
?>
<style>
    body { font-family: 'Helvetica', 'Arial', sans-serif; color: #1e2d3d; font-size: 10pt; margin: 0; padding: 0; }
    .header { background: <?= $c1 ?>; color: #fff; padding: 12mm 15mm 8mm; margin: -10mm -10mm 6mm; }
    .header .brand { font-size: 20pt; font-weight: 800; letter-spacing: 1px; }
    .header .brand img { height: 28px; vertical-align: middle; margin-right: 8px; }
    .header .sub { font-size: 9pt; opacity: 0.85; margin-top: 2mm; text-transform: uppercase; letter-spacing: 2px; }
    .header .doctype { font-size: 14pt; font-weight: 700; margin-top: 4mm; color: #fff; }
    .doc-number { font-size: 22pt; font-weight: 800; color: <?= $c2 ?>; margin: 4mm 0 6mm; }
    h2 { font-size: 11pt; margin: 5mm 0 2mm; color: <?= $c1 ?>; border-bottom: 2px solid <?= $c2 ?>; padding-bottom: 1mm; text-transform: uppercase; letter-spacing: 1px; }
    table.detail { width: 100%; border-collapse: collapse; margin: 2mm 0 4mm; }
    table.detail td { padding: 1.5mm 3mm; vertical-align: top; font-size: 9pt; }
    table.detail td.k { color: #6b7a8d; width: 20%; text-transform: uppercase; font-size: 7pt; letter-spacing: .5px; }
    table.detail td.v { color: #1e2d3d; font-weight: 600; }
    table.items { width: 100%; border-collapse: collapse; margin-top: 3mm; }
    table.items th { background: <?= $c1 ?>; color: #fff; padding: 2mm 3mm; font-size: 8pt; text-transform: uppercase; letter-spacing: .5px; }
    table.items td { border-bottom: 1px solid #e2e8f0; padding: 2mm 3mm; font-size: 9pt; }
    table.items td.num { text-align: right; }
    table.items td.cent { text-align: center; }
    .totals { margin-top: 4mm; text-align: right; }
    .totals table { margin-left: auto; width: 45%; border-collapse: collapse; }
    .totals td { padding: 1.5mm 4mm; font-size: 10pt; }
    .totals td.lbl { color: #6b7a8d; }
    .totals td.val { font-weight: 600; text-align: right; }
    .totals tr.gt td { font-size: 14pt; font-weight: 800; color: <?= $c1 ?>; border-top: 2px solid <?= $c1 ?>; padding-top: 3mm; }
    .validity { margin-top: 4mm; padding: 3mm; background: #f8fafc; border-left: 3px solid <?= $c2 ?>; font-size: 8pt; }
    .foot { margin-top: 10mm; padding-top: 3mm; border-top: 1px solid #e2e8f0; color: #94a3b8; font-size: 7pt; text-align: center; }
</style>

<div class="header">
    <div class="brand"><?php if ($logoData): ?><img src="<?= $logoData ?>" alt=""><?php endif; ?><?= e($companyName) ?></div>
    <div class="sub"><?= e($tenant['name'] ?? '') ?></div>
    <div class="doctype">QUOTATION</div>
</div>

<div class="doc-number"><?= e($quote['quote_number']) ?></div>

<table style="width:100%;">
    <tr>
        <td style="width:55%;vertical-align:top;">
            <h2>Prepared For</h2>
            <table class="detail">
                <tr><td class="k">Customer</td><td class="v"><?= e($quote['customer_name'] ?? '—') ?></td></tr>
                <tr><td class="k">Contact</td><td class="v"><?= e($quote['customer_contact'] ?? '—') ?></td></tr>
                <tr><td class="k">Email</td><td class="v"><?= e($quote['customer_email'] ?? '—') ?></td></tr>
                <tr><td class="k">Phone</td><td class="v"><?= e($quote['customer_phone'] ?? '—') ?></td></tr>
                <tr><td class="k">Address</td><td class="v"><?= e($quote['customer_address'] ?? '—') ?></td></tr>
                <tr><td class="k">Tax ID</td><td class="v"><?= e($quote['customer_tax_id'] ?? '—') ?></td></tr>
            </table>
        </td>
        <td style="width:45%;vertical-align:top;">
            <h2>Quote Details</h2>
            <table class="detail">
                <tr><td class="k">Status</td><td class="v"><?= e(strtoupper($quote['status'])) ?></td></tr>
                <tr><td class="k">Date</td><td class="v"><?= e(date('d M Y', strtotime($quote['created_at'] ?? ''))) ?></td></tr>
                <tr><td class="k">Valid Until</td><td class="v"><?= e($quote['valid_until'] ? date('d M Y', strtotime($quote['valid_until'])) : '—') ?></td></tr>
            </table>
            <?php if (!empty($quote['valid_until']) && strtotime($quote['valid_until']) < time()): ?>
                <div class="validity" style="color:#dc2626; border-left-color:#dc2626;">
                    ⚠ This quotation has expired on <?= e(date('d M Y', strtotime($quote['valid_until']))) ?>
                </div>
            <?php else: ?>
                <div class="validity">
                    This quotation is valid until <?= e($quote['valid_until'] ? date('d M Y', strtotime($quote['valid_until'])) : 'further notice') ?>.
                </div>
            <?php endif; ?>
        </td>
    </tr>
</table>

<h2>Line Items</h2>
<table class="items">
    <thead>
        <tr><th style="width:6%;">#</th><th>Description</th><th style="width:10%;">Qty</th><th style="width:14%;">Unit Price</th><th style="width:10%;">Disc</th><th style="width:15%;">Total</th></tr>
    </thead>
    <tbody>
        <?php foreach ($items as $i => $it): ?>
            <tr>
                <td style="text-align:center;"><?= $i + 1 ?></td>
                <td><?= e($it['description']) ?></td>
                <td class="num"><?= rtrim(rtrim(number_format((float) $it['quantity'], 2), '0'), '.') ?></td>
                <td class="num">TZS <?= number_format((float) $it['unit_price'], 2) ?></td>
                <td class="cent"><?= (float) $it['discount_percent'] ?>%</td>
                <td class="num">TZS <?= number_format((float) $it['line_total'], 2) ?></td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<div class="totals">
    <table>
        <tr><td class="lbl">Subtotal</td><td class="val">TZS <?= number_format((float) $quote['subtotal'], 2) ?></td></tr>
        <?php if ((float) $quote['discount_percent'] > 0): ?>
        <tr><td class="lbl">Discount (<?= (float) $quote['discount_percent'] ?>%)</td><td class="val">-TZS <?= number_format((float) $quote['discount_amount'], 2) ?></td></tr>
        <?php endif; ?>
        <?php if ((float) $quote['tax_total'] > 0): ?>
        <tr><td class="lbl">VAT (18%)</td><td class="val">TZS <?= number_format((float) $quote['tax_total'], 2) ?></td></tr>
        <?php endif; ?>
        <tr class="gt"><td class="lbl">GRAND TOTAL</td><td class="val">TZS <?= number_format((float) $quote['grand_total'], 2) ?></td></tr>
    </table>
</div>

<?php if (!empty($quote['notes'])): ?>
    <h2>Notes</h2>
    <p style="font-size:9pt; color:#555;"><?= nl2br(e($quote['notes'])) ?></p>
<?php endif; ?>
<?php if (!empty($quote['terms'])): ?>
    <h2>Terms & Conditions</h2>
    <p style="font-size:9pt; color:#555;"><?= nl2br(e($quote['terms'])) ?></p>
<?php endif; ?>

<div class="foot"><?= e($companyName) ?> &mdash; Generated on <?= date('d M Y, H:i') ?> &mdash; This is a computer-generated document</div>
