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

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

$username = $_SESSION['user'];

$data = mysqli_query($conn, "
    SELECT 
        username,
        status,
        tanggal,
        bukti,
        alasan,
        SUM(total_harga) as total_harga,
        SUM(qty) as qty
    FROM orders
    WHERE username='$username'
    GROUP BY username, tanggal, status, bukti, alasan
    ORDER BY tanggal 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: 'Poppins', sans-serif;
    background: linear-gradient(135deg, #f5f7fa, #e4e7eb);
}
.navbar{
    background: linear-gradient(90deg,#ff5722,#f44336);
    color:white;
    padding:20px 30px;
    display:flex;
    justify-content:space-between;
    align-items:center;
    box-shadow: 0 8px 20px rgba(0,0,0,0.2);
}
.navbar{
    position:relative; /* WAJIB biar logo bisa keluar */
}

/* LOGO FLOAT */
.logo-float{
    position:absolute;
    right:900px; /* posisi dekat tombol kembali */
    top:20%;
    transform:translateY(30%);
    backdrop-filter: blur(5px);

    height:80px;
    width:auto;

    background:white;
    padding:6px 10px;
    border-radius:12px;

    box-shadow:0 10px 25px rgba(0,0,0,0.25);
    transition:0.3s;
}

/* efek hover */
.logo-float:hover{
    transform:translateY(20%) scale(1.05);
}
.container{
    width:95%;
    margin:40px auto;
}
.card{
    background: rgba(255,255,255,0.7);
    backdrop-filter: blur(10px);
    padding:25px;
    border-radius:20px;
    box-shadow: 
        0 10px 25px rgba(0,0,0,0.1),
        inset 0 1px 0 rgba(255,255,255,0.6);
}
table{
    width:100%;
    border-collapse:separate;
    border-spacing:0 10px;
}
th{
    background: linear-gradient(90deg,#ff5722,#f44336);
    color:white;
    padding:15px;
}
td{
    background:white;
    padding:15px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.05);
}
.badge{
    padding:7px 16px;
    border-radius:20px;
    font-size:12px;
    font-weight:600;
    color:white;
}
.pending{ background:linear-gradient(#ff9800,#fb8c00); }
.dibayar{ background:linear-gradient(#28a745,#218838); }
.dikirim{ background:linear-gradient(#0d6efd,#0b5ed7); }
.selesai{ background:linear-gradient(#00e676,#00c853); }
.ditolak{ background:linear-gradient(#ff5252,#ff1744); }

.btn{
    padding:8px 14px;
    border-radius:10px;
    text-decoration:none;
    color:white;
    font-size:13px;
    display:inline-block;
}
.btn-detail{ background:#17a2b8; }
.btn-terima{ background:#28a745; } /* tombol baru */
.back{ background:#0d6efd; }

.alasan{
    display:block;
    font-size:11px;
    color:#dc3545;
    margin-top:5px;
}
</style>

</head>
<body>

<div class="navbar">
    
<h2 style="
    display:flex;
    align-items:center;
    gap:15px;
    font-weight:bold;
">
    📦 Riwayat Pesanan Saya
    <img src="images/wpo.png" class="logo-float">      
</h2>


    <a href="user_dashboard.php" class="btn back">⬅️ Kembali</a>
</div>

<div class="container">

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

            <?php
            $no = 1;
            mysqli_data_seek($data, 0);
            while($d = mysqli_fetch_assoc($data)){
            ?>
            
        <tr>
        <td><?= $no++ ?></td>
        <td><?= $d['qty'] ?></td>
        <td>Rp <?= number_format($d['total_harga']) ?></td>

        <!-- STATUS -->
        <td>
        <?php
        if($d['status'] == 'Pending'){
            echo "<span class='badge pending'>Pending</span>";
        } elseif($d['status'] == 'Dibayar'){
            echo "<span class='badge dibayar'>Dibayar</span>";
        } elseif($d['status'] == 'Dikirim'){
            echo "<span class='badge dikirim'>Dikirim</span>";
        } elseif($d['status'] == 'Ditolak'){
            echo "<span class='badge ditolak'>Ditolak</span>
                  <span class='alasan'>".$d['alasan']."</span>";
        } else {
            echo "<span class='badge selesai'>Selesai</span>";
        }
        ?>
        </td>

        <!-- TANGGAL -->
        <td><?= date('d M Y H:i', strtotime($d['tanggal'])) ?></td>

        <!-- DETAIL -->
        <td>
            <a href="detail_user.php?tgl=<?= $d['tanggal'] ?>" 
               class="btn btn-detail">📋 Detail</a>
        </td>


        </tr>

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

</div>

</body>
</html>