<?php
require_once __DIR__ . '/../../../includes/number_to_words.php';

$hsnSummary = [];
$allRows    = [];

$items->data_seek(0);
while ($row = $items->fetch_assoc()) {
    $hsn = $row['hsn_code'] ?? '';
    if (!isset($hsnSummary[$hsn])) {
        $hsnSummary[$hsn] = ['taxable'=>0,'cgst'=>0,'sgst'=>0,'rate'=>$row['gst_percent']];
    }
    $hsnSummary[$hsn]['taxable'] += $row['taxable_amount'];
    $hsnSummary[$hsn]['cgst']    += $row['gst_amount'] / 2;
    $hsnSummary[$hsn]['sgst']    += $row['gst_amount'] / 2;
    $allRows[] = $row;
}

$exactTotal   = $inv['total_taxable'] + $inv['total_gst'];
$roundedTotal = round($exactTotal);
$roundOff     = $roundedTotal - $exactTotal;
$totalQty     = array_sum(array_column($allRows, 'sold_qty'));
$primaryBank  = $company['bank_accounts'][0] ?? null;
?>

<div class="modern-invoice">

<!-- ══ TOP BAR ══ -->
<?php if (!empty($isPdf)): ?>
<table style="width:100%;border-collapse:collapse;border-bottom:1px solid #bbb;background:#f5f5f5;">
<tr>
  <td style="padding:5px 12px;width:25%;">&nbsp;</td>
  <td style="padding:5px 12px;text-align:center;font-size:14px;font-weight:800;letter-spacing:3px;text-transform:uppercase;color:#111;"><?= ($inv['status'] === 'final') ? 'TAX INVOICE' : 'QUOTATION' ?></td>
  <td style="padding:5px 12px;text-align:right;font-size:9.5px;font-weight:700;color:#555;text-transform:uppercase;letter-spacing:0.5px;width:25%;">Original For Recipient</td>
</tr>
</table>
<?php else: ?>
<div class="mi-topbar">
    <span></span>
    <span class="mi-doc-title"><?= ($inv['status'] === 'final') ? 'TAX INVOICE' : 'QUOTATION' ?></span>
    <span class="mi-original">Original For Recipient</span>
</div>
<?php endif; ?>

<!-- ══ HEADER ══ -->
<?php if (!empty($isPdf)): ?>
<table style="width:100%;border-collapse:collapse;border-bottom:1px solid #bbb;">
<tr>
  <td style="vertical-align:top;padding:10px 12px;border-right:1px solid #bbb;width:58%;">
    <table style="border-collapse:collapse;"><tr>
      <?php if (!empty($company['logo'])): ?>
      <td style="vertical-align:top;padding-right:8px;">
        <img src="../<?= htmlspecialchars(ltrim($company['logo'],'/')) ?>" style="width:72px;height:72px;object-fit:contain;">
      </td>
      <?php endif; ?>
      <td style="vertical-align:top;">
        <div class="mi-company-name"><?= htmlspecialchars($company['company_name']) ?></div>
        <div class="mi-company-sub">
            GSTIN: <?= htmlspecialchars($company['gst_no']) ?><br>
            <?= nl2br(htmlspecialchars($company['address'])) ?><br>
            <?php if (!empty($company['phone'])): ?>Mobile: <?= htmlspecialchars($company['phone']) ?><?php endif; ?>
            <?php if (!empty($company['email'])): ?><br>Email: <?= htmlspecialchars($company['email']) ?><?php endif; ?>
        </div>
      </td>
    </tr></table>
  </td>
  <td style="vertical-align:top;width:42%;padding:0;">
    <table style="width:100%;border-collapse:collapse;">
      <tr>
        <td style="padding:7px 10px;vertical-align:top;border-bottom:1px solid #e0e0e0;border-right:1px solid #e0e0e0;width:50%;">
          <div class="mi-meta-label">Invoice #</div>
          <div class="mi-meta-val"><?= htmlspecialchars($inv['invoice_no']) ?></div>
          <barcode code="<?= htmlspecialchars($inv['invoice_no']) ?>" type="C128B" style="width:110px;height:26px;display:block;margin-top:4px;"></barcode>
        </td>
        <td style="padding:7px 10px;vertical-align:top;border-bottom:1px solid #e0e0e0;width:50%;">
          <div class="mi-meta-label">Invoice Date</div>
          <div class="mi-meta-val"><?= date('d M Y', strtotime($inv['invoice_date'])) ?></div>
        </td>
      </tr>
      <tr>
        <td style="padding:7px 10px;vertical-align:top;border-right:1px solid #e0e0e0;width:50%;">
          <div class="mi-meta-label">Place of Supply</div>
          <div class="mi-meta-val"><?= htmlspecialchars($client['state'] ?? '—') ?></div>
        </td>
        <td style="padding:7px 10px;vertical-align:top;width:50%;">
          <div class="mi-meta-label">Due Date</div>
          <div class="mi-meta-val"><?= date('d M Y', strtotime($inv['invoice_date'])) ?></div>
        </td>
      </tr>
    </table>
  </td>
