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

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

$username = $_GET['user'];
$tanggal  = $_GET['tgl'];

$data = mysqli_query($conn, "
    SELECT o.*, p.nama_produk, p.gambar, u.alamat
    FROM orders o
    JOIN produk p ON o.produk_id = p.id
    JOIN users u ON o.username = u.username
    WHERE o.username='$username'
    AND o.tanggal='$tanggal'
");

$first = mysqli_fetch_assoc($data);
mysqli_data_seek($data, 0);

?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Detail Pesanan</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
body{
    background:#f4f6f9;
    font-family:Arial;
}
.container{
    width:90%;
    margin:30px auto;
}
.card{
    padding:20px;
    border-radius:12px;
    background:white;
    box-shadow:0 4px 10px rgba(0,0,0,0.08);
}
table{
    width:100%;
}
img{
    width:80px;
    border-radius:10px;
}
</style>
</head>

<body>

<div class="container">
    <div class="card">
        <h3>📦 Detail Pesanan</h3>
        <p><b>User:</b> <?= $username ?></p>
        <p><b>Tanggal:</b> <?= $tanggal ?></p>

        <div style="
            background:#f1f3f5;
            padding:15px;
            border-radius:10px;
            margin-bottom:15px;
            ">
            <b>📍 Alamat Pengiriman:</b><br>
            <?= $first['alamat'] ?>
        </div>

        <table class="table table-bordered">
            <tr>
                <th>No</th>
                <th>Produk</th>
                <th>Gambar</th>
                <th>Qty</th>
                <th>Harga</th>
                <th>Subtotal</th>
            </tr>

            <?php 
            $no=1; 
            $total=0;
            while($d=mysqli_fetch_assoc($data)){ 
                $subtotal = $d['total_harga'];
                $total += $d['total_harga'];
            ?>
            <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'] / $d['qty']) ?></td>
                <td>Rp <?= number_format($d['total_harga']) ?></td>
            </tr>
            <?php } ?>

            <tr>
                <td colspan="5"><b>Total</b></td>
                <td><b>Rp <?= number_format($total) ?></b></td>
            </tr>
        </table>

        <a href="pesanan.php" class="btn btn-primary">⬅️ Kembali</a>
    </div>
</div>

</body>
</html>