<?php
include "koneksi.php";
$faktur = mysqli_real_escape_string($conn, $_GET['faktur']);


// 1. Ambil Data Header (Info Nota)
$query_header = mysqli_query($conn, "SELECT * FROM tabel_penjualan WHERE no_faktur_penjualan = '$faktur'");
$data = mysqli_fetch_assoc($query_header);

// 2. Ambil Data Rinci (Daftar Barang)
$query_rinci = mysqli_query($conn, "SELECT * FROM tabel_rinci_penjualan WHERE no_faktur_penjualan = '$faktur'");
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <title>Detail Nota <?php echo substr($faktur,8,7); ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 p-5 md:p-10">
    <div class="max-w-4xl mx-auto bg-white p-8 shadow-lg rounded-lg">
        <div class="flex justify-between border-b pb-5 mb-5">
            <div>
                <h1 class="text-3xl font-extrabold text-blue-600">INVOICE</h1>
                <p class="text-sm text-gray-500 mt-1">Nomor: <span class="font-mono font-bold text-gray-800"><?php echo substr($faktur,8,7); ?></span></p>
            </div>
            <div class="text-right text-sm text-gray-600">
                <p class="font-bold text-gray-800 text-lg">Mie Djoetek</p>
                <p>Tanggal: <?php echo date('d/m/Y', strtotime($data['tgl_penjualan'])); ?></p>
            </div>
        </div>

        <div class="overflow-x-auto mb-8">
            <table class="w-full text-left">
                <thead>
                    <tr class="bg-gray-50 border-b">
                        <th class="py-3 px-2 text-gray-700">Kode</th>
                        <th class="py-3 px-2 text-gray-700">Nama Barang</th>
                        <th class="py-3 px-2 text-right text-gray-700">Harga</th>
                        <th class="py-3 px-2 text-right text-gray-700">Pajak</th>
                        <th class="py-3 px-2 text-right text-gray-700">Subtotal</th>
                    </tr>
                </thead>
                <tbody class="divide-y">
                    <?php while($item = mysqli_fetch_assoc($query_rinci)): ?>
                    <tr>
                        <td class="py-4 px-2 font-mono text-xs"><?php echo $item['kd_barang']; ?></td>
                        <td class="py-4 px-2 text-gray-800"><?php echo $item['nm_barang']; ?></td>
                        <td class="py-4 px-2 text-right">Rp <?php echo number_format($item['harga'], 0, ',', '.'); ?></td>
                        <td class="py-4 px-2 text-right text-gray-500 italic">Rp <?php echo number_format($item['nppn'], 0, ',', '.'); ?></td>
                        <td class="py-4 px-2 text-right font-semibold">Rp <?php echo number_format(($item['sub_total_jual']+$item['nppn']), 0, ',', '.'); ?></td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
                <tfoot>
                    <tr class="border-t-2">
                        <td colspan="4" class="py-4 text-right font-bold text-gray-700 px-2 uppercase">Total Bersih (Netto)</td>
                        <td class="py-4 text-right font-bold text-2xl text-blue-700 px-2">Rp <?php echo number_format($data['netto'], 0, ',', '.'); ?></td>
                    </tr>
                </tfoot>
            </table>
        </div>

        <div class="flex gap-4 no-print border-t pt-5">
            <button onclick="window.history.back()" class="bg-gray-200 hover:bg-gray-300 px-5 py-2 rounded-lg font-medium transition">Kembali</button>
            <a href="cetak_pdf.php?faktur=<?php echo $faktur; ?>" class="bg-red-600 hover:bg-red-700 text-white px-5 py-2 rounded-lg font-medium flex items-center gap-2 transition">
                <span>Download PDF</span>
            </a>
        </div>
    </div>
</body>
</html>