<?php
if (!isset($_SESSION)) session_start();
require __DIR__ . '/../auth.php';
$CAN_EDIT = $GLOBALS['CAN_EDIT'] ?? false;
$CAN_VIEW = $GLOBALS['CAN_VIEW'] ?? true;
if (!isset($pageTitle)) $pageTitle = "Admin — Smurfette's Country Delights";
if (!isset($pageClass)) $pageClass = "page-admin";
$currentAdminPage = basename($_SERVER['PHP_SELF'] ?? 'index.php');
$showAdminSidebar = ($currentAdminPage === 'index.php');
include __DIR__ . '/../../includes/header.php';
$adminMenu = [
  'Content' => [
    ['file'=>'index.php','icon'=>'🏠','label'=>'Dashboard'],
    ['file'=>'about.php','icon'=>'ℹ️','label'=>'About'],
    ['file'=>'news.php','icon'=>'📰','label'=>'News'],
    ['file'=>'pages.php','icon'=>'🧩','label'=>'Page Builder'],
    ['file'=>'schedule.php','icon'=>'📅','label'=>'Schedule'],
    ['file'=>'top50.php','icon'=>'🎵','label'=>'Top 50'],
    ['file'=>'events.php','icon'=>'🎉','label'=>'Events'],
  ],
  'Community' => [
    ['file'=>'djs.php','icon'=>'🎙️','label'=>'DJs'],
    ['file'=>'friends.php','icon'=>'🤝','label'=>'Friends'],
    ['file'=>'fans.php','icon'=>'⭐','label'=>'Fans'],
    ['file'=>'requests.php','icon'=>'🎶','label'=>'Requests'],
    ['file'=>'applications.php','icon'=>'📝','label'=>'Applications'],
    ['file'=>'contact.php','icon'=>'📨','label'=>'Contact'],
    ['file'=>'comments.php','icon'=>'💬','label'=>'Comments'],
    ['file'=>'chat.php','icon'=>'🗨️','label'=>'Live Chat'],
    ['file'=>'poll.php','icon'=>'📊','label'=>'Poll'],
  ],
  'Tools' => [
    ['file'=>'media.php','icon'=>'🖼️','label'=>'Media Library'],
    ['file'=>'player.php','icon'=>'🪟','label'=>'Popout Player'],
    ['file'=>'navigation.php','icon'=>'🧭','label'=>'Navigation'],
    ['file'=>'backup.php','icon'=>'🧰','label'=>'Backup / Restore'],
    ['file'=>'radiostats.php','icon'=>'📡','label'=>'Radio Stats'],
    ['file'=>'westats.php','icon'=>'📈','label'=>'Web Stats'],
  ],
  'System' => [
    ['file'=>'users.php','icon'=>'👤','label'=>'User Management','adminOnly'=>true],
    ['href'=>'http://cr3.broadcasturself.com:1982/status.xsl','icon'=>'📶','label'=>'Icecast Status','newTab'=>true],
    ['href'=>'https://broadcasturself.com','icon'=>'🌐','label'=>'Broadcasturself','newTab'=>true],
  ]
];
?>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="/admin/assets/admin-v2.css">
<main class="content-area">
  <div class="smurf-divider"></div>
  <div class="content-wrapper">
    <?php if ($showAdminSidebar): ?>
      <div class="admin-shell">
        <aside class="admin-sidebar">
          <div class="admin-brand">
            <div class="admin-brand-icon">🎙️</div>
            <div>
              <h2>Smurfettes Admin V2</h2>
              <small>Control room for pages, radio and community.</small>
            </div>
          </div>
          <div class="admin-search">
            <input type="search" id="adminMenuSearch" placeholder="Search admin tools...">
          </div>
          <?php foreach ($adminMenu as $section => $items): ?>
            <div class="admin-sidebar-section" data-admin-section="<?= htmlspecialchars($section, ENT_QUOTES, 'UTF-8') ?>">
              <h3 class="admin-sidebar-heading"><?= htmlspecialchars($section, ENT_QUOTES, 'UTF-8') ?></h3>
              <div class="admin-sidebar-links">
                <?php foreach ($items as $item): ?>
                  <?php if (!empty($item['adminOnly']) && (($_SESSION['role'] ?? '') !== 'admin')) continue; ?>
                  <?php $href = $item['href'] ?? ('/admin/' . $item['file']); ?>
                  <?php $active = (!empty($item['file']) && $currentAdminPage === $item['file']) ? 'is-active' : ''; ?>
                  <a class="admin-sidebar-link <?= $active ?>" href="<?= htmlspecialchars($href, ENT_QUOTES, 'UTF-8') ?>" <?= !empty($item['newTab']) ? 'target="_blank" rel="noopener"' : '' ?> data-admin-link="<?= htmlspecialchars(strtolower($item['label']), ENT_QUOTES, 'UTF-8') ?>">
                    <span class="ico"><?= $item['icon'] ?></span>
                    <span><?= htmlspecialchars($item['label'], ENT_QUOTES, 'UTF-8') ?></span>
                  </a>
                <?php endforeach; ?>
              </div>
            </div>
          <?php endforeach; ?>
        </aside>
        <section class="admin-main">
    <?php else: ?>
        <section class="admin-main admin-main-solo">
    <?php endif; ?>
          <div class="admin-v2-topbar">
            <div>
              <h1><?= htmlspecialchars($pageTitle, ENT_QUOTES, 'UTF-8') ?></h1>
              <p>Logged in as <strong><?= htmlspecialchars($_SESSION['user'] ?? 'guest', ENT_QUOTES, 'UTF-8') ?></strong> · Role: <strong><?= htmlspecialchars($_SESSION['role'] ?? 'viewer', ENT_QUOTES, 'UTF-8') ?></strong></p>
            </div>
            <div class="admin-chip-row">
              <span class="admin-chip"><?= $CAN_EDIT ? '✍️ Edit access' : '👀 View only' ?></span>
              <a class="admin-return" href="/admin/index.php">← Return to Admin</a>
              <a class="admin-return" href="/admin/logout.php">Logout</a>
            </div>
          </div>
