<?php
include "koneksi.php";

$id = intval($_GET['id'] ?? 0);

// Ambil transaksi master
$trans = $conn->query("SELECT * FROM tabel_transaksi WHERE id=$id")->fetch_assoc();
if (!$trans) {
    die("Transaksi tidak ditemukan!");
}

// Ambil detail transaksi
$detail = $conn->query("SELECT d.*, p.nama_produk 
    FROM tabel_transaksi_detail d 
    JOIN tabel_produk p ON d.produk_id = p.id 
    WHERE d.transaksi_id=$id");
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Nota Transaksi</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { font-size: 14px; }
        .nota { max-width: 700px; margin: auto; }
        .table th, .table td { padding: 5px; }
        .text-right { text-align: right; }
        .no-print { margin-top: 15px; }
    </style>
</head>
<body onload="window.print()">

<div class="nota">
    <h4 class="text-center">Nota Transaksi</h4>
    <hr>

    <div class="row mb-3">
        <div class="col-6">
            <strong>Tanggal:</strong> <?= $trans['tanggal'] ?><br>
            <strong>Customer:</strong> <?= htmlspecialchars($trans['customer']) ?>
        </div>
        <div class="col-6 text-end">
            <strong>No Transaksi:</strong> #<?= $trans['id'] ?><br>
            <strong>Total:</strong> Rp <?= number_format($trans['grand_total'],0,',','.') ?>
        </div>
    </div>

    <table class="table table-bordered">
        <thead class="table-light">
            <tr>
                <th>No</th>
                <th>Produk</th>
                <th class="text-end">Harga</th>
                <th class="text-center">Qty</th>
                <th class="text-end">Subtotal</th>
            </tr>
        </thead>
        <tbody>
            <?php $no=1; $total=0; while($d = $detail->fetch_assoc()): ?>
            <tr>
                <td><?= $no++ ?></td>
                <td><?= $d['nama_produk'] ?></td>
                <td class="text-end"><?= number_format($d['harga'],0,',','.') ?></td>
                <td class="text-center"><?= $d['qty'] ?></td>
                <td class="text-end"><?= number_format($d['subtotal'],0,',','.') ?></td>
            </tr>
            <?php $total += $d['subtotal']; endwhile; ?>
        </tbody>
        <tfoot>
            <tr>
                <th colspan="4" class="text-end">Grand Total</th>
                <th class="text-end">Rp <?= number_format($total,0,',','.') ?></th>
            </tr>
        </tfoot>
    </table>

    <p class="mt-3 text-center">Terima kasih atas transaksi Anda 🙏</p>
</div>

</body>
</html>
