<?php
// includes/admin_layout_top.php
declare(strict_types=1);

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

$PAGE_TITLE = $PAGE_TITLE ?? 'Admin Portal';
$ACTIVE     = $ACTIVE ?? '';

$adminName = $_SESSION['admin_name'] ?? $_SESSION['user_name'] ?? 'Admin';
$adminRole = strtolower((string)($_SESSION['admin_role'] ?? $_SESSION['user_role'] ?? 'admin'));

$layoutDb = $mysqli ?? ($conn ?? null);
$themeColor = '#2563eb';

if ($layoutDb instanceof mysqli) {
    require_once __DIR__ . '/elms_settings.php';
    $themeColor = elms_setting($layoutDb, 'theme_color', '#2563eb');
}

if (!preg_match('/^#[0-9a-fA-F]{6}$/', $themeColor)) {
    $themeColor = '#2563eb';
}


if (!function_exists('h')) {
    function h($value): string
    {
        return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
    }
}

if (!function_exists('admin_active')) {
    function admin_active(string $key, string $active): string
    {
        return $key === $active ? 'active' : '';
    }
}

if (!function_exists('admin_role_label')) {
    function admin_role_label(string $role): string
    {
        return match ($role) {
            'super_admin' => 'Super Admin',
            'admin'       => 'Admin',
            'manager'     => 'Manager',
            default       => ucfirst(str_replace('_', ' ', $role)),
        };
    }
}

