<?php
session_start();
date_default_timezone_set('Asia/Jakarta');
if(!isset($_SESSION['admin'])){
    header("Location: index.php");
    exit;
}
include 'koneksi.php';

$data = mysqli_query($conn, "
    SELECT 
        username,
        status,
        tanggal,
        bukti,
        SUM(total_harga) as total_harga,
        SUM(qty) as qty,
        MAX(id) as id
    FROM orders
    GROUP BY username, tanggal, status, bukti
    ORDER BY id DESC
");

?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Pesanan Masuk</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>
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;
}
.btn{
    padding:10px 18px;
    border-radius:8px;
    color:white;
    text-decoration:none;
    font-weight:bold;
}
.back{background:#007bff;}
.done{background:#28a745;}
.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);
}
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;}
.dikirim{background:#0d6efd;}
.ditolak{background:#dc3545;}
</style>
</head>

<body>

<div class="navbar">
    <h2>📦 Pesanan Masuk</h2>
    <a href="admin.php" class="btn back">⬅️ Dashboard</a>
</div>

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

<tr>
    <th>No</th>
    <th>User</th>
    <th>Total Qty</th>
    <th>Total</th>
    <th>Status</th>
    <th>Tanggal</th>
    <th>Bukti</th>
    <th>Aksi</th>
</tr>

<?php $no=1; while($d=mysqli_fetch_assoc($data)){ ?>

<tr>
    <td><?= $no++ ?></td>
    <td><?= $d['username'] ?></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>";
        }elseif($d['status']=='Dikirim'){
            echo "<span class='badge dikirim'>Dikirim</span>";
        }elseif($d['status']=='Ditolak'){
            echo "<span class='badge ditolak'>Ditolak</span>";
        }else{
            echo "<span class='badge selesai'>Selesai</span>";
        }
        ?>
    </td>

    <td><?= $d['tanggal'] ?></td>

    <td>
        <?php if($d['bukti'] != ''){ ?>
            <a href="#" data-bs-toggle="modal" data-bs-target="#bukti<?= $d['id'] ?>">
                <img src="/warungpojok/bukti/<?= $d['bukti'] ?>" width="70">
            </a>
        <?php } else { ?>
            <p class="text-danger">Tidak ada</p>
        <?php } ?>
    </td>

    <td>
        <a href="detail.php?user=<?= $d['username'] ?>&tgl=<?= $d['tanggal'] ?>"
        class="btn btn-info btn-sm">📋 Detail</a>
        <br>
        
        <a href="acc.php?id=<?= $d['id'] ?>" class="btn btn-primary btn-sm">ACC</a>
        <a href="tolak.php?id=<?= $d['id'] ?>" class="btn btn-danger btn-sm">Tolak</a><br>
    </td>

</tr>

<!-- MODAL BUKTI -->
<div class="modal fade" id="bukti<?= $d['id'] ?>" tabindex="-1">
    <div class="modal-dialog modal-xl modal-dialog-centered">
        <div class="modal-content">

            <div class="modal-header">
                <h5 class="modal-title">Bukti Pembayaran</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <div class="modal-body text-center">
                <img src="/warungpojok/bukti/<?= $d['bukti'] ?>" 
                     class="img-fluid rounded"
                     style="max-height:500px;">
            </div>

        </div>
    </div>
</div>

<?php } ?>

</table>
</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>