<?php
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/auth.php';

requireAdmin();
$cartCount = isset($_SESSION['cart']) ? array_sum(array_column($_SESSION['cart'], 'quantity')) : 0;
?>
<!DOCTYPE html>
<html lang="th" data-theme="<?php echo $_SESSION['theme'] ?? 'light'; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard - <?php echo SITE_NAME; ?></title>
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="<?php echo SITE_URL; ?>/assets/css/style.css" rel="stylesheet">
    <!-- Chart.js -->
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script>const SITE_URL = '<?php echo SITE_URL; ?>';</script>
</head>
<body>
<nav class="navbar navbar-expand-lg bg-dark navbar-dark sticky-top">
  <div class="container-fluid px-4">
    <a class="navbar-brand fw-bold text-warning" href="<?php echo SITE_URL; ?>/admin/index.php">
      ⚡ ADMIN DASHBOARD
    </a>
    <div class="d-flex align-items-center gap-3 ms-auto">
      <a href="<?php echo SITE_URL; ?>/index.php" class="btn btn-outline-light btn-sm rounded-pill">🏠 กลับหน้าร้านค้า</a>
      <span class="text-light small">👤 <?php echo sanitize($_SESSION['user']['fullname']); ?></span>
      <a href="<?php echo SITE_URL; ?>/login.php?action=logout" class="btn btn-danger btn-sm rounded-pill">ออกจากระบบ</a>
    </div>
  </div>
</nav>

<div class="container-fluid px-4 my-4">
  <div class="row">
    <!-- Sidebar Navigation -->
    <div class="col-md-3 col-lg-2 mb-4">
      <div class="card card-custom p-3">
<?php
$currentPage = basename($_SERVER['SCRIPT_NAME']);
?>
        <div class="list-group list-group-flush admin-sidebar-nav">
          <a href="<?php echo SITE_URL; ?>/admin/index.php" class="list-group-item list-group-item-action fw-bold border-0 rounded mb-1 text-nowrap <?php echo $currentPage === 'index.php' ? 'active' : ''; ?>">📊 สรุปภาพรวม (Dashboard)</a>
          <a href="<?php echo SITE_URL; ?>/admin/vendors.php" class="list-group-item list-group-item-action fw-bold border-0 rounded mb-1 text-nowrap d-flex justify-content-between align-items-center <?php echo $currentPage === 'vendors.php' ? 'active' : ''; ?>">
            <span>🏪 จัดการร้านค้า</span>
            <?php
              // Badge จำนวน pending vendor
              $pdo = getDBConnection();
              $pendingVendors = $pdo->query("SELECT COUNT(*) FROM vendor_applications WHERE status='pending'")->fetchColumn();
              if ($pendingVendors > 0): ?>
              <span class="badge bg-danger rounded-pill ms-1"><?php echo $pendingVendors; ?></span>
            <?php endif; ?>
          </a>
          <a href="<?php echo SITE_URL; ?>/admin/products.php" class="list-group-item list-group-item-action fw-bold border-0 rounded mb-1 text-nowrap <?php echo $currentPage === 'products.php' ? 'active' : ''; ?>">👕 จัดการสินค้า & สต็อก</a>
          <a href="<?php echo SITE_URL; ?>/admin/categories.php" class="list-group-item list-group-item-action fw-bold border-0 rounded mb-1 text-nowrap <?php echo $currentPage === 'categories.php' ? 'active' : ''; ?>">🏷️ จัดการหมวดหมู่</a>
          <a href="<?php echo SITE_URL; ?>/admin/brands.php" class="list-group-item list-group-item-action fw-bold border-0 rounded mb-1 text-nowrap <?php echo $currentPage === 'brands.php' ? 'active' : ''; ?>">🔖 จัดการแบรนด์</a>
          <a href="<?php echo SITE_URL; ?>/admin/sizes.php" class="list-group-item list-group-item-action fw-bold border-0 rounded mb-1 text-nowrap <?php echo $currentPage === 'sizes.php' ? 'active' : ''; ?>">📏 จัดการไซซ์</a>
          <a href="<?php echo SITE_URL; ?>/admin/filter_settings.php" class="list-group-item list-group-item-action fw-bold border-0 rounded mb-1 text-nowrap <?php echo $currentPage === 'filter_settings.php' ? 'active' : ''; ?>">⚙️ ตั้งค่าตัวกรองสินค้า</a>
          <a href="<?php echo SITE_URL; ?>/admin/orders.php" class="list-group-item list-group-item-action fw-bold border-0 rounded mb-1 text-nowrap <?php echo $currentPage === 'orders.php' ? 'active' : ''; ?>">📦 จัดการคำสั่งซื้อ & สลิป</a>
          <a href="<?php echo SITE_URL; ?>/admin/bank_accounts.php" class="list-group-item list-group-item-action fw-bold border-0 rounded mb-1 text-nowrap <?php echo $currentPage === 'bank_accounts.php' ? 'active' : ''; ?>">🏦 บัญชีธนาคารกลาง</a>
          <a href="<?php echo SITE_URL; ?>/admin/users.php" class="list-group-item list-group-item-action fw-bold border-0 rounded mb-1 text-nowrap <?php echo $currentPage === 'users.php' ? 'active' : ''; ?>">👥 จัดการสมาชิก</a>
          <a href="<?php echo SITE_URL; ?>/admin/export.php" class="list-group-item list-group-item-action fw-bold border-0 rounded mb-1 text-nowrap <?php echo $currentPage === 'export.php' ? 'active' : ''; ?>">📄 Export รายงาน CSV</a>
        </div>
      </div>
    </div>

    <!-- Main Content Area -->
    <div class="col-md-9 col-lg-10">
