<?php
$adminTitle = $adminTitle ?? 'UGFX Admin';
$bootstrapCss = ugfx_vendor_asset('assets/vendor/bootstrap/css/bootstrap.min.css', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css', '../');
$bootstrapIconsCss = ugfx_vendor_asset('assets/vendor/bootstrap-icons/font/bootstrap-icons.css', 'https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css', '../');
$currentAdminPage = basename($_SERVER['PHP_SELF'] ?? 'dashboard.php');
$currentAdminUser = ugfx_admin_current_user();
$onlineAdmins = ugfx_admin_online_users();
$navItems = ugfx_nav_items();
$allAdminNav = [
    'dashboard.php' => 'Dashboard',
    'chat.php' => 'Chat',
    'news.php' => 'News',
    'events.php' => 'Events',
    'pages.php' => 'Pages',
    'friends.php' => 'Friends',
    'djs.php' => 'DJs / Team',
    'schedule.php' => 'Schedule',
    'requests.php' => 'Requests',
    'contact.php' => 'Contact',
    'applications.php' => 'Applications',
    'users.php' => 'Users & Roles',
];
$adminNav = [];
foreach ($allAdminNav as $file => $label) {
    $permission = match ($file) {
        'dashboard.php' => 'dashboard',
        'chat.php' => 'chat',
        'news.php' => 'news',
        'events.php' => 'events',
        'pages.php' => 'pages',
        'friends.php' => 'friends',
        'djs.php' => 'djs',
        'schedule.php' => 'schedule',
        'requests.php' => 'requests',
        'contact.php' => 'contact',
        'applications.php' => 'applications',
        'users.php' => 'users',
        default => 'dashboard',
    };
    if (ugfx_admin_can($permission)) $adminNav[$file] = $label;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= htmlspecialchars($adminTitle) ?></title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&family=Orbitron:wght@500;700;800&display=swap" rel="stylesheet">
<link href="<?= htmlspecialchars($bootstrapCss) ?>" rel="stylesheet">
<link href="<?= htmlspecialchars($bootstrapIconsCss) ?>" rel="stylesheet">
<link rel="stylesheet" href="../assets/css/theme.css">
<link rel="stylesheet" href="../assets/css/responsive.css">
</head>
<body class="ugfx-body ugfx-admin-shell">
<div class="ugfx-site-shell">
<header class="ugfx-topbar sticky-top ugfx-admin-topbar">
    <nav class="navbar navbar-expand-xl navbar-dark ugfx-nav">
        <div class="container-xxl">
            <a class="navbar-brand ugfx-brand" href="../index.php">
                <span class="ugfx-brand-mark">UGFX</span>
                <span class="ugfx-brand-text"><?= htmlspecialchars($ugfx['site_name']) ?> Admin</span>
            </a>
            <button class="navbar-toggler ugfx-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#ugfxAdminPublicNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="ugfxAdminPublicNav">
                <ul class="navbar-nav ms-auto align-items-xl-center gap-xl-2">
                    <?php foreach ($navItems as $item): ?>
                        <li class="nav-item">
                            <a class="nav-link" href="../<?= htmlspecialchars($item['href']) ?>"><?= htmlspecialchars($item['label']) ?></a>
                        </li>
                    <?php endforeach; ?>
                    <li class="nav-item ms-xl-2">
                        <a class="btn ugfx-btn-outline btn-sm" href="../index.php"><i class="bi bi-arrow-left-circle"></i> Return to site</a>
                    </li>
                    <li class="nav-item">
                        <a class="btn ugfx-btn-primary btn-sm" href="../listen-live.php"><i class="bi bi-broadcast"></i> Listen</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
</header>

<section class="container-xxl py-4">
    <div class="ugfx-card p-3 p-lg-4 mb-4 ugfx-admin-nav-shell">
        <div class="d-flex flex-column flex-lg-row gap-3 justify-content-between align-items-lg-center">
            <div>
                <div class="ugfx-micro-label">SIGNED IN</div>
                <div class="fw-bold mb-2"><?= htmlspecialchars((string)($currentAdminUser['username'] ?? 'Admin')) ?></div>
                <div class="d-flex gap-2 align-items-center flex-wrap">
                    <span class="ugfx-chip">Role: <?= htmlspecialchars(ucfirst((string)($currentAdminUser['role'] ?? 'admin'))) ?></span>
                    <?php if (!empty($currentAdminUser['temp_login'])): ?><span class="ugfx-chip">Temp local login</span><?php endif; ?>
                    <span class="ugfx-chip ugfx-chip-blue"><i class="bi bi-broadcast"></i> <?= count($onlineAdmins) ?> online</span>
                </div>
            </div>
            <div class="d-flex gap-2 flex-wrap justify-content-lg-end">
                <a class="btn ugfx-btn-outline btn-sm" href="../index.php"><i class="bi bi-house"></i> Public home</a>
                <a class="btn ugfx-btn-outline btn-sm" href="../chat.php"><i class="bi bi-chat-dots"></i> Public chat</a>
                <a class="btn ugfx-btn-outline btn-sm" href="logout.php"><i class="bi bi-box-arrow-right"></i> Logout</a>
            </div>
        </div>
        <div class="ugfx-admin-pill-nav mt-3">
            <?php foreach ($adminNav as $file => $label): ?>
                <a href="<?= htmlspecialchars($file) ?>" class="<?= $currentAdminPage === $file ? 'active' : '' ?>"><?= htmlspecialchars($label) ?></a>
            <?php endforeach; ?>
        </div>
    </div>