</tr>
</table>
<?php else: ?>
<div class="mi-header">

    <!-- Company Info -->
    <div class="mi-company-col">
        <?php if (!empty($company['logo'])): ?>
        <img src="../<?= htmlspecialchars(ltrim($company['logo'],'/')) ?>" class="mi-logo">
        <?php endif; ?>
        <div class="mi-company-text">
            <div class="mi-company-name"><?= htmlspecialchars($company['company_name']) ?></div>
            <div class="mi-company-sub">
                GSTIN: <?= htmlspecialchars($company['gst_no']) ?><br>
                <?= nl2br(htmlspecialchars($company['address'])) ?><br>
                <?php if (!empty($company['phone'])): ?>Mobile: <?= htmlspecialchars($company['phone']) ?><?php endif; ?>
                <?php if (!empty($company['email'])): ?><br>Email: <?= htmlspecialchars($company['email']) ?><?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Invoice Meta Grid -->
    <div class="mi-meta-col">
        <div class="mi-meta-row">
            <div class="mi-meta-cell">
                <div class="mi-meta-label">Invoice #</div>
                <div class="mi-meta-val"><?= htmlspecialchars($inv['invoice_no']) ?></div>
                <svg id="barcode" data-invoice="<?= htmlspecialchars($inv['invoice_no']) ?>"></svg>
            </div>
            <div class="mi-meta-cell">
                <div class="mi-meta-label">Invoice Date</div>
                <div class="mi-meta-val"><?= date('d M Y', strtotime($inv['invoice_date'])) ?></div>
            </div>
        </div>
        <div class="mi-meta-row">
            <div class="mi-meta-cell">
                <div class="mi-meta-label">Place of Supply</div>
                <div class="mi-meta-val"><?= htmlspecialchars($client['state'] ?? '—') ?></div>
            </div>
            <div class="mi-meta-cell">
                <div class="mi-meta-label">Due Date</div>
                <div class="mi-meta-val"><?= date('d M Y', strtotime($inv['invoice_date'])) ?></div>
            </div>
        </div>
    </div>

</div>
<?php endif; ?>

