<?php
    include 'config.php';

    $counter = isset($_GET['counter']) ? $_GET['counter'] : '';
    $tanggal = isset($_GET['tanggal']) ? $_GET['tanggal'] : '';
    $nama    = isset($_GET['nama']) ? $_GET['nama'] : '';
    $alamat  = isset($_GET['alamat']) ? $_GET['alamat'] : '';
    $nowa    = isset($_GET['nowa']) ? $_GET['nowa'] : '';
    $note    = isset($_GET['note']) ? $_GET['note'] : '';

    $result = mysqli_query($conn, "SELECT produk_temp.*, produk.produk_nama, produk.produk_harga 
                                    FROM produk_temp 
                                    LEFT JOIN produk ON produk_id = temp_produk 
                                    WHERE temp_master = '$counter'");
    $items = [];
    $total_raw = 0;
    while ($row = mysqli_fetch_array($result)) {
        $items[] = $row;
        $total_raw += $row['produk_harga'] * $row['temp_jumlah'];
    }

    if (count($items) === 0) {
?>
    <div class="empty-cart">🛒 Keranjang masih kosong<br>Pilih menu dari kategori di atas</div>
<?php
    } else {
?>
    <form id="cartForm" action="konfirmasi.php" method="GET">
        <?php foreach ($items as $it) { ?>
        <div class="cart-item" data-harga="<?php echo $it['produk_harga']; ?>">
            <div style="flex:1;">
                <div class="c-nama"><?php echo htmlspecialchars($it['produk_nama']); ?></div>
                <div class="c-harga">Rp <?php echo number_format($it['produk_harga']); ?></div>
            </div>
            <div class="qty-control">
                <button type="button" data-action="dec" data-temp-id="<?php echo $it['temp_id']; ?>">-</button>
                <input type="number" min="1" name="<?php echo $it['temp_id']; ?>" data-temp-id="<?php echo $it['temp_id']; ?>" value="<?php echo $it['temp_jumlah']; ?>" readonly>
                <button type="button" data-action="inc" data-temp-id="<?php echo $it['temp_id']; ?>">+</button>
            </div>
            <button type="button" class="delete-btn" data-temp-id="<?php echo $it['temp_id']; ?>" title="Hapus">🗑️</button>
        </div>
        <?php } ?>

        <div class="discount-row">
            <div class="discount-group">
                <label for="diskpersen">Diskon (%)</label>
                <input type="number" id="diskpersen" name="diskpersen" value="0" min="0" max="100">
            </div>
            <div class="discount-group">
                <label for="diskrp">Diskon (Rp)</label>
                <input type="number" id="diskrp" name="diskrp" value="0" min="0">
            </div>
        </div>

        <div class="total-box">Total: Rp <span id="totalAmount"><?php echo number_format($total_raw); ?></span></div>

        <input type="hidden" name="counter" value="<?php echo htmlspecialchars($counter); ?>">
        <input type="hidden" name="tanggal" value="<?php echo htmlspecialchars($tanggal); ?>">
        <input type="hidden" name="nama" value="<?php echo htmlspecialchars($nama); ?>">
        <input type="hidden" name="alamat" value="<?php echo htmlspecialchars($alamat); ?>">
        <input type="hidden" name="nowa" value="<?php echo htmlspecialchars($nowa); ?>">
        <input type="hidden" name="note" value="<?php echo htmlspecialchars($note); ?>">
        <input type="hidden" name="tujuan" value="USER_ADD">

        <button type="submit" class="submit-btn">✅ Konfirmasi Pesanan</button>
    </form>
<?php
    }
?>
