<?php
require_once '../../config/database.php';

if (!isset($_GET['id'])) {
    die("ID Transaksi tidak ditemukan.");
}

$transaction_id = $_GET['id'];

// 1. Ambil data Transaksi & Kasir
$stmt = $pdo->prepare("SELECT t.*, u.username FROM transactions t JOIN users u ON t.user_id = u.id WHERE t.id = ?");
$stmt->execute([$transaction_id]);
$trx = $stmt->fetch();

if (!$trx) {
    die("Transaksi tidak valid.");
}

// 2. Ambil detail barang
$det_stmt = $pdo->prepare("
    SELECT p.product_name, p.selling_price, td.qty 
    FROM transaction_details td 
    JOIN products p ON td.product_id = p.id 
    WHERE td.transaction_id = ?
");
$det_stmt->execute([$transaction_id]);
$items = $det_stmt->fetchAll();

// 3. Hitung Subtotal
$subtotal = 0;
foreach ($items as $item) {
    $subtotal += ($item['selling_price'] * $item['qty']);
}
$total_bayar = $subtotal - $trx['discount_amount'];
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Struk #<?= $transaction_id ?></title>
    <style>
        /* CSS KHUSUS PRINTER THERMAL 58mm */
        @page { margin: 0; }
        body {
            font-family: 'Courier New', Courier, monospace; /* Font standar mesin kasir */
            margin: 0;
            padding: 0;
            width: 58mm; /* Lebar kertas pas 58mm */
            font-size: 12px;
            color: #000;
            line-height: 1.2;
        }
        .struk-container {
            padding: 2mm;
            width: 100%;
            box-sizing: border-box;
        }
        .center { text-align: center; }
        .right { text-align: right; }
        .bold { font-weight: bold; }
        .line { border-bottom: 1px dashed #000; margin: 5px 0; }
        
        table { width: 100%; border-collapse: collapse; }
        td { vertical-align: top; padding: 2px 0; font-size: 11px; }
        
        /* Hilangkan tombol saat dicetak di kertas */
        @media print {
            .no-print { display: none; }
            body { width: 100%; }
        }
        
        .btn-print {
            background-color: #10b981; color: white; border: none; 
            padding: 10px; width: 100%; font-weight: bold; cursor: pointer;
            margin-bottom: 10px; font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="no-print">
        <button class="btn-print" onclick="window.print()">🖨️ CETAK SEKARANG</button>
    </div>

    <div class="struk-container">
        <div class="center">
            <h2 style="margin: 0; font-size: 16px;">KebhavTamvan</h2>
            <div style="font-size: 10px;">
                Los Warung ds Jungjang No.A7 & A8<br>
                Jl. Perumahan Saphire Pejagalan<br>
                Telp: 081388265913
            </div>
        </div>
        
        <div class="line"></div>
        
        <table style="font-size: 10px;">
            <tr>
                <td><?= date('d-m-Y H:i', strtotime($trx['transaction_date'])) ?></td>
                <td class="right">Kasir: <?= htmlspecialchars($trx['username']) ?></td>
            </tr>
            <tr>
                <td colspan="2">No: TRX-<?= str_pad($transaction_id, 5, '0', STR_PAD_LEFT) ?></td>
            </tr>
        </table>

        <div class="line"></div>

        <table>
            <?php foreach ($items as $item): ?>
            <tr>
                <td colspan="2" class="bold"><?= htmlspecialchars($item['product_name']) ?></td>
            </tr>
            <tr>
                <td><?= $item['qty'] ?> x <?= number_format($item['selling_price'], 0, ',', '.') ?></td>
                <td class="right">Rp <?= number_format($item['selling_price'] * $item['qty'], 0, ',', '.') ?></td>
            </tr>
            <?php endforeach; ?>
        </table>

        <div class="line"></div>

        <table>
            <tr>
                <td>Sub Total</td>
                <td class="right">Rp <?= number_format($subtotal, 0, ',', '.') ?></td>
            </tr>
            <?php if ($trx['discount_amount'] > 0): ?>
            <tr>
                <td>Diskon</td>
                <td class="right">- Rp <?= number_format($trx['discount_amount'], 0, ',', '.') ?></td>
            </tr>
            <?php endif; ?>
            <tr class="bold" style="font-size: 13px;">
                <td style="padding-top: 5px;">TOTAL</td>
                <td class="right" style="padding-top: 5px;">Rp <?= number_format($total_bayar, 0, ',', '.') ?></td>
            </tr>
            <tr>
                <td>Bayar (Cash)</td>
                <td class="right">Rp <?= number_format($total_bayar, 0, ',', '.') ?></td>
            </tr>
        </table>

        <div class="line"></div>
        
        <div class="center" style="margin-top: 10px; font-size: 10px;">
            Terima kasih sudah mampir!<br>
            Tiktok/IG: Kebhav Tamvan
        </div>
    </div>

    <script>
        window.onload = function() {
            // Otomatis memicu perintah print saat halaman beres di-load
            setTimeout(function() {
                window.print();
            }, 500);
        };
    </script>
</body>
</html>