<?php
// Variables available:
// $company, $inv, $client, $items
?>

<div class="thermal-invoice">

    <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-row">
        <div>Invoice: <?= htmlspecialchars($inv['invoice_no']) ?></div>
        <div>Date: <?= date('d-m-Y', strtotime($inv['invoice_date'])) ?></div>
    </div>

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

    <hr>

    <div class="th-left">
        <b>Customer:</b><br>
        <?= htmlspecialchars($client['name']) ?><br>
        <?= htmlspecialchars($client['phone'] ?? '') ?>
    </div>

    <hr>

    <table class="th-items" width="100%">
        <thead>
            <tr>
                <th align="left">Item</th>
                <th align="right">Qty</th>
                <th align="right">Rate</th>
                <th align="right">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>
                    <?= htmlspecialchars($row['product_name']) ?>
                    <?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>

    <table width="100%">
        <tr>
            <td>Gross</td>
            <td align="right"><?= number_format($gross,2) ?></td>
        </tr>
        <tr>
            <td>CGST</td>
            <td align="right"><?= number_format($cgst,2) ?></td>
        </tr>
        <tr>
            <td>SGST</td>
            <td align="right"><?= number_format($sgst,2) ?></td>
        </tr>
        <tr class="th-grand">
            <td>Net</td>
            <td align="right"><?= number_format($inv['net_total'],2) ?></td>
        </tr>
    </table>

    <hr>

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

    <div class="th-center th-small">
        Payment Mode: <?= htmlspecialchars($inv['payment_mode']) ?><br>
        Thank you for your business!<br>
        Visit Again 🙏
    </div>

</div>
