<?php
$adminPage = basename($_SERVER['PHP_SELF'], '.php');
$navItems = [
    ['index',          '📊', 'Dashboard',         'index.php'],
    ['products',       '📦', 'Products',           'products.php'],
    ['categories',     '🗂️', 'Categories',         'categories.php'],
    ['quotes',         '📋', 'RFQ / Quotes',       'quotes.php'],
    ['estimates',      '📄', 'Estimates',           'estimates.php'],
    ['orders',         '🛒', 'Purchase Orders',     'orders.php'],
    ['invoices',       '💰', 'Invoices',            'invoices.php'],
    ['certificates',   '🛡️', 'Digital Vault',       'certificates.php'],
    ['ai-maintenance', '🧠', 'AI Oracle',           'ai-maintenance.php'],
    ['customers',      '👥', 'Customers',           'customers.php'],
    ['repair',         '🛠️', 'Repair Requests',     'repair.php'],
    ['gallery',        '🖼️', 'Gallery',             'gallery.php'],
    ['media',          '📷', 'Media Library',       'media.php'],
    ['site-images',    '🖼️', 'Site Images',        'site-images.php'],
    ['mailbox',        '📬', 'Mailbox',             'mailbox.php'],
    ['reports',        '📈', 'Reports',             'reports.php'],
    ['content',        '✏️', 'Content Editor',      'content.php'],
    // ── Content Management ──
    ['blog',           '📝', 'Blog Posts',          'blog.php'],
    ['projects',       '🏗️', 'Projects',            'projects.php'],
    ['brands',         '🏷️', 'Brands',              'brands.php'],
    ['certifications', '🏅', 'Certifications',      'certifications.php'],
    ['faq',            '❓', 'FAQs',                'faq.php'],
    ['careers',        '💼', 'Careers',             'careers.php'],
    ['client-logos',   '🏢', 'Client Logos',        'client-logos.php'],
    ['downloads',      '⬇', 'Downloads',            'downloads.php'],
    // ── System ──
    ['broadcast',      '📢', 'Broadcast Email',     'broadcast.php'],
    ['email-templates','✉️', 'Email Templates',     'email-templates.php'],
    ['template-editor','🎨', 'PDF Designer',        'template-editor.php'],
    ['filemanager',    '🚀', 'Advanced Asset Suite', 'filemanager.php'],
    ['updater',        '🔄', 'Site Updater',        'updater.php'],
    ['api',            '🔌', 'Nexus API Console',   'api.php'],
    ['change-logs',      '📝', 'Technical Logs',      'change-logs.php'],
    ['settings',       '⚙️', 'Global Settings',     'settings.php'],
];
?>
<style>
/* Override any existing sidebar CSS completely */
#admin-sidebar * { box-sizing: border-box; }
#sb-nav::-webkit-scrollbar { width: 4px; }
#sb-nav::-webkit-scrollbar-track { background: transparent; }
#sb-nav::-webkit-scrollbar-thumb { background: #2a3147; border-radius: 4px; }
#sb-nav a { display:flex; align-items:center; gap:12px; padding:9px 16px;
  margin:2px 8px; border-radius:8px; font-size:13px; font-weight:500;
  color:#9ca3af; text-decoration:none; transition:background .15s, color .15s; }
#sb-nav a:hover { background:rgba(255,107,0,0.12); color:#ff7a15; }
#sb-nav a.active { background:rgba(255,107,0,0.15); color:#ff7a15;
  border-left:3px solid #ff6b00; padding-left:13px; }
</style>

<aside id="admin-sidebar" style="
  position:fixed; top:0; left:0; z-index:9999;
  width:260px; height:100vh;
  background:#0f172a;
  border-right:1px solid #2a3147;
  overflow:visible;
