<?php
// Admin layout header + sidebar (di-include di semua halaman admin)
?>
<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?= e($pageTitle) ?> — Admin Budi Jaya</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
  <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;500;600;700&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="<?= BASE_URL ?>/assets/css/admin.css">
</head>
<body class="admin-body">

<!-- Sidebar Toggle (mobile) -->
<div class="admin-wrapper">

  <!-- ═══ SIDEBAR ═══ -->
  <aside class="admin-sidebar" id="adminSidebar">
    <div class="sidebar-brand">
      <div class="sidebar-brand-icon"><i class="bi bi-house-door-fill"></i></div>
      <div>
        <div class="sidebar-brand-name">Budi Jaya</div>
        <div class="sidebar-brand-sub">Admin Panel</div>
      </div>
    </div>

    <nav class="sidebar-nav">
      <div class="nav-section-label">Utama</div>
      <a href="<?= BASE_URL ?>/admin/" class="sidebar-link <?= basename($_SERVER['PHP_SELF']) === 'index.php' && dirname($_SERVER['PHP_SELF']) !== '/' ? 'active' : '' ?>">
        <i class="bi bi-speedometer2"></i><span>Dashboard</span>
      </a>

      <div class="nav-section-label mt-3">Katalog</div>
      <a href="<?= BASE_URL ?>/admin/produk.php" class="sidebar-link <?= str_contains($_SERVER['PHP_SELF'], 'produk') ? 'active' : '' ?>">
        <i class="bi bi-box-seam"></i><span>Manajemen Produk</span>
      </a>
      <a href="<?= BASE_URL ?>/admin/kategori.php" class="sidebar-link <?= str_contains($_SERVER['PHP_SELF'], 'kategori') ? 'active' : '' ?>">
        <i class="bi bi-tag"></i><span>Kategori</span>
      </a>
      <a href="<?= BASE_URL ?>/admin/galeri.php" class="sidebar-link <?= str_contains($_SERVER['PHP_SELF'], 'galeri') ? 'active' : '' ?>">
        <i class="bi bi-images"></i><span>Galeri Portofolio</span>
      </a>
      <a href="<?= BASE_URL ?>/admin/hero_histori.php" class="sidebar-link <?= str_contains($_SERVER['PHP_SELF'], 'hero_histori') ? 'active' : '' ?>">
        <i class="bi bi-palette2"></i><span>Hero & Histori</span>
      </a>
      <a href="<?= BASE_URL ?>/admin/chat.php" class="sidebar-link <?= str_contains($_SERVER['PHP_SELF'], 'admin/chat') ? 'active' : '' ?>" id="menuChat">
        <i class="bi bi-chat-dots-fill"></i>
        <span>Chat Pelanggan</span>
        <?php
        $chatUnread = $pdo->query("SELECT COUNT(*) FROM chat_pesan WHERE pengirim='pelanggan' AND dibaca=0")->fetchColumn();
        if ($chatUnread > 0): ?>
          <span class="sidebar-badge" id="chatBadge"><?= $chatUnread ?></span>
        <?php endif; ?>
      </a>

      <div class="nav-section-label mt-3">Transaksi</div>
      <a href="<?= BASE_URL ?>/admin/pesanan.php" class="sidebar-link <?= str_contains($_SERVER['PHP_SELF'], 'pesanan') ? 'active' : '' ?>">
        <i class="bi bi-receipt"></i><span>Pesanan</span>
        <?php
          $nBaru = $pdo->query("SELECT COUNT(*) FROM pesanan WHERE status IN('pending','dibayar')")->fetchColumn();
          if ($nBaru > 0): ?>
          <span class="sidebar-badge"><?= $nBaru ?></span>
        <?php endif; ?>
      </a>
    

      <div class="nav-section-label mt-3">Lainnya</div>
      <a href="<?= BASE_URL ?>" target="_blank" class="sidebar-link">
        <i class="bi bi-globe"></i><span>Lihat Website</span>
      </a>
      <a href="<?= BASE_URL ?>/logout.php" class="sidebar-link text-danger">
        <i class="bi bi-box-arrow-left"></i><span>Logout</span>
      </a>
    </nav>
  </aside>
  <!-- ═══ END SIDEBAR ═══ -->

  <!-- ═══ MAIN CONTENT ═══ -->
  <div class="admin-main">
    <!-- Top bar -->
    <header class="admin-topbar">
      <button class="sidebar-toggle" onclick="toggleSidebar()">
        <i class="bi bi-list fs-4"></i>
      </button>
      <div class="admin-topbar-title">
        <?= e($pageTitle) ?>
      </div>
      <div class="d-flex align-items-center gap-3">
        <div class="admin-avatar">
          <i class="bi bi-person-circle fs-5"></i>
          <span class="d-none d-md-inline"><?= e($_SESSION['user_nama'] ?? 'Admin') ?></span>
        </div>
      </div>
    </header>

    <!-- Konten halaman dimulai di sini -->
    <div class="admin-content">
      <?= flashMessage() ?>