<?php
session_start();
define('BASE_URL', '/eleganceofindia/admin-panal/');
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/db.php';
require_auth();

$currentPage = 'orders';

// Fetch all orders
$stmt = $pdo->query("SELECT * FROM orders ORDER BY created_at DESC");
$orders = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Orders — Elegance of India Admin</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" />
  <link rel="stylesheet" href="assets/admin.css" />
  <style>
    .status-badge {
      display: inline-block;
      padding: 4px 10px;
      font-size: 11px;
      font-weight: 600;
      border-radius: 4px;
      text-transform: capitalize;
    }
    .status-pending { background: #fff3e0; color: #e65100; }
    .status-processing { background: #e3f2fd; color: #1565c0; }
    .status-shipped { background: #e8f5e9; color: #2e7d32; }
    .status-cancelled { background: #ffebee; color: #c62828; }
  </style>
</head>
<body>
<?php include __DIR__ . '/includes/sidebar.php'; ?>
<div class="main-content">
  <?php include __DIR__ . '/includes/topbar.php'; ?>
  <div class="page-body">

    <div class="acard">
      <div class="acard-header">
        <span class="acard-title">All Orders</span>
      </div>
      <div class="atable-wrap">
        <?php if (empty($orders)): ?>
          <div class="empty-state">
            <i class="fas fa-shopping-cart"></i>
            <p>No orders yet.</p>
          </div>
        <?php else: ?>
        <table class="atable">
          <thead>
            <tr>
              <th>Order ID</th>
              <th>Customer</th>
              <th>Date</th>
              <th>Total</th>
              <th>Payment</th>
              <th>Status</th>
              <th>Action</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($orders as $o): ?>
            <tr>
              <td><strong><?= htmlspecialchars($o['order_number']) ?></strong></td>
              <td>
                <div style="font-weight:600;"><?= htmlspecialchars($o['first_name'] . ' ' . $o['last_name']) ?></div>
                <div style="font-size:11px;color:var(--text-muted);"><?= htmlspecialchars($o['email']) ?></div>
              </td>
              <td><?= date('d M Y, h:i A', strtotime($o['created_at'])) ?></td>
              <td>USD <?= number_format($o['total'], 2) ?></td>
              <td><span class="status-badge" style="background:#f5f5f5;color:#333;"><?= htmlspecialchars($o['payment_method']) ?></span></td>
              <td>
                <span class="status-badge status-<?= strtolower($o['order_status']) ?>">
                  <?= htmlspecialchars($o['order_status']) ?>
                </span>
              </td>
              <td>
                <a href="order_details.php?id=<?= $o['id'] ?>" class="btn btn-outline btn-sm">View Details</a>
              </td>
            </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
        <?php endif; ?>
      </div>
    </div>

  </div>
</div>
<script>
  document.getElementById('sidebarToggle')?.addEventListener('click', () => {
    document.querySelector('.sidebar').classList.toggle('open');
  });
</script>
</body>
</html>
