<?php
session_start();
include 'koneksi.php';

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

$username = $_SESSION['user'];

$data = mysqli_query($conn, "
    SELECT k.*, p.nama_produk, p.harga, p.gambar
    FROM keranjang k
    JOIN produk p ON k.produk_id = p.id
    WHERE k.username='$username'
");
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Keranjang Belanja</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css">
<style>

.qty-box{
    display:flex;
    justify-content:center;
    align-items:center;
    gap:10px;
}

.qty-btn{
    text-decoration:none;
    background:#28a745;
    color:white;
    padding:4px 10px;
    border-radius:5px;
    font-weight:bold;
}

body{
    font-family: Arial, sans-serif;
    background: #f4f6f9;
    margin: 0;
}
.container{
    width: 90%;
    margin: 30px auto;
}
.box{
    background: white;
    padding: 20px;
    border-radius: 14px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.08);
}
table{
    width: 100%;
    border-collapse: collapse;
}
th{
    background: #f44336;
    color: white;
}
th,td{
    padding: 14px;
    text-align: center;
    border: 1px solid #ddd;
}
td img{
    width: 80px;
    height: 80px;
    object-fit: cover;
    border-radius: 10px;
}
.btn{
    padding: 10px 18px;
    border-radius: 8px;
    color: white;
    text-decoration: none;
    font-weight: bold;
}
.checkout{ background:#28a745; }
.back{ background:#007bff; }
.hapus{ background:#dc3545; }

.action{
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
}
.empty{
    text-align: center;
    padding: 30px;
    color: #666;
    font-size: 20px;
}
</style>
</head>
<body>

<div class="container">
    <div class="box">
        <h2>🛒 Keranjang Belanja</h2>

        <?php if(mysqli_num_rows($data) > 0){ ?>
        <table>
            <tr>
                <th>No</th>
                <th>Produk</th>
                <th>Harga</th>
                <th>Qty</th>
                <th>Gambar</th>
                <th>Subtotal</th>
                <th>Aksi</th>
            </tr>

            <?php
            $no = 1;
            $total = 0;
            while($d = mysqli_fetch_assoc($data)){
                $subtotal = $d['harga'] * $d['qty'];
                $total += $subtotal;
            ?>
            <tr>
                <td><?= $no++ ?></td>
                <td><?= $d['nama_produk'] ?></td>
                <td>Rp <?= number_format($d['harga']) ?></td>
                <td class="qty-box">
                    
    <a href="update_qty.php?id=<?= $d['produk_id'] ?>&aksi=kurang" class="qty-btn">−</a>
    <span><?= $d['qty'] ?></span>
    <a href="update_qty.php?id=<?= $d['produk_id'] ?>&aksi=tambah" class="qty-btn">+</a>
</td>

                <td><img src="images/<?= $d['gambar'] ?>" alt="<?= $d['nama_produk'] ?>"></td>
                <td>Rp <?= number_format($subtotal) ?></td>
                <td>
                    <a href="hapus_keranjang.php?id=<?= $d['id'] ?>" class="btn hapus">🗑 Hapus</a>
                </td>
            </tr>
            <?php } ?>
        </table>

        <h3>Total: Rp <?= number_format($total) ?></h3>

        <div class="action">
    <a href="user_dashboard.php" class="btn back">🛒 Tambah Belanja</a>
    <a href="pembayaran.php" class="btn checkout">💳 Checkout</a>
</div>

        <?php } else { ?>
            <div class="empty">
                🛒 Keranjang masih kosong
                <br><br>
                <a href="user_dashboard.php" class="btn back">⬅️ Belanja Sekarang</a>
            </div>
        <?php } ?>

    </div>
</div>

</body>
</html>