<!-- ══ BILL TO / SHIP TO ══ -->
<?php
$_shipAddr  = !empty($client['ship_address']) ? $client['ship_address'] : ($client['address']  ?? '');
$_shipCity  = !empty($client['ship_city'])    ? $client['ship_city']    : ($client['city']     ?? '');
$_shipState = !empty($client['ship_state'])   ? $client['ship_state']  : ($client['state']    ?? '');
$_shipPin   = !empty($client['ship_pincode']) ? $client['ship_pincode'] : ($client['pincode']  ?? '');
$_billLine2 = implode(', ', array_filter([$client['city'] ?? '', $client['state'] ?? '']));
if (!empty($client['pincode'])) $_billLine2 .= ' - ' . $client['pincode'];
$_shipLine2 = implode(', ', array_filter([$_shipCity, $_shipState]));
if (!empty($_shipPin)) $_shipLine2 .= ' - ' . $_shipPin;
?>
<table style="width:100%;border-collapse:collapse;margin-bottom:0;font-size:9px;">
<tr>
    <td style="width:50%;vertical-align:top;padding:6px 10px;border:1px solid #d1d5db;border-right:none;">
        <div style="font-size:7.5px;font-weight:700;letter-spacing:0.6px;color:#6b7280;text-transform:uppercase;margin-bottom:3px;">Bill To / Buyer</div>
        <strong style="font-size:9.5px;"><?= htmlspecialchars($client['name']) ?></strong><br>
        <?php if (!empty($client['address'])): ?><?= nl2br(htmlspecialchars($client['address'])) ?><br><?php endif; ?>
        <?php if (!empty($_billLine2)): ?><?= htmlspecialchars($_billLine2) ?><br><?php endif; ?>
        <?php if (!empty($client['phone'])): ?>Ph: <?= htmlspecialchars($client['phone']) ?><br><?php endif; ?>
        <?php if (!empty($client['email'])): ?>Email: <?= htmlspecialchars($client['email']) ?><br><?php endif; ?>
        <?php if (!empty($client['gstin'])): ?>GSTIN: <?= htmlspecialchars($client['gstin']) ?><?php endif; ?>
    </td>
    <td style="width:50%;vertical-align:top;padding:6px 10px;border:1px solid #d1d5db;">
        <div style="font-size:7.5px;font-weight:700;letter-spacing:0.6px;color:#6b7280;text-transform:uppercase;margin-bottom:3px;">Ship To</div>
        <strong style="font-size:9.5px;"><?= htmlspecialchars($client['name']) ?></strong><br>
        <?php if (!empty($_shipAddr)): ?><?= nl2br(htmlspecialchars($_shipAddr)) ?><br><?php endif; ?>
        <?php if (!empty($_shipLine2)): ?><?= htmlspecialchars($_shipLine2) ?><br><?php endif; ?>
        <?php if (!empty($client['phone'])): ?>Ph: <?= htmlspecialchars($client['phone']) ?><br><?php endif; ?>
        <?php if (!empty($client['email'])): ?>Email: <?= htmlspecialchars($client['email']) ?><?php endif; ?>
    </td>
</tr>
</table>

<!-- ══ ITEMS TABLE ══ -->
<table class="mi-items">
    <thead>
        <tr>
            <th style="width:4%">#</th>
            <th style="width:32%;text-align:left;">Item</th>
            <th style="width:7%">MRP</th>
            <th style="width:9%">HSN/SAC</th>
            <th style="width:6%">Tax</th>
            <th style="width:6%">Qty</th>
            <th style="width:10%">Rate / Item</th>
            <th style="width:8%">Disc</th>
            <th style="width:5%">Per</th>
            <th style="width:11%;text-align:right;">Amount</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($allRows as $i => $row):
        $discText = '-';
        if ($row['discount_type'] === 'percent')     $discText = $row['discount_value'].'%';
        elseif ($row['discount_type'] === 'amount')  $discText = '&#8377;'.number_format($row['discount_value'],2);
    ?>
    <tr>
        <td class="c"><?= $i + 1 ?></td>
        <td><strong><?= htmlspecialchars($row['product_name']) ?></strong></td>
        <td class="r"><?= $row['mrp'] > 0 ? number_format($row['mrp'],2) : '-' ?></td>
        <td class="c"><?= htmlspecialchars($row['hsn_code']) ?></td>
        <td class="c"><?= number_format($row['gst_percent'],0) ?>%</td>
        <td class="r"><?= number_format($row['sold_qty'],0) ?> <?= htmlspecialchars($row['unit'] ?? '') ?></td>
        <td class="r"><?= number_format($row['selling_price_incl'],2) ?></td>
        <td class="c"><?= $discText ?></td>
        <td class="c"><?= htmlspecialchars($row['unit'] ?? 'PCS') ?></td>
        <td class="r"><strong><?= number_format($row['total_amount'],2) ?></strong></td>
    </tr>
    <?php endforeach; ?>
    <?php $fillerCount = empty($isPdf) ? max(0, 15 - count($allRows)) : 0; for ($f = 0; $f < $fillerCount; $f++): ?>
    <tr class="mi-filler-row"><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
    <?php endfor; ?>
    </tbody>
