<?php
/** @var array $invoice */
/** @var array $tenant */
/** @var array $items */
/** @var array $payments */
$c1 = $tenant['brand_color_1'] ?? '#0b2545';
$c2 = $tenant['brand_color_2'] ?? '#1a4d8c';
$companyName = \Core\Settings::get('company_name') ?: APP_NAME;
$overdue = (float) ($invoice['balance_due'] ?? 0) > 0 && !empty($invoice['due_date']) && strtotime($invoice['due_date']) < time();
$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: 50%; 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: 13pt; font-weight: 800; color: <?= $c1 ?>; border-top: 2px solid <?= $c1 ?>; padding-top: 3mm; }
    .totals tr.bal td { font-size: 14pt; font-weight: 800; color: <?= $overdue ? '#dc2626' : '#16a34a' ?>; }
    .paid-stamp { position: absolute; top: 30mm; right: 15mm; border: 4px solid #16a34a; color: #16a34a; font-size: 28pt; font-weight: 800; padding: 3mm 8mm; transform: rotate(-12deg); opacity: 0.7; border-radius: 4mm; }
    .overdue-box { background: #fef2f2; border: 2px solid #fecaca; padding: 4mm; margin-top: 3mm; border-radius: 2mm; text-align: center; }
    .overdue-box .txt { color: #dc2626; font-weight: 800; font-size: 12pt; }
    table.pmts { width: 100%; border-collapse: collapse; margin-top: 2mm; }
    table.pmts th { background: #f1f5f9; color: #475569; padding: 1.5mm 3mm; font-size: 7pt; text-transform: uppercase; letter-spacing: .5px; }
    table.pmts td { border-bottom: 1px solid #e2e8f0; padding: 1.5mm 3mm; font-size: 8pt; }
    table.pmts td.num { text-align: right; }
    .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">TAX INVOICE</div>
</div>

<?php if ((float) ($invoice['balance_due'] ?? 0) <= 0 && (float) ($invoice['grand_total'] ?? 0) > 0): ?>
    <div class="paid-stamp">PAID</div>
<?php endif; ?>

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

<table style="width:100%;">
    <tr>
        <td style="width:50%;vertical-align:top;">
            <h2>Bill To</h2>
            <table class="detail">
                <tr><td class="k">Customer</td><td class="v"><?= e($invoice['customer_name'] ?? '—') ?></td></tr>
                <tr><td class="k">Contact</td><td class="v"><?= e($invoice['customer_contact'] ?? '—') ?></td></tr>
                <tr><td class="k">Email</td><td class="v"><?= e($invoice['customer_email'] ?? '—') ?></td></tr>
                <tr><td class="k">Phone</td><td class="v"><?= e($invoice['customer_phone'] ?? '—') ?></td></tr>
                <tr><td class="k">Address</td><td class="v"><?= e($invoice['customer_address'] ?? '—') ?></td></tr>
                <tr><td class="k">Tax ID</td><td class="v"><?= e($invoice['customer_tax_id'] ?? '—') ?></td></tr>
            </table>
        </td>
        <td style="width:50%;vertical-align:top;">
            <h2>Invoice Details</h2>
            <table class="detail">
                <tr><td class="k">Status</td><td class="v"><?= e(strtoupper($invoice['status'])) ?></td></tr>
                <tr><td class="k">Issue Date</td><td class="v"><?= e(date('d M Y', strtotime($invoice['created_at'] ?? ''))) ?></td></tr>
                <tr><td class="k">Due Date</td><td class="v"><?= e($invoice['due_date'] ? date('d M Y', strtotime($invoice['due_date'])) : '—') ?></td></tr>
            </table>
            <?php if ($overdue): ?>
                <div class="overdue-box">
                    <div class="txt">⚠ OVERDUE</div>
                    <div style="font-size:8pt;color:#991b1b;">Payment of TZS <?= number_format((float) $invoice['balance_due'], 2) ?> is past due</div>
                </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) $invoice['subtotal'], 2) ?></td></tr>
        <?php if ((float) $invoice['discount_percent'] > 0): ?>
        <tr><td class="lbl">Discount (<?= (float) $invoice['discount_percent'] ?>%)</td><td class="val">-TZS <?= number_format((float) $invoice['discount_amount'], 2) ?></td></tr>
        <?php endif; ?>
        <?php if ((float) $invoice['tax_total'] > 0): ?>
        <tr><td class="lbl">VAT (18%)</td><td class="val">TZS <?= number_format((float) $invoice['tax_total'], 2) ?></td></tr>
        <?php endif; ?>
        <tr class="gt"><td class="lbl">TOTAL</td><td class="val">TZS <?= number_format((float) $invoice['grand_total'], 2) ?></td></tr>
        <tr><td class="lbl">Amount Paid</td><td class="val">TZS <?= number_format((float) $invoice['amount_paid'], 2) ?></td></tr>
        <tr class="bal"><td class="lbl">Balance Due</td><td class="val">TZS <?= number_format((float) $invoice['balance_due'], 2) ?></td></tr>
    </table>
</div>

<?php if (!empty($payments)): ?>
    <h2>Payment History</h2>
    <table class="pmts">
        <thead>
            <tr><th>Date</th><th>Method</th><th>Reference</th><th class="num">Amount</th></tr>
        </thead>
        <tbody>
            <?php foreach ($payments as $pmt): ?>
                <tr>
                    <td><?= e($pmt['payment_date']) ?></td>
                    <td><?= e(str_replace('_', ' ', $pmt['payment_method'])) ?></td>
                    <td><?= e($pmt['reference_number'] ?? '—') ?></td>
                    <td class="num">TZS <?= number_format((float) $pmt['amount'], 2) ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>

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


