<?php
session_start();
date_default_timezone_set('Asia/Jakarta');
include 'koneksi.php';

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

$username = $_SESSION['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'
");

// 🔥 ambil 1 data untuk alamat
$first = mysqli_fetch_assoc($data);
?>

<!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:25px;
    border-radius:15px;
    background:white;
    box-shadow:0 6px 15px rgba(0,0,0,0.1);
}
img{
    width:80px;
    border-radius:10px;
}
.alamat-box{
    background:linear-gradient(135deg,#f1f3f5,#ffffff);
    padding:15px;
    border-radius:12px;
    box-shadow:0 4px 10px rgba(0,0,0,0.08);
    margin-bottom:20px;
}
</style>
</head>

<body>

<div class="container">
    <div class="card">

        <h3>📦 Detail Pesanan Saya</h3>
        <p><b>Tanggal:</b> <?= $tanggal ?></p>

        <!-- 🔥 ALAMAT -->
        <div class="alamat-box">
            <b style="color:#f44336;">📍 Alamat Pengiriman</b><br>
            <span><?= $first['alamat'] ?></span>
        </div>

        <?php
        // 🔥 balikin pointer biar loop tetap jalan
        mysqli_data_seek($data, 0);
        ?>

        <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)){ 
                $harga = $d['total_harga'] / $d['qty'];
                $subtotal = $d['total_harga'];
                $total += $subtotal;
            ?>
            <tr>
                <td><?= $no++ ?></td>
                <td><?= $d['nama_produk'] ?></td>
                <td><img src="images/<?= $d['gambar'] ?>"></td>
                <td><?= $d['qty'] ?></td>
                <td>Rp <?= number_format($harga) ?></td>
                <td>Rp <?= number_format($subtotal) ?></td>
            </tr>
            <?php } ?>

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

            <?php
                mysqli_data_seek($data, 0);
                $cek = mysqli_fetch_assoc($data);
            ?>
            <br><br>

            <?php if($cek['status'] == 'Dikirim'){ ?>

            <a href="terima.php?id=<?= $tanggal ?>" 
            class="btn btn-success"
            onclick="return confirm('Yakin barang sudah diterima?')">
            ✅ Terima Barang
            </a>

            <?php } elseif($cek['status'] == 'Selesai'){ ?>

            <a href="invoice.php?tgl=<?= $tanggal ?>" 
            class="btn btn-dark"
            target="_blank">
            🧾 Lihat Invoice
            </a>

<?php } ?>

        </table>

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

    </div>
</div>

</body>
</html>