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

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

$username = $_SESSION['user'];

$data = mysqli_query($conn, "
    SELECT o.*, p.nama_produk, p.gambar
    FROM orders o
    JOIN produk p ON o.produk_id = p.id
    WHERE o.username='$username'
    ORDER BY o.id DESC
");

$totalPesanan = mysqli_num_rows($data);
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Riwayat Pesanan</title>
<style>
body{
    margin:0;
    font-family:Arial,sans-serif;
    background:#f4f6f9;
}
.navbar{
    background:linear-gradient(90deg,#ff5722,#f44336);
    color:white;
    padding:20px 30px;
    display:flex;
    justify-content:space-between;
    align-items:center;
}
.navbar h2{
    margin:0;
}
.btn{
    padding:10px 18px;
    border-radius:8px;
    color:white;
    text-decoration:none;
    font-weight:bold;
}
.back{
    background:#007bff;
}
.container{
    width:95%;
    margin:30px auto;
}
.card{
    background:white;
    padding:20px;
    border-radius:16px;
    box-shadow:0 4px 12px rgba(0,0,0,0.08);
    margin-bottom:20px;
}
.stat{
    text-align:center;
}
.stat h3{
    margin:0;
    color:#666;
}
.stat p{
    font-size:32px;
    font-weight:bold;
    color:#f44336;
    margin:10px 0 0;
}
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:70px;
    height:70px;
    border-radius:10px;
    object-fit:cover;
}
.badge{
    padding:8px 14px;
    border-radius:20px;
    color:white;
    font-size:13px;
    font-weight:bold;
}
.pending{background:#ff9800;}
.dibayar{background:#28a745;}
.selesai{background:#007bff;}
</style>
</head>
<body>

<div class="navbar">
    <h2>📦 Riwayat Pesanan Saya</h2>
    <a href="user_dashboard.php" class="btn back">⬅️ Kembali</a>
</div>

<div class="container">

    <div class="card stat">
        <h3>Total Pesanan</h3>
        <p><?= $totalPesanan ?></p>
    </div>

    <div class="card">
        <table>
            <tr>
                <th>No</th>
                <th>Produk</th>
                <th>Gambar</th>
                <th>Qty</th>
                <th>Total</th>
                <th>Status</th>
                <th>Tanggal</th>
            </tr>

            <?php
            $no = 1;
            mysqli_data_seek($data, 0);
            while($d = mysqli_fetch_assoc($data)){
            ?>
            <tr>
                <td><?= $no++ ?></td>
                <td><?= $d['nama_produk'] ?></td>
                <td><img src="images/<?= $d['gambar'] ?>"></td>
                <td><?= $d['qty'] ?></td>
                <td>Rp <?= number_format($d['total_harga']) ?></td>
                <td>
                    <?php
                    if($d['status'] == 'Pending'){
                        echo "<span class='badge pending'>Pending</span>";
                    } elseif($d['status'] == 'Dibayar'){
                        echo "<span class='badge dibayar'>Dibayar</span>";
                    } else {
                        echo "<span class='badge selesai'>Selesai</span>";
                    }
                    ?>
                </td>
                <td><?= $d['tanggal'] ?></td>
            </tr>
            <?php } ?>
        </table>
    </div>

</div>

</body>
</html>