<?php
require_once '../db.php';
require_once '../includes/company.php';

$id   = (int)($_GET['id'] ?? 0);
$type = $_GET['type'] ?? 'invoice';   // invoice | transport

if ($id <= 0) die("Invalid Invoice");

$inv = $conn->query("SELECT * FROM sales_invoices WHERE id = $id")->fetch_assoc();
if (!$inv) die("Invoice not found");

$company = getCompany($conn, $inv['company_id']);

$client = $conn->query("SELECT * FROM clients WHERE id = {$inv['client_id']}")->fetch_assoc();

$items = $conn->query("
    SELECT
        si.*,
        si.description AS item_description,
        p.name AS product_name,
        p.hsn_code,
        p.unit,
        pi.mrp
    FROM sales_items si
    JOIN products p ON p.id = si.product_id
    LEFT JOIN purchase_items pi ON pi.id = si.purchase_item_id
    WHERE si.sales_invoice_id = $id
    ORDER BY si.id ASC
");

ob_start();
if ($type === 'transport') {
    require 'layout_thermal_transport.php';
} else {
    require 'layout_thermal.php';
}
$invoiceHtml = ob_get_clean();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Thermal — <?= htmlspecialchars($inv['invoice_no']) ?></title>
<link rel="stylesheet" href="css/print_thermal.css">
<script src="../assets/js/JsBarcode.all.min.js"></script>
<script src="../assets/js/qrcode.min.js"></script>
<style>
/* Screen preview wrapper */
body { background: #e5e7eb; padding: 12px; }
.thermal-wrap {
    max-width: 80mm;
    margin: 0 auto;
    background: #fff;
    box-shadow: 0 2px 12px rgba(0,0,0,.18);
    padding: 4px 0;
}
@media print {
    body { background: #fff; padding: 0; }
    .thermal-wrap { box-shadow: none; max-width: none; margin: 0; padding: 0; }
    .no-print { display: none !important; }
}
</style>
</head>
<body>

<div class="no-print" style="text-align:center;margin-bottom:10px;">
    <button onclick="window.print()" style="padding:6px 20px;background:#1e3a5f;color:#fff;border:none;border-radius:5px;cursor:pointer;font-size:13px;">
        &#128438; Print
    </button>
    <button onclick="window.close()" style="padding:6px 16px;background:#6b7280;color:#fff;border:none;border-radius:5px;cursor:pointer;font-size:13px;margin-left:8px;">
        Close
    </button>
</div>

<div class="thermal-wrap">
<?= $invoiceHtml ?>
</div>

<script>
window.onload = function() {
    var barcodeEl = document.getElementById('barcode');
    if (barcodeEl && typeof JsBarcode !== 'undefined') {
        JsBarcode(barcodeEl, barcodeEl.dataset.invoice, {
            format: 'CODE128', width: 1.2, height: 34, displayValue: false, margin: 0
        });
        barcodeEl.removeAttribute('width');
        barcodeEl.removeAttribute('height');
    }
    var qrEl = document.getElementById('upiQR');
    if (qrEl && qrEl.dataset.upi && typeof QRCode !== 'undefined') {
        new QRCode(qrEl, {
            text: 'upi://pay?pa=' + qrEl.dataset.upi
                + '&pn=' + encodeURIComponent(qrEl.dataset.name)
                + '&am=' + qrEl.dataset.amount,
            width: 92, height: 92
        });
    }
};
</script>
</body>
</html>
