<?php
$time = date('h:i A', strtotime($inv['created_at'] ?? $inv['invoice_date']));
?>

<div class="thermal-invoice">

    <?php if(!empty($company['logo'])): ?>
        <div class="th-center">
            <img src="../<?= $company['logo'] ?>" class="th-logo">
        </div>
    <?php endif; ?>

    <div class="th-center th-company"><?= strtoupper($company['company_name']) ?></div>
    <div class="th-center th-small">
        <?= nl2br(htmlspecialchars($company['address'])) ?><br>
        GSTIN: <?= htmlspecialchars($company['gst_no']) ?><br>
        Ph: <?= htmlspecialchars($company['phone']) ?>
    </div>

    <div class="th-sep"></div>

    <div class="th-center th-title">
        <?= ($inv['status'] === 'final') ? 'TAX INVOICE' : 'QUOTATION' ?>
    </div>

    <div class="th-sep"></div>

    <div class="th-left th-small">
        Name: <?= htmlspecialchars($client['name']) ?><br>
        Bill No: <?= htmlspecialchars($inv['invoice_no']) ?><br>
        Date: <?= date('d-m-Y', strtotime($inv['invoice_date'])) ?> <?= $time ?>
    </div>

    <div class="th-center">
        <svg id="barcode" data-invoice="<?= htmlspecialchars($inv['invoice_no']) ?>"></svg>
    </div>

    <div class="th-sep"></div>

    <table class="th-items">
        <thead>
            <tr>
                <th class="c-item">ITEM</th>
                <th class="c-qty">QTY</th>
                <th class="c-rate">RATE</th>
                <th class="c-amt">AMT</th>
            </tr>
        </thead>
        <tbody>
        <?php
        $gross = 0;
        $cgst = 0;
        $sgst = 0;

        while($row = $items->fetch_assoc()):
            $line = $row['total_amount'];
            $gross += $line;
            $gst = $row['gst_amount'];
            $cgst += $gst / 2;
            $sgst += $gst / 2;
        ?>
            <tr>
                <td class="c-item">
                    <?= htmlspecialchars($row['product_name']) ?>
                    <?php if(!empty($row['item_description'])): ?>
                        <div class="th-desc"><?= nl2br(htmlspecialchars($row['item_description'])) ?></div>
                    <?php endif; ?>
                </td>
                <td class="c-qty"><?= $row['sold_qty'] ?></td>
                <td class="c-rate"><?= number_format($row['selling_price_incl'],2) ?></td>
                <td class="c-amt"><?= number_format($line,2) ?></td>
            </tr>
			<tr>
				<td colspan="4"><hr></td>
			</tr>
        <?php endwhile; ?>
        </tbody>
    </table>

    <div class="th-sep"></div>

    <table class="th-totals">
        <tr><td>Subtotal</td><td><?= number_format($gross,2) ?></td></tr>
        <tr><td>Taxable</td><td><?= number_format($inv['total_taxable'],2) ?></td></tr>
        <tr><td>CGST</td><td><?= number_format($cgst,2) ?></td></tr>
        <tr><td>SGST</td><td><?= number_format($sgst,2) ?></td></tr>
        <tr class="th-grand"><td>GRAND TOTAL</td><td><?= number_format($inv['net_total'],2) ?></td></tr>
    </table>

    <div class="th-sep"></div>

    <div class="th-center">
        <div id="upiQR"
             data-upi="<?= htmlspecialchars($company['upi_id']) ?>"
             data-name="<?= htmlspecialchars($company['upi_name']) ?>"
             data-amount="<?= number_format($inv['net_total'],2,'.','') ?>">
        </div>
        <div class="th-small">Scan & Pay ₹<?= number_format($inv['net_total'],2) ?></div>
    </div>

    <div class="th-sep"></div>

    <div class="th-center th-small">
        THANK YOU VISIT AGAIN!<br>
        This is a computer generated invoice.
    </div>

</div>