</table>

<!-- ══ TOTALS ══ -->
<?php if (!empty($isPdf)): ?>
<div class="mi-totals-wrap" style="overflow:hidden;">
<table style="border-collapse:collapse;float:right;">
  <tr><td style="padding:4px 8px;text-align:right;min-width:150px;color:#444;font-size:10.5px;font-style:italic;border-bottom:1px solid #eee;">Taxable Amount</td><td style="padding:4px 8px;text-align:right;font-weight:600;font-size:10.5px;min-width:90px;border-bottom:1px solid #eee;"><?= number_format($inv['total_taxable'],2) ?></td></tr>
  <tr><td style="padding:4px 8px;text-align:right;color:#444;font-size:10.5px;font-style:italic;border-bottom:1px solid #eee;">CGST <?= number_format((!empty($allRows) ? $allRows[0]['gst_percent'] : 0) / 2, 1) ?>%</td><td style="padding:4px 8px;text-align:right;font-weight:600;font-size:10.5px;border-bottom:1px solid #eee;"><?= number_format($inv['total_gst']/2,2) ?></td></tr>
  <tr><td style="padding:4px 8px;text-align:right;color:#444;font-size:10.5px;font-style:italic;border-bottom:1px solid #eee;">SGST <?= number_format((!empty($allRows) ? $allRows[0]['gst_percent'] : 0) / 2, 1) ?>%</td><td style="padding:4px 8px;text-align:right;font-weight:600;font-size:10.5px;border-bottom:1px solid #eee;"><?= number_format($inv['total_gst']/2,2) ?></td></tr>
  <?php if (!empty($inv['overall_discount_value'])): ?>
  <tr><td style="padding:4px 8px;text-align:right;color:#444;font-size:10.5px;font-style:italic;border-bottom:1px solid #eee;">Discount</td><td style="padding:4px 8px;text-align:right;font-weight:600;font-size:10.5px;border-bottom:1px solid #eee;">- <?= number_format($inv['overall_discount_value'],2) ?></td></tr>
  <?php endif; ?>
  <?php if (abs($roundOff) > 0.001): ?>
  <tr><td style="padding:4px 8px;text-align:right;color:#444;font-size:10.5px;font-style:italic;border-bottom:1px solid #eee;">Round Off</td><td style="padding:4px 8px;text-align:right;font-weight:600;font-size:10.5px;border-bottom:1px solid #eee;"><?= number_format($roundOff,2) ?></td></tr>
  <?php endif; ?>
  <tr style="background:#1e3a5f;">
    <td style="padding:6px 10px;text-align:left;color:#fff;font-weight:700;font-size:10px;">Total &nbsp; <?= number_format($totalQty,0) ?> item(s)</td>
    <td style="padding:6px 10px;text-align:right;color:#fff;font-weight:700;font-size:13px;">&#8377; <?= number_format($roundedTotal,2) ?></td>
  </tr>
