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

<div class="thermal-invoice">

    <!-- LOGO -->
    <?php if(!empty($company['logo'])): ?>
        <div class="th-center">
            <img src="../<?= $company['logo'] ?>" class="th-logo" style="max-width:90px; max-height:70px; width:auto; height:auto;">
        </div>
    <?php endif; ?>

    <!-- COMPANY -->
    <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>

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

	

    <!-- INVOICE INFO -->
    <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'])) ?><br>
        Time: <?= $time ?>
    </div>

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

    <hr>

    <!-- ITEM HEADER -->
    <table class="th-items" width="100%">
        <thead>
            <tr>
                <th style="padding-left: 20px;text-align: center;">ITEM</th>
                <th style="padding-left: 20px;text-align: center;">QTY</th>
                <th style="padding-left: 20px;text-align: center;">RATE</th>
                <th style="padding-left: 20px;text-align: center;">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  style="padding-left: 5px;">
                    <?= htmlspecialchars($row['product_name']) ?><br>
                    <?php if(!empty($row['item_description'])): ?>
                        <div class="th-desc"><?= nl2br(htmlspecialchars($row['item_description'])) ?></div>
                    <?php endif; ?>
                </td>
                <td align="right"><?= $row['sold_qty'] ?></td>
                <td align="right"><?= number_format($row['selling_price_incl'],2) ?></td>
                <td align="right"><?= number_format($line,2) ?></td>
            </tr>
        <?php endwhile; ?>
        </tbody>
    </table>

    <hr>

    <!-- TOTALS -->
    <table width="100%" class="th-totals">
        <tr><td style="padding-left: 20px;">Subtotal</td><td style="text-align: right;padding-right: 20px;"><?= number_format($gross,2) ?></td></tr>
        <tr><td style="padding-left: 20px;">Taxable</td><td style="text-align: right;padding-right: 20px;"><?= number_format($inv['total_taxable'],2) ?></td></tr>
        <tr><td style="padding-left: 20px;">CGST</td><td style="text-align: right;padding-right: 20px;"><?= number_format($cgst,2) ?></td></tr>
        <tr><td style="padding-left: 20px;">SGST</td><td style="text-align: right;padding-right: 20px;"><?= number_format($sgst,2) ?></td></tr>
        <tr class="th-grand">
            <td style="padding-left: 20px;">GRAND TOTAL</td>
            <td style="text-align: right;padding-right: 20px;"><?= number_format($inv['net_total'],2) ?></td>
        </tr>
    </table>

    <hr>

    <!-- UPI QR -->
    <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>

    <hr>

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

</div>
