<?php
require 'config.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

$id = $_GET['id'] ?? 0;

$stmt = $pdo->prepare("SELECT * FROM transactions WHERE id = ?");
$stmt->execute([$id]);
$trans = $stmt->fetch();

if (!$trans) {
    die("Transaksi tidak ditemukan!");
}

// AMBIL DATA MENU DARI KOLOM CART
$cart_items = json_decode($trans['cart'], true) ?? [];
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <link rel="icon" href="favicon.ico" type="image/x-icon">
    <meta charset="UTF-8">
    <title>Struk #<?= $trans['invoice'] ?></title>
    <style>
        @page { size: 56mm auto; margin: 0 0 20mm 0; }
        body { 
            font-family: 'Courier New', monospace; 
            margin: 0; padding: 0; width: 56mm; font-size: 11px; 
        }
        .receipt { word-wrap: break-word; overflow-wrap: break-word;  padding: 10px 8px 20mm 8px; width: 100%; box-sizing: border-box; }
        .text-center { text-align: center; }
        .bold { font-weight: bold; }
        .flex { display: flex; justify-content: space-between; }
        hr { border: none; border-top: 1px dashed #000; margin: 5px 0; }
        table { width: 100%; border-collapse: collapse; }
        .item-row td { padding: 2px 0; }
        svg#barcode { width: 100%; max-width: 45mm; }
    </style>
<!-- v2026.06.29 -->
</head>
<body onload="setTimeout(function(){ window.print(); }, 3000);">

<div class="receipt">
    <div class="text-center">
        <h2 class="bold" style="font-size:15px; margin:0;">GoldenWok RESTO</h2>
        <p style="font-size:10px; margin:2px 0;">Jl. Ciater, Palapa Tangerang Selatan</p>
        <hr>
        <p style="font-size:10px; margin:2px 0;">Invoice: <?= $trans['invoice'] ?></p>
        <p style="font-size:10px; margin:2px 0;"><?= date('d/m/Y H:i', strtotime($trans['created_at'])) ?></p>
        <hr>
    </div>

    <table>
        <?php foreach ($cart_items as $item): ?>
        <tr class="item-row">
            <td colspan="2"><?= htmlspecialchars($item['name']) ?></td>
        </tr>
        <tr class="item-row">
            <td style="padding-bottom: 5px;">
                <?= $item['qty'] ?> x <?= number_format($item['price']) ?>
            </td>
            <td class="text-right" style="padding-bottom: 5px;">
                <?= number_format($item['qty'] * $item['price']) ?>
            </td>
        </tr>
        <?php endforeach; ?>
    </table>

    <hr>

    <div style="font-size:11px;">
        <div class="flex">
            <span>Sub Total</span>
            <span>Rp <?= number_format($trans['subtotal']) ?></span>
        </div>
        <?php if ($trans['discount'] > 0): ?>
        <div class="flex">
            <span>Diskon</span>
            <span>-Rp <?= number_format($trans['discount']) ?></span>
        </div>
        <?php endif; ?>
        <div class="flex">
            <span>Service (Incl.)</span>
            <span>Rp <?= number_format($trans['service_charge']) ?></span>
        </div>
        <?php if ($trans['takeaway_fee'] > 0): ?>
        <div class="flex">
            <span>Fee (<?= $trans['order_type'] ?>)</span>
            <span>Rp <?= number_format($trans['takeaway_fee']) ?></span>
        </div>
        <?php endif; ?>
    </div>

    <hr>

    <div class="flex bold" style="font-size:13px;">
        <span>TOTAL</span>
        <span>Rp <?= number_format($trans['total']) ?></span>
    </div>

    <hr>
    <div class="text-center">
        <p style="font-size:10px;">Customer: <?= htmlspecialchars($trans['customer_name']) ?></p>
        <p style="font-size:10px; margin-top:5px;">Terima Kasih!</p>
    </div>

    <div class="text-center mt-4">
        <svg id="barcode"></svg>
    </div>

    <!-- SOBEK DISINI -->
    <div style="text-align:center;margin-top:10mm;padding-top:6mm;border-top:2px dashed #000;">
        <div style="font-size:10px;color:#999;letter-spacing:3px;">- - - - - SOBEK DISINI - - - - -</div>
        <div style="height:18mm;"></div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.5/dist/JsBarcode.all.min.js"></script>
<script>
    JsBarcode("#barcode", "<?= $trans['invoice'] ?>", {
        format: "CODE128",
        lineColor: "#000",
        width: 1.5,
        height: 35,
        displayValue: true,
        fontSize: 10
    });
</script>

</body>
</html>