<?php
// Collect items into array for row-separator logic
$itemRows = [];
while ($row = $items->fetch_assoc()) $itemRows[] = $row;

$cgst_total = 0;
$sgst_total = 0;
foreach ($itemRows as $r) {
    $cgst_total += $r['gst_amount'] / 2;
    $sgst_total += $r['gst_amount'] / 2;
}
?>

<div class="thermal-invoice">

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

    <!-- Company Header -->
    <div class="th-center th-company"><?= htmlspecialchars($company['company_name']) ?></div>
    <div class="th-center th-addr">
        <?php if (!empty($company['address'])): ?><?= nl2br(htmlspecialchars($company['address'])) ?><br><?php endif; ?>
        <?php if (!empty($company['gst_no'])): ?>GSTIN: <?= htmlspecialchars($company['gst_no']) ?><br><?php endif; ?>
        <?php if (!empty($company['phone'])): ?>Ph: <?= htmlspecialchars($company['phone']) ?><?php endif; ?>
    </div>

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

    <div class="th-center th-title">&#9733; CREDIT NOTE / RETURN &#9733;</div>

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

    <!-- Bill Info -->
    <table class="th-info">
        <tr>
            <td>Credit Note</td>
            <td><strong><?= htmlspecialchars($sr['return_no']) ?></strong></td>
        </tr>
        <tr>
            <td>Return Date</td>
            <td><?= date('d-m-Y', strtotime($sr['return_date'])) ?></td>
        </tr>
        <tr>
            <td>Invoice No</td>
            <td><?= htmlspecialchars($inv['invoice_no']) ?></td>
        </tr>
        <tr>
            <td>Customer</td>
            <td><strong><?= htmlspecialchars($client['name']) ?></strong></td>
        </tr>
        <?php if (!empty($client['phone'])): ?>
        <tr><td>Mobile</td><td><?= htmlspecialchars($client['phone']) ?></td></tr>
        <?php endif; ?>
    </table>

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

    <!-- Items -->
    <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 $total_items = count($itemRows); foreach ($itemRows as $idx => $row): ?>
        <tr>
            <td class="c-item"><?= htmlspecialchars($row['product_name']) ?></td>
            <td class="c-qty"><?= number_format($row['return_qty'], 0) ?></td>
            <td class="c-rate"><?= number_format($row['rate_incl_gst'], 2) ?></td>
            <td class="c-amt"><?= number_format($row['total_amount'], 2) ?></td>
        </tr>
        <?php if ($idx < $total_items - 1): ?>
        <tr class="th-item-sep"><td colspan="4"></td></tr>
        <?php endif; ?>
        <?php endforeach; ?>
        </tbody>
    </table>

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

    <!-- Totals -->
    <table class="th-totals">
        <tr><td>Taxable</td><td><?= number_format($sr['total_taxable'], 2) ?></td></tr>
        <tr><td>CGST</td><td><?= number_format($cgst_total, 2) ?></td></tr>
        <tr><td>SGST</td><td><?= number_format($sgst_total, 2) ?></td></tr>
        <tr class="th-grand">
            <td>TOTAL RETURN</td>
            <td>&#8377; <?= number_format($sr['total_amount'], 2) ?></td>
        </tr>
    </table>

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

    <!-- Barcode -->
    <div class="th-center">
        <svg id="barcode" data-invoice="<?= htmlspecialchars($sr['return_no']) ?>"></svg>
    </div>

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

    <!-- Footer -->
    <div class="th-center th-footer-text">GOODS ACCEPTED</div>
    <div class="th-center th-small" style="margin-top:3px;">
        Credit note issued. This is a computer generated document.
    </div>
    <div style="height:6mm;"></div>

</div>