</table>
<div style="clear:both;"></div>
</div>
<?php else: ?>
<div class="mi-totals-wrap">
    <div class="mi-total-line">
        <span class="mi-total-label">Taxable Amount</span>
        <span class="mi-total-value"><?= number_format($inv['total_taxable'],2) ?></span>
    </div>
    <div class="mi-total-line">
        <span class="mi-total-label">CGST <?= number_format((!empty($allRows) ? $allRows[0]['gst_percent'] : 0) / 2, 1) ?>%</span>
        <span class="mi-total-value"><?= number_format($inv['total_gst']/2,2) ?></span>
    </div>
    <div class="mi-total-line">
        <span class="mi-total-label">SGST <?= number_format((!empty($allRows) ? $allRows[0]['gst_percent'] : 0) / 2, 1) ?>%</span>
        <span class="mi-total-value"><?= number_format($inv['total_gst']/2,2) ?></span>
    </div>
    <?php if (!empty($inv['overall_discount_value'])): ?>
    <div class="mi-total-line">
        <span class="mi-total-label">Discount</span>
        <span class="mi-total-value">- <?= number_format($inv['overall_discount_value'],2) ?></span>
    </div>
    <?php endif; ?>
    <?php if (abs($roundOff) > 0.001): ?>
    <div class="mi-total-line">
        <span class="mi-total-label">Round Off</span>
        <span class="mi-total-value"><?= number_format($roundOff,2) ?></span>
    </div>
    <?php endif; ?>
    <div class="mi-grand-line">
        <span class="grand-left">Total &nbsp; <?= number_format($totalQty,0) ?> item(s)</span>
        <span class="grand-right">&#8377; <?= number_format($roundedTotal,2) ?></span>
    </div>
</div>
<?php endif; ?>

<!-- ══ AMOUNT IN WORDS ══ -->
<div class="mi-words">
    <strong>Amount Chargeable (in words):</strong> INR <?= numberToWords($roundedTotal) ?> Only. &nbsp;&nbsp; <em>E &amp; O.E</em>
</div>

<!-- ══ HSN SUMMARY ══ -->
<div class="mi-hsn-section">
    <div class="mi-hsn-title">HSN / SAC Summary</div>
    <table class="mi-hsn">
        <thead>
            <tr>
                <th rowspan="2">HSN/SAC</th>
                <th rowspan="2">Taxable Value</th>
                <th colspan="2">Central Tax</th>
                <th colspan="2">State / UT Tax</th>
                <th rowspan="2">Total Tax Amount</th>
            </tr>
            <tr>
                <th>Rate</th><th>Amount</th>
                <th>Rate</th><th>Amount</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($hsnSummary as $hsn => $d): ?>
        <tr>
            <td class="c"><?= htmlspecialchars($hsn) ?></td>
            <td class="r"><?= number_format($d['taxable'],2) ?></td>
            <td class="c"><?= $d['rate']/2 ?>%</td>
            <td class="r"><?= number_format($d['cgst'],2) ?></td>
            <td class="c"><?= $d['rate']/2 ?>%</td>
            <td class="r"><?= number_format($d['sgst'],2) ?></td>
            <td class="r"><?= number_format($d['cgst']+$d['sgst'],2) ?></td>
        </tr>
        <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr>
                <td><strong>TOTAL</strong></td>
                <td class="r"><strong><?= number_format($inv['total_taxable'],2) ?></strong></td>
                <td></td>
                <td class="r"><strong><?= number_format($inv['total_gst']/2,2) ?></strong></td>
                <td></td>
                <td class="r"><strong><?= number_format($inv['total_gst']/2,2) ?></strong></td>
                <td class="r"><strong><?= number_format($inv['total_gst'],2) ?></strong></td>
            </tr>
        </tfoot>
    </table>
</div>