if (!function_exists('admin_role_badge')) {
    function admin_role_badge(string $role): string
    {
        return match ($role) {
            'super_admin' => 'bg-danger',
            'admin'       => 'bg-primary',
            'manager'     => 'bg-success',
            default       => 'bg-secondary',
        };
    }
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title><?= h($PAGE_TITLE) ?> | EDEN ELMS Admin</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">

    <!-- DataTables Optional -->
    <link href="https://cdn.datatables.net/1.13.8/css/dataTables.bootstrap5.min.css" rel="stylesheet">
    <link href="https://cdn.datatables.net/buttons/2.4.2/css/buttons.bootstrap5.min.css" rel="stylesheet">

    <style>
        :root {
            --admin-bg: #f4f7fb;
            --admin-sidebar: #0d1b2f;
            --admin-sidebar-2: #081425;
            --admin-primary: <?= h($themeColor) ?>;
            --admin-primary-2: <?= h($themeColor) ?>;
            --admin-text: #111827;
            --admin-muted: #64748b;
            --admin-border: #e5e7eb;
            --admin-card: #ffffff;
        }

        * {
            box-sizing: border-box;
        }

        body {
            background: var(--admin-bg);
            color: var(--admin-text);
            font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
            font-size: 14px;
            margin: 0;
        }

        .admin-shell {
            min-height: 100vh;
            display: flex;
        }

        .admin-sidebar {
            width: 250px;
            background: linear-gradient(180deg, var(--admin-sidebar), var(--admin-sidebar-2));
            color: #fff;
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            z-index: 1040;
            overflow-y: auto;
            box-shadow: 8px 0 24px rgba(15, 23, 42, .18);
        }

        .admin-brand {
            padding: 18px 16px 16px;
            border-bottom: 1px solid rgba(255,255,255,.08);
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .admin-brand-logo {
            width: 50px;
            height: 50px;
            border-radius: 12px;
            background: #fff;
            object-fit: contain;
            padding: 5px;
            box-shadow: 0 8px 20px rgba(0,0,0,.18);
        }

        .admin-brand-title {
            font-size: 14px;
            font-weight: 900;
            letter-spacing: .2px;
            margin: 0;
            line-height: 1.2;
        }

        .admin-brand-sub {
            font-size: 11px;
            color: rgba(255,255,255,.62);
            margin: 3px 0 0;
        }

        .admin-nav {
            padding: 12px 10px 28px;
        }

        .admin-nav-label {
            color: rgba(255,255,255,.45);
            font-size: 10px;
            font-weight: 800;
            letter-spacing: .08em;
            text-transform: uppercase;
            padding: 15px 10px 7px;
        }

        .admin-nav a {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 10px 10px;
            border-radius: 10px;
            color: rgba(255,255,255,.78);
            text-decoration: none;
            font-weight: 650;
            margin-bottom: 4px;
            transition: .18s ease;
            font-size: 13px;
        }

        .admin-nav a i {
            font-size: 15px;
            width: 19px;
            text-align: center;
        }

        .admin-nav a:hover {
            color: #fff;
            background: rgba(255,255,255,.08);
        }

        .admin-nav a.active {
            color: #fff;
            background: linear-gradient(135deg, #1d4ed8, #2563eb);
            box-shadow: 0 10px 22px rgba(37, 99, 235, .32);
        }

        .admin-sidebar-time {
            color: rgba(255,255,255,.48);
            font-size: 11px;
            padding: 10px 12px 18px;
        }

        .admin-main {
            margin-left: 250px;
            width: calc(100% - 250px);
            min-height: 100vh;
        }

        .admin-topbar {
            min-height: 72px;
            background: rgba(255,255,255,.95);
            backdrop-filter: blur(12px);
            border-bottom: 1px solid var(--admin-border);
            position: sticky;
            top: 0;
            z-index: 1030;
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 16px;
            padding: 12px 22px;
        }

        .admin-page-title {
            font-size: 18px;
            font-weight: 850;
            margin: 0;
        }

        .admin-breadcrumb {
            color: var(--admin-muted);
            font-size: 12px;
            margin-top: 2px;
        }

        .admin-content {
            padding: 22px;
        }

        .admin-card {
            background: var(--admin-card);
            border: 1px solid var(--admin-border);
            border-radius: 18px;
            box-shadow: 0 8px 24px rgba(15, 23, 42, .055);
        }

        .admin-card-header {
            padding: 16px 18px;
            border-bottom: 1px solid var(--admin-border);
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 12px;
            flex-wrap: wrap;
        }

        .admin-card-body {
            padding: 18px;
        }

        .admin-user-box {
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .admin-avatar {
            width: 38px;
            height: 38px;
            border-radius: 50%;
            background: #e0ecff;
            color: #1d4ed8;
            display: grid;
            place-items: center;
            font-weight: 900;
            border: 0;
        }

        .admin-avatar:hover {
            background: #cfe0ff;
        }

        .btn-admin-primary {
            background: #2563eb;
            border-color: #2563eb;
            color: #fff;
            border-radius: 12px;
            font-weight: 750;
            padding: 9px 14px;
        }

        .btn-admin-primary:hover {
            background: #1d4ed8;
            border-color: #1d4ed8;
            color: #fff;
        }

        .table {
            vertical-align: middle;
        }

        .table thead th {
            background: #f8fafc;
            color: #475569;
            font-size: 12px;
            text-transform: uppercase;
            letter-spacing: .04em;
            border-bottom: 1px solid var(--admin-border);
        }

        .badge {
            border-radius: 999px;
            padding: 6px 10px;
            font-weight: 750;
        }

        .form-control,
        .form-select {
            border-radius: 12px;
            border-color: #dbe3ef;
            padding: 10px 12px;
        }

        .form-control:focus,
        .form-select:focus {
            border-color: #2563eb;
            box-shadow: 0 0 0 .2rem rgba(37,99,235,.12);
        }

        .mobile-menu-btn {
            display: none;
        }

        .dropdown-menu {
            border: 1px solid #e5e7eb;
            border-radius: 14px;
            box-shadow: 0 14px 34px rgba(15,23,42,.14);
            padding: 8px;
        }

        .dropdown-item {
            border-radius: 10px;
            font-weight: 650;
            font-size: 13px;
            padding: 9px 10px;
        }

        @media (max-width: 992px) {
            .admin-sidebar {
                transform: translateX(-100%);
                transition: .2s ease;
            }

            .admin-sidebar.show {
                transform: translateX(0);
            }

            .admin-main {
                margin-left: 0;
                width: 100%;
            }

            .mobile-menu-btn {
                display: inline-flex;
            }

            .admin-content {
                padding: 16px;
            }
        }

        @media (max-width: 576px) {
            .admin-brand {
                padding: 14px;
            }

            .admin-topbar {
                padding: 10px 14px;
            }

            .admin-page-title {
                font-size: 16px;
            }
        }
    </style>
</head>
<body>

<div class="admin-shell">

    <aside class="admin-sidebar" id="adminSidebar">
        <div class="admin-brand">
            <img
                src="/elms/includes/logo/edden_logo.jpg"
                alt="EDEN Campus"
                class="admin-brand-logo"
                onerror="this.style.display='none';"
            >

            <div>
                <h1 class="admin-brand-title">EDEN CAMPUS</h1>
                <p class="admin-brand-sub">Admin Portal</p>
            </div>
        </div>

        <nav class="admin-nav">
            <div class="admin-nav-label">Home</div>

            <a href="/elms/admin/dashboard.php" class="<?= admin_active('dashboard', $ACTIVE) ?>">
                <i class="bi bi-house-door"></i>
                <span>Dashboard</span>
            </a>

            <div class="admin-nav-label">Manage Institute</div>

            <a href="/elms/admin/centres/" class="<?= admin_active('centres', $ACTIVE) ?>">
                <i class="bi bi-geo-alt"></i>
                <span>Centres</span>
            </a>

            <a href="/elms/admin/lecturers/index.php" class="<?= admin_active('lecturers', $ACTIVE) ?>">
                <i class="bi bi-person-video3"></i>
                <span>Lecturers</span>
            </a>

            <a href="/elms/admin/courses/" class="<?= admin_active('courses', $ACTIVE) ?>">
                <i class="bi bi-journal-bookmark"></i>
                <span>Courses</span>
            </a>

            <a href="/elms/admin/batches/" class="<?= admin_active('batches', $ACTIVE) ?>">
                <i class="bi bi-calendar2-week"></i>
                <span>Batches</span>
            </a>

            <a href="/elms/admin/student/" class="<?= admin_active('students', $ACTIVE) ?>">
                <i class="bi bi-people"></i>
                <span>Students</span>
            </a>

            <div class="admin-nav-label">Assessments</div>

            <a href="/elms/admin/assessments/" class="<?= admin_active('assessments', $ACTIVE) ?>">
                <i class="bi bi-clipboard-check"></i>
                <span>Assessments</span>
            </a>

            <a href="/elms/admin/assessments/submissions.php" class="<?= admin_active('submissions', $ACTIVE) ?>">
                <i class="bi bi-cloud-arrow-up"></i>
                <span>Submissions</span>
            </a>

            <a href="/elms/admin/assessments/gradebook.php" class="<?= admin_active('gradebook', $ACTIVE) ?>">
                <i class="bi bi-journal-check"></i>
                <span>Gradebook</span>
            </a>

            <a href="/elms/admin/marking.php" class="<?= admin_active('marking', $ACTIVE) ?>">
                <i class="bi bi-pencil-square"></i>
                <span>Marking</span>
            </a>

            <a href="/elms/admin/release.php" class="<?= admin_active('release', $ACTIVE) ?>">
                <i class="bi bi-send-check"></i>
                <span>Release</span>
            </a>

            <div class="admin-nav-label">Operations</div>

            <a href="/elms/admin/approvals.php" class="<?= admin_active('approvals', $ACTIVE) ?>">
                <i class="bi bi-patch-check"></i>
                <span>Approvals</span>
            </a>

            <a href="/elms/admin/verification.php" class="<?= admin_active('verification', $ACTIVE) ?>">
                <i class="bi bi-shield-check"></i>
                <span>Verification</span>
            </a>

            <a href="/elms/admin/reports/" class="<?= admin_active('reports', $ACTIVE) ?>">
                <i class="bi bi-graph-up"></i>
                <span>Reports</span>
            </a>

            <a href="/elms/admin/announcements/" class="<?= admin_active('announcements', $ACTIVE) ?>">
                <i class="bi bi-megaphone"></i>
                <span>Announcements</span>
            </a>
            
            <a href="/elms/admin/announcements/student_notices.php"
   class="<?= admin_active('student_notices', $ACTIVE) ?>">
    <i class="bi bi-image"></i>
    <span>Student Notices</span>
</a>

            <a href="/elms/admin/planner.php" class="<?= admin_active('planner', $ACTIVE) ?>">
                <i class="bi bi-calendar-event"></i>
                <span>Planner</span>
            </a>

            <?php if (in_array($adminRole, ['super_admin', 'admin'], true)): ?>
                <div class="admin-nav-label">Admin</div>

                <a href="/elms/admin/users.php" class="<?= admin_active('users', $ACTIVE) ?>">
                    <i class="bi bi-person-gear"></i>
                    <span>Users</span>
                </a>

                <a href="/elms/admin/settings.php" class="<?= admin_active('settings', $ACTIVE) ?>">
                    <i class="bi bi-gear"></i>
                    <span>Settings</span>
                </a>
            <?php endif; ?>

            <div class="admin-nav-label">Account</div>

            <a href="/elms/admin/profile.php" class="<?= admin_active('profile', $ACTIVE) ?>">
                <i class="bi bi-person-circle"></i>
                <span>Profile</span>
            </a>

            <a href="/elms/admin/logout.php">
                <i class="bi bi-box-arrow-right"></i>
                <span>Logout</span>
            </a>

            <div class="admin-sidebar-time">
                <?= date('F d, Y h:i A') ?>
            </div>
        </nav>
    </aside>

    <main class="admin-main">
        <header class="admin-topbar">
            <div class="d-flex align-items-center gap-3">
                <button type="button" class="btn btn-light mobile-menu-btn" onclick="toggleAdminSidebar()">
                    <i class="bi bi-list"></i>
                </button>

                <div>
                    <h2 class="admin-page-title"><?= h($PAGE_TITLE) ?></h2>
                    <div class="admin-breadcrumb">
                        Admin Portal / <?= h($PAGE_TITLE) ?>
                    </div>
                </div>
            </div>

            <div class="admin-user-box dropdown">
                <div class="text-end d-none d-sm-block">
                    <div class="fw-bold"><?= h($adminName) ?></div>
                    <span class="badge <?= admin_role_badge($adminRole) ?>">
                        <?= h(admin_role_label($adminRole)) ?>
                    </span>
                </div>

                <button
                    class="admin-avatar dropdown-toggle"
                    type="button"
                    data-bs-toggle="dropdown"
                    aria-expanded="false"
                    title="Account"
                >
                    <?= h(strtoupper(substr((string)$adminName, 0, 1))) ?>
                </button>

                <ul class="dropdown-menu dropdown-menu-end">
                    <li class="px-2 py-2">
                        <div class="fw-bold"><?= h($adminName) ?></div>
                        <div class="small text-muted"><?= h(admin_role_label($adminRole)) ?></div>
                    </li>

                    <li><hr class="dropdown-divider"></li>

                    <li>
                        <a class="dropdown-item" href="/elms/admin/profile.php">
                            <i class="bi bi-person-circle me-2"></i>
                            Profile
                        </a>
                    </li>

                    <li>
                        <a class="dropdown-item" href="/elms/admin/settings.php">
                            <i class="bi bi-gear me-2"></i>
                            Settings
                        </a>
                    </li>

                    <li><hr class="dropdown-divider"></li>

                    <li>
                        <a class="dropdown-item text-danger" href="/elms/admin/logout.php">
                            <i class="bi bi-box-arrow-right me-2"></i>
                            Logout
                        </a>
                    </li>
                </ul>
            </div>
        </header>

        <section class="admin-content">