">

  <!-- TOP: Logo (pinned, absolute) -->
  <?php $sbLogo = setting('logo'); ?>
  <div id="sb-logo" style="
    position:absolute; top:0; left:0; right:0;
    padding:14px 16px 12px;
    border-bottom:1px solid #2a3147;
    background:#0f172a;
    display:flex; align-items:center; gap:10px;
  ">
    <?php if ($sbLogo): ?>
      <img src="<?= rtrim(SITE_URL, '/') ?>/uploads/<?= e($sbLogo) ?>" alt="Logo" style="height:34px;max-width:140px;object-fit:contain;">
      <div style="font-size:11px; color:#6b7280; white-space:nowrap;">Admin Panel</div>
    <?php else: ?>
      <div>
        <div style="font-family:Rajdhani,Georgia,sans-serif; font-size:20px; font-weight:900; letter-spacing:1px; color:#e8eaed;">
          <span style="color:#ff6b00;">RIG</span> N <span style="color:#ff6b00;">DIG</span>
        </div>
        <div style="font-size:11px; color:#6b7280; margin-top:1px;">Admin Panel</div>
      </div>
    <?php endif; ?>
  </div>

  <!-- USER INFO (pinned below logo, absolute) -->
  <div id="sb-user" style="
    position:absolute; top:72px; left:0; right:0;
    padding:10px 16px 12px;
    border-bottom:1px solid #2a3147;
    background:#111827;
  ">
    <div style="font-size:11px; color:#6b7280;">Logged in as</div>
    <div style="font-size:14px; font-weight:600; color:#e8eaed; margin-top:2px; line-height:1.2;"><?= e($_SESSION['admin_name'] ?? 'Admin') ?></div>
    <div style="font-size:11px; color:#ff6b00; margin-top:1px;"><?= ucfirst(str_replace('_', ' ', $_SESSION['admin_role'] ?? '')) ?></div>
  </div>

  <!-- NAV: Scrollable middle (absolute, fills between top and bottom sections) -->
  <div id="sb-nav" style="
    position:absolute; top:142px; bottom:84px; left:0; right:0;
    overflow-y:auto; overflow-x:hidden;
    padding:6px 0;
  ">
    <?php foreach ($navItems as [$page, $icon, $label, $href]): 
      // Ensure href is subfolder-aware
      $fullHref = (SITE_URL ? rtrim(SITE_URL, '/') : '') . '/admin/' . ltrim($href, '/');
      if ($href === '/admin/') $fullHref = (SITE_URL ? rtrim(SITE_URL, '/') : '') . '/admin/';
    ?>
    <a href="<?= $fullHref ?>" class="<?= $adminPage === $page ? 'active' : '' ?>">
      <span style="width:20px; text-align:center; font-size:15px; flex-shrink:0;"><?= $icon ?></span>
      <span><?= $label ?></span>
    </a>
    <?php endforeach; ?>
  </div>

  <!-- FOOTER: View site + Logout (pinned at bottom, absolute) -->
  <div id="sb-foot" style="
    position:absolute; bottom:0; left:0; right:0;
    padding:8px;
    border-top:1px solid #2a3147;
    background:#111827;
  ">
    <a href="/" target="_blank" style="
      display:flex; align-items:center; gap:10px;
      padding:8px 16px; border-radius:8px;
      font-size:13px; color:#9ca3af; text-decoration:none;
    " onmouseover="this.style.background='rgba(255,107,0,0.1)';this.style.color='#ff7a15';"
       onmouseout="this.style.background='';this.style.color='#9ca3af';">
      <span style="width:20px;text-align:center;">🌐</span> View Website
    </a>
    <a href="/admin/login.php?logout=1" style="
      display:flex; align-items:center; gap:10px;
      padding:8px 16px; border-radius:8px;
      font-size:13px; color:#f87171; text-decoration:none;
    " onmouseover="this.style.background='rgba(239,68,68,0.1)';"
       onmouseout="this.style.background='';">
      <span style="width:20px;text-align:center;">🚪</span> Logout
    </a>
  </div>

</aside>