<!-- ══ BANK | QR | SIGNATURE ══ -->
<?php if (!empty($isPdf)): ?>
<?php $_miUpiName = !empty($primaryBank['upi_name']) ? $primaryBank['upi_name'] : $company['company_name']; ?>
<table style="width:100%;border-collapse:collapse;border-bottom:1px solid #bbb;">
<tr>
  <td style="vertical-align:top;padding:8px 12px;border-right:1px solid #bbb;font-size:10px;line-height:1.7;">
    <strong>Bank Details:</strong>
    <?php foreach ($company['bank_accounts'] as $b): ?>
    <div class="mi-bank-entry">
        <table style="border:none;border-collapse:collapse;font-size:7.5px;line-height:1.5;">
            <tr><td style="padding:0;white-space:nowrap;">Bank</td><td style="padding:0 4px;">:</td><td style="padding:0;"><strong><?= htmlspecialchars($b['bank_name']) ?></strong></td></tr>
            <?php if ($b['account_holder']): ?><tr><td style="padding:0;white-space:nowrap;">Account Holder</td><td style="padding:0 4px;">:</td><td style="padding:0;"><?= htmlspecialchars($b['account_holder']) ?></td></tr><?php endif; ?>
            <tr><td style="padding:0;white-space:nowrap;">Account No</td><td style="padding:0 4px;">:</td><td style="padding:0;"><?= htmlspecialchars($b['account_no']) ?></td></tr>
            <tr><td style="padding:0;white-space:nowrap;">IFSC Code</td><td style="padding:0 4px;">:</td><td style="padding:0;"><?= htmlspecialchars($b['ifsc']) ?></td></tr>
            <?php if ($b['branch']): ?><tr><td style="padding:0;white-space:nowrap;">Branch</td><td style="padding:0 4px;">:</td><td style="padding:0;"><?= htmlspecialchars($b['branch']) ?></td></tr><?php endif; ?>
        </table>
    </div>
    <?php endforeach; ?>
  </td>
  <?php if ($primaryBank && !empty($primaryBank['upi_id'])): ?>
  <td style="vertical-align:middle;padding:8px 6px;border-right:1px solid #bbb;text-align:center;width:135px;">
    <?php if (!empty($hasPHPQR)):
        $_miUpiStr = 'upi://pay?pa=' . urlencode($primaryBank['upi_id']) . '&pn=' . urlencode($_miUpiName) . '&am=' . number_format($roundedTotal, 2, '.', '');
        ob_start(); QRcode::png($_miUpiStr, false, QR_ECLEVEL_M, 3, 2); $_miQrPng = ob_get_clean();
        $_miQrSrc = 'data:image/png;base64,' . base64_encode($_miQrPng);
    ?>
    <div style="font-size:9px;font-weight:700;color:#1e3a5f;margin-bottom:2px;">Scan &amp; Pay</div>
    <img src="<?= $_miQrSrc ?>" style="width:90px;height:90px;display:block;margin:0 auto 2px;">
    <div style="font-size:10px;font-weight:800;color:#1e3a5f;"><?= htmlspecialchars($_miUpiName) ?></div>
    <div style="font-size:11px;font-weight:900;color:#15803d;margin-top:2px;">&#8377; <?= number_format($roundedTotal,2) ?></div>
    <?php else: ?>
    <div style="font-size:9px;font-weight:700;color:#1e3a5f;margin-bottom:2px;">Pay via UPI</div>
    <div style="font-size:10px;font-weight:800;color:#1e3a5f;"><?= htmlspecialchars($_miUpiName) ?></div>
    <div style="font-size:8px;color:#666;margin-top:1px;"><?= htmlspecialchars($primaryBank['upi_id']) ?></div>
    <div style="font-size:11px;font-weight:900;color:#15803d;margin-top:3px;">&#8377; <?= number_format($roundedTotal,2) ?></div>
    <?php endif; ?>
  </td>
  <?php endif; ?>
  <td style="vertical-align:top;padding:8px 12px;text-align:right;font-size:10.5px;width:180px;">
    <div>For <?= htmlspecialchars($company['company_name']) ?></div>
    <?php if (!empty($company['signature'])): ?>
    <img src="../<?= htmlspecialchars(ltrim($company['signature'],'/')) ?>"
         style="height:70px;max-width:180px;object-fit:contain;display:block;margin:8px 0 4px auto;transform:rotate(-30deg);transform-origin:center;">
    <?php else: ?>
    <div style="height:50px;"></div>
    <?php endif; ?>
    <div>Authorised Signatory</div>
  </td>
