<?php
session_start();
require 'db.php';
if(!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin'){
    header("Location: login.php");
    exit();
}
/* ===========================
   DELETE ORDER
=========================== */
if (isset($_GET['delete'])) {
    $delete_id = intval($_GET['delete']);

    $stmt = $conn->prepare("DELETE FROM orders WHERE id = ?");
    $stmt->bind_param("i", $delete_id);
    $stmt->execute();
    $stmt->close();

    header("Location:orders.php");
    exit();
}

/* ===========================
   UPDATE STATUS
=========================== */
if (isset($_POST['update_status'])) {
    $order_id = intval($_POST['order_id']);
    $status = $_POST['status'];

    $stmt = $conn->prepare("UPDATE orders SET status = ? WHERE id = ?");
    $stmt->bind_param("si", $status, $order_id);
    $stmt->execute();
    $stmt->close();
}

/* ===========================
   PAGINATION
=========================== */
$limit = 3; // orders per page
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$offset = ($page - 1) * $limit;

// total orders
$total_result = $conn->query("SELECT COUNT(*) as total FROM orders");
$total_row = $total_result->fetch_assoc();
$total_orders = $total_row['total'];
$total_pages = ceil($total_orders / $limit);

// fetch paginated orders
$orders = $conn->query("SELECT * FROM orders ORDER BY created_at DESC LIMIT $limit OFFSET $offset");
?>

<!DOCTYPE html>
<html>
<head>
<title>Admin Orders</title>
<style>
body {
    font-family: Arial;
    background: #f4f6f9;
    padding: 20px;
}

/* PRELOADER */
#preloader {
    position: fixed;
    width: 100%;
    height: 100%;
    background: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
}

.loader {
    border: 6px solid #f3f3f3;
    border-top: 6px solid #5d1b01;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    100% { transform: rotate(360deg); }
}

.order-card {
    background: #fff;
    padding: 20px;
    margin-bottom: 20px;
    border-radius: 10px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.05);
}

.status.Pending { color: orange; font-weight: bold; }
.status.Processing { color: blue; font-weight: bold; }
.status.Completed { color: green; font-weight: bold; }
.status.Cancelled { color: red; font-weight: bold; }

button {
    padding: 6px 10px;
    border-radius: 5px;
    border: none;
    cursor: pointer;
}

.update-btn { background: #28a745; color: #fff; }
.delete-btn { background: red; color: #fff; text-decoration: none; padding: 6px 10px; }

.pagination a {
    padding: 8px 12px;
    margin: 5px;
    text-decoration: none;
    background: #ddd;
    border-radius: 5px;
}

.pagination a.active {
    background: #5d1b01;
    color: #fff;
}
</style>
</head>
<body>

<!-- PRELOADER -->
<div id="preloader">
    <div class="loader"></div>
</div>
<!-- FLOATING BACK BUTTON -->
<a href="javascript:history.back()" class="floating-back">
    ←
</a>

<style>
.floating-back {
    position: fixed;
    bottom: 30px;
    left: 30px;
    background: #5d1b01;
    color: #fff;
    font-size: 22px;
    text-decoration: none;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 5px 15px rgba(0,0,0,0.2);
    transition: 0.3s;
    z-index: 999;
}

.floating-back:hover {
    background: #8b2e0c;
    transform: scale(1.1);
}
</style>

<h2>Admin Order Management</h2>

<?php while ($order = $orders->fetch_assoc()) : ?>

<div class="order-card">

<strong>Order #<?= $order['id']; ?></strong><br>
<?= $order['customer_name']; ?> |
<?= $order['email']; ?> |
<?= $order['phone']; ?><br>

<span class="status <?= $order['status']; ?>">
    <?= $order['status']; ?>
</span>

<p>
Address: <?= $order['address']; ?>, <?= $order['city']; ?><br>
Payment: <?= $order['payment_method']; ?><br>
Total: ₦<?= number_format($order['total'],2); ?>
</p>

<!-- ORDER ITEMS -->
<table width="100%" border="1" cellpadding="5">
<tr>
    <th>Product ID</th>
    <th>Qty</th>
    <th>Price</th>
    <th>Subtotal</th>
</tr>

<?php
$order_id = $order['id'];
$items = $conn->query("SELECT * FROM order_items WHERE order_id = $order_id");

while ($item = $items->fetch_assoc()) :
    $subtotal = $item['quantity'] * $item['price'];
?>
<tr>
    <td><?= $item['product_id']; ?></td>
    <td><?= $item['quantity']; ?></td>
    <td>₦<?= number_format($item['price'],2); ?></td>
    <td>₦<?= number_format($subtotal,2); ?></td>
</tr>
<?php endwhile; ?>
</table>

<br>

<!-- UPDATE STATUS -->
<form method="POST" style="display:inline;">
<input type="hidden" name="order_id" value="<?= $order['id']; ?>">
<select name="status">
    <option value="Pending">Pending</option>
    <option value="Processing">Processing</option>
    <option value="Completed">Completed</option>
    <option value="Cancelled">Cancelled</option>
</select>
<button type="submit" name="update_status" class="update-btn">Update</button>
</form>

<!-- DELETE -->
<a href="?delete=<?= $order['id']; ?>" 
   onclick="return confirm('Delete this order?')" 
   class="delete-btn">Delete</a>

</div>

<?php endwhile; ?>

<!-- PAGINATION -->
<div class="pagination">
<?php for ($i=1; $i <= $total_pages; $i++): ?>
    <a href="?page=<?= $i; ?>" class="<?= ($page == $i) ? 'active' : ''; ?>">
        <?= $i; ?>
    </a>
<?php endfor; ?>
</div>

<script>
// PRELOADER HIDE
window.addEventListener("load", function(){
    document.getElementById("preloader").style.display = "none";
});
</script>

</body>
</html>