function printReceipt() {
    if (!window.LAST_ORDER_ID) return;
    window.location.href = "/sales/receipt/" + window.LAST_ORDER_ID + "/?print=1";
}

async function shareReceipt() {

    if (!window.LAST_ORDER_ID) {
        alert("Order ID missing.");
        return;
    }

    // 1. Load the receipt HTML from server (hidden)
    let resp = await fetch("/sales/receipt/" + window.LAST_ORDER_ID + "/?raw=1");
    let html = await resp.text();

    // 2. Create a hidden container for rendering
    let container = document.createElement("div");
    container.style.position = "fixed";
    container.style.left = "-9999px";
    container.innerHTML = html;
    document.body.appendChild(container);

    // 3. Wait and convert to image
    await new Promise(r => setTimeout(r, 200));

    let receipt = container.querySelector(".receipt");
    const canvas = await html2canvas(receipt, { scale: 2, useCORS: true });

    const blob = await new Promise(resolve => canvas.toBlob(resolve, "image/png"));
    const file = new File([blob], "receipt.png", { type: "image/png" });

    // 4. Mobile sharing
    if (navigator.canShare && navigator.canShare({ files: [file] })) {
        try {
            await navigator.share({
                title: "Receipt",
                text: "Here is your SwiftPOS receipt.",
                files: [file]
            });
        } catch (err) {
            console.log(err);
        }
    } else {
        alert("🤷‍♂️ WhatsApp Web does not allow automatic image sharing.\nUse a mobile phone.");
    }

    // 5. Cleanup
    document.body.removeChild(container);
}