</tr>
</table>
<?php else: ?>
<div class="mi-footer-row">

    <!-- Bank Details -->
    <div class="mi-bank-col">
        <strong>Bank Details:</strong>
        <?php foreach ($company['bank_accounts'] as $b): ?>
        <div class="mi-bank-entry">
            <table style="border:none;border-collapse:collapse;font-size:7.5px;line-height:1.5;">
                <tr><td style="padding:0;white-space:nowrap;">Bank</td><td style="padding:0 4px;">:</td><td style="padding:0;"><strong><?= htmlspecialchars($b['bank_name']) ?></strong></td></tr>
                <?php if ($b['account_holder']): ?><tr><td style="padding:0;white-space:nowrap;">Account Holder</td><td style="padding:0 4px;">:</td><td style="padding:0;"><?= htmlspecialchars($b['account_holder']) ?></td></tr><?php endif; ?>
                <tr><td style="padding:0;white-space:nowrap;">Account No</td><td style="padding:0 4px;">:</td><td style="padding:0;"><?= htmlspecialchars($b['account_no']) ?></td></tr>
                <tr><td style="padding:0;white-space:nowrap;">IFSC Code</td><td style="padding:0 4px;">:</td><td style="padding:0;"><?= htmlspecialchars($b['ifsc']) ?></td></tr>
                <?php if ($b['branch']): ?><tr><td style="padding:0;white-space:nowrap;">Branch</td><td style="padding:0 4px;">:</td><td style="padding:0;"><?= htmlspecialchars($b['branch']) ?></td></tr><?php endif; ?>
            </table>
        </div>
        <?php endforeach; ?>
    </div>

    <!-- QR Code -->
    <?php if ($primaryBank && !empty($primaryBank['upi_id'])): ?>
    <div class="mi-qr-col">
        <div id="upiQR"
             data-upi="<?= htmlspecialchars($primaryBank['upi_id']) ?>"
             data-name="<?= htmlspecialchars($primaryBank['upi_name'] ?? $company['company_name']) ?>"
             data-amount="<?= number_format($roundedTotal,2,'.','') ?>"></div>
        <div class="mi-qr-label">Pay using UPI</div>
        <div class="mi-qr-amt">&#8377; <?= number_format($roundedTotal,2) ?></div>
    </div>
    <?php endif; ?>

    <!-- Signature -->
    <div class="mi-sign-col">
        <div>For <?= htmlspecialchars($company['company_name']) ?></div>
        <?php if (!empty($company['signature'])): ?>
        <img src="../<?= htmlspecialchars(ltrim($company['signature'],'/')) ?>"
             style="height:70px;max-width:180px;object-fit:contain;display:block;margin:8px 0 4px;transform:rotate(-30deg);transform-origin:center;">
        <?php else: ?>
        <div class="mi-sign-space"></div>
        <?php endif; ?>
        <div>Authorised Signatory</div>
    </div>

</div>
<?php endif; ?>

<!-- ══ TERMS ══ -->
<div class="mi-terms">
    <strong>Declaration:</strong><br>
    - Goods once sold will not be taken back.<br>
    - Warranty as per manufacturer's terms only.<br>
    - If the bill is not paid within 7 days, interest will be charged 5% per Month.<br>
    - Subject to Dhanbad Jurisdiction only.
</div>

<!-- ══ PAGE FOOTER ══ -->
<div class="mi-page-footer">
    This is a computer-generated invoice. No signature required. Thank you for your valued business!
</div>

</div>
