<?php
/**
 * SmartThink LMS - CMS Header
 * 
 * Common header for all admin pages
 */

// Check if user is logged in and has CMS access
if (!isLoggedIn()) {
    redirect('../lms/login.php');
}

// Get current user
$user = getCurrentUser();
if (!$user) {
    redirect('../lms/login.php');
}

// Enforce role-based access for CMS (admin or super_admin)
if (!hasRole('admin') && !hasRole('super_admin')) {
    redirect('../lms/dashboard.php');
}

// Get current page
$currentPage = basename($_SERVER['PHP_SELF']);
?>
<!DOCTYPE html>
<html lang="en" class="layout-menu-fixed layout-compact" data-assets-path="<?php echo APP_URL; ?>/assets/materio/assets/" data-template="vertical-menu-template-free">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <title><?php echo isset($pageTitle) ? htmlspecialchars($pageTitle) : 'SmartThink Admin'; ?></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@300;400;500;600;700&display=swap" rel="stylesheet" />

    <link rel="stylesheet" href="<?php echo APP_URL; ?>/assets/materio/assets/vendor/fonts/iconify-icons.css" />

    <link rel="stylesheet" href="<?php echo APP_URL; ?>/assets/materio/assets/vendor/libs/node-waves/node-waves.css" />
    <link rel="stylesheet" href="<?php echo APP_URL; ?>/assets/materio/assets/vendor/css/core.css" />
    <link rel="stylesheet" href="<?php echo APP_URL; ?>/assets/materio/assets/css/demo.css" />

    <!-- Font Awesome for CMS action icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" />

    <link rel="stylesheet" href="<?php echo APP_URL; ?>/assets/materio/assets/vendor/libs/perfect-scrollbar/perfect-scrollbar.css" />
    <link rel="stylesheet" href="<?php echo APP_URL; ?>/assets/materio/assets/vendor/libs/apex-charts/apex-charts.css" />

    <script src="<?php echo APP_URL; ?>/assets/materio/assets/vendor/js/helpers.js"></script>
    <script src="<?php echo APP_URL; ?>/assets/materio/assets/js/config.js"></script>

    <style>
        :root {
            --tcs-primary: #004B87;
            --tcs-primary-light: #0066CC;
            --tcs-primary-dark: #003366;
            --tcs-accent: #00A3CC;
            --tcs-gray-50: #F8F9FA;
            --tcs-gray-100: #E9ECEF;
            --tcs-gray-200: #DEE2E6;
            --tcs-gray-300: #CED4DA;
            --tcs-gray-400: #ADB5BD;
            --tcs-gray-500: #6C757D;
            --tcs-gray-600: #495057;
            --tcs-gray-700: #343A40;
            --tcs-gray-800: #212529;
            --tcs-gray-900: #141619;
            --tcs-white: #FFFFFF;
        }

        body {
            font-family: 'Inter', sans-serif;
            background-color: var(--tcs-gray-50);
        }

        .st-brand {
            display: inline-flex;
            align-items: center;
            gap: 0.75rem;
        }

        .st-brand-mark {
            width: 48px;
            height: 48px;
            border-radius: 12px;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            color: var(--tcs-white);
            font-weight: 800;
            letter-spacing: 0.5px;
            background: linear-gradient(135deg, var(--tcs-primary) 0%, var(--tcs-primary-light) 100%);
            box-shadow: 0 8px 24px rgba(0, 75, 135, 0.25);
        }

        .st-brand-name {
            line-height: 1.2;
            color: var(--tcs-gray-800);
        }

        .st-brand-sub {
            font-size: 0.8rem;
            color: var(--tcs-gray-500);
            font-weight: 500;
        }

        #layout-menu {
            background: var(--tcs-white);
            border-right: 1px solid var(--tcs-gray-200);
            box-shadow: 2px 0 10px rgba(0, 0, 0, 0.08);
        }

        .menu-inner .menu-item {
            margin: 0.25rem 0;
        }

        .menu-inner .menu-link {
            border-radius: 8px;
            margin: 0 0.75rem;
            padding: 0.75rem 1rem;
            transition: all 0.2s ease;
            color: var(--tcs-gray-600);
        }

        .menu-inner .menu-link:hover {
            background-color: var(--tcs-gray-50);
            color: var(--tcs-primary);
            transform: translateX(4px);
        }

        .menu-inner .menu-link.active {
            background-color: var(--tcs-primary);
            color: var(--tcs-white);
            box-shadow: 0 4px 12px rgba(0, 75, 135, 0.3);
        }

        .menu-inner .menu-icon {
            color: inherit;
            margin-right: 0.75rem;
        }

        .layout-navbar {
            background: var(--tcs-white);
            border-bottom: 1px solid var(--tcs-gray-200);
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
            backdrop-filter: blur(12px);
        }

        .navbar-nav .nav-link {
            color: var(--tcs-gray-600);
            transition: color 0.2s ease;
        }

        .navbar-nav .nav-link:hover {
            color: var(--tcs-primary);
        }

        .form-control {
            border: 1px solid var(--tcs-gray-300);
            border-radius: 8px;
            padding: 0.75rem 1rem;
            transition: all 0.2s ease;
        }

        .form-control:focus {
            border-color: var(--tcs-primary);
            box-shadow: 0 0 0 0.2rem rgba(0, 75, 135, 0.25);
        }

        .btn-primary {
            background-color: var(--tcs-primary);
            border-color: var(--tcs-primary);
            border-radius: 8px;
            padding: 0.75rem 1.5rem;
            font-weight: 600;
            transition: all 0.2s ease;
        }

        .btn-primary:hover {
            background-color: var(--tcs-primary-dark);
            border-color: var(--tcs-primary-dark);
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(0, 75, 135, 0.3);
        }

        .card {
            border: none;
            border-radius: 12px;
            box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
            transition: all 0.2s ease;
        }

        .card-header {
            background: var(--tcs-gray-50);
            border-bottom: 1px solid var(--tcs-gray-200);
            border-radius: 12px 12px 0 0 !important;
            padding: 1.5rem;
        }

        .card-body {
            padding: 1.5rem;
        }

        .avatar-initial {
            background: linear-gradient(135deg, var(--tcs-primary) 0%, var(--tcs-accent) 100%);
        }

        .badge {
            border-radius: 20px;
            font-weight: 500;
            padding: 0.5rem 1rem;
        }

        .menu-header {
            color: var(--tcs-gray-500);
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin: 1.5rem 0 0.5rem 0;
            font-size: 0.8rem;
        }

        .layout-page {
            background-color: var(--tcs-gray-50);
        }

        .content-wrapper {
            padding: 0;
        }

        .container-xxl {
            max-width: 1400px;
        }

        @media (max-width: 1199.98px) {
            .layout-menu-toggle {
                margin-left: 0;
            }
        }

        @media (max-width: 767.98px) {
            .menu-inner .menu-link {
                margin: 0 0.5rem;
                padding: 0.5rem 0.75rem;
            }

            .card-body {
                padding: 1rem;
            }
        }
    </style>
</head>
<body>

<div class="layout-wrapper layout-content-navbar">
    <div class="layout-container">
        <aside id="layout-menu" class="layout-menu menu-vertical menu bg-menu-theme">
            <div class="app-brand demo">
                <a href="dashboard.php" class="app-brand-link">
                    <span class="st-brand">
                        <span class="st-brand-mark">ST</span>
                        <span class="st-brand-name">
                            <span class="app-brand-text demo menu-text fw-semibold">SmartThink</span>
                            <span class="d-block st-brand-sub">Admin Console</span>
                        </span>
                    </span>
                </a>

                <a href="javascript:void(0);" class="layout-menu-toggle menu-link text-large ms-auto">
                    <i class="menu-toggle-icon d-xl-inline-block align-middle"></i>
                </a>
            </div>

            <div class="menu-inner-shadow"></div>

            <ul class="menu-inner py-1">
                <li class="menu-item<?php echo $currentPage === 'dashboard.php' ? ' active' : ''; ?>">
                    <a href="dashboard.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-home-smile-line"></i>
                        <div>Dashboard</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'users.php' ? ' active' : ''; ?>">
                    <a href="users.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-group-line"></i>
                        <div>Users</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'student_sessions.php' ? ' active' : ''; ?>">
                    <a href="student_sessions.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-shield-user-line"></i>
                        <div>Student Sessions</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'courses.php' ? ' active' : ''; ?>">
                    <a href="courses.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-book-3-line"></i>
                        <div>Courses</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'cohorts.php' ? ' active' : ''; ?>">
                    <a href="cohorts.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-team-line"></i>
                        <div>Cohorts</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'enrollments.php' ? ' active' : ''; ?>">
                    <a href="enrollments.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-user-3-line"></i>
                        <div>Enrollments</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'agreement_templates.php' ? ' active' : ''; ?>">
                    <a href="agreement_templates.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-file-sign-line"></i>
                        <div>Agreements</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'categories.php' ? ' active' : ''; ?>">
                    <a href="categories.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-folder-3-line"></i>
                        <div>Categories</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'assessments.php' ? ' active' : ''; ?>">
                    <a href="assessments.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-list-check"></i>
                        <div>Assessments</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'certificates.php' ? ' active' : ''; ?>">
                    <a href="certificates.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-award-line"></i>
                        <div>Certificates</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'payments.php' ? ' active' : ''; ?>">
                    <a href="payments.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-bank-card-line"></i>
                        <div>Payments</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'appointments.php' ? ' active' : ''; ?>">
                    <a href="appointments.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-calendar-check-line"></i>
                        <div>Appointments</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'whatsapp.php' ? ' active' : ''; ?>">
                    <a href="whatsapp.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-whatsapp-line"></i>
                        <div>WhatsApp</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'reports.php' ? ' active' : ''; ?>">
                    <a href="reports.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-bar-chart-2-line"></i>
                        <div>Reports</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'analytics.php' ? ' active' : ''; ?>">
                    <a href="analytics.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-line-chart-line"></i>
                        <div>Analytics</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'site_content.php' ? ' active' : ''; ?>">
                    <a href="site_content.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-file-list-3-line"></i>
                        <div>Site Content</div>
                    </a>
                </li>
                <li class="menu-item<?php echo $currentPage === 'settings.php' ? ' active' : ''; ?>">
                    <a href="settings.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-settings-3-line"></i>
                        <div>Settings</div>
                    </a>
                </li>

                <li class="menu-header small text-uppercase"><span class="menu-header-text">LMS</span></li>
                <li class="menu-item">
                    <a href="../lms/dashboard.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-layout-4-line"></i>
                        <div>Back to LMS</div>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="../lms/logout.php" class="menu-link">
                        <i class="menu-icon icon-base ri ri-logout-box-r-line"></i>
                        <div>Logout</div>
                    </a>
                </li>
            </ul>
        </aside>

        <div class="layout-page">
            <nav class="layout-navbar container-xxl navbar-detached navbar navbar-expand-xl align-items-center bg-navbar-theme" id="layout-navbar">
                <div class="layout-menu-toggle navbar-nav align-items-xl-center me-4 me-xl-0 d-xl-none">
                    <a class="nav-item nav-link px-0 me-xl-6" href="javascript:void(0)">
                        <i class="icon-base ri ri-menu-line icon-md"></i>
                    </a>
                </div>

                <div class="navbar-nav-right d-flex align-items-center justify-content-end" id="navbar-collapse">
                    <div class="navbar-nav align-items-center">
                        <div class="nav-item d-flex align-items-center">
                            <i class="icon-base ri ri-search-line icon-lg lh-0"></i>
                            <input type="text" class="form-control border-0 shadow-none" placeholder="Search..." aria-label="Search..." />
                        </div>
                    </div>

                    <ul class="navbar-nav flex-row align-items-center ms-md-auto">
                        <li class="nav-item navbar-dropdown dropdown-user dropdown">
                            <a class="nav-link dropdown-toggle hide-arrow p-0" href="javascript:void(0);" data-bs-toggle="dropdown">
                                <div class="avatar avatar-online">
                                    <span class="avatar-initial rounded-circle bg-primary">
                                        <?php echo isset($user['first_name']) ? strtoupper(substr($user['first_name'], 0, 1)) : 'A'; ?>
                                    </span>
                                </div>
                            </a>
                            <ul class="dropdown-menu dropdown-menu-end">
                                <li>
                                    <a class="dropdown-item" href="profile.php">
                                        <i class="icon-base ri ri-user-line icon-md me-3"></i>
                                        <span>My Profile</span>
                                    </a>
                                </li>
                                <li>
                                    <a class="dropdown-item" href="settings.php">
                                        <i class="icon-base ri ri-settings-3-line icon-md me-3"></i>
                                        <span>Settings</span>
                                    </a>
                                </li>
                                <li>
                                    <div class="dropdown-divider my-1"></div>
                                </li>
                                <li>
                                    <div class="d-grid px-4 pt-2 pb-1">
                                        <a class="btn btn-danger d-flex" href="../lms/logout.php">
                                            <small class="align-middle">Logout</small>
                                            <i class="ri ri-logout-box-r-line ms-2 ri-xs"></i>
                                        </a>
                                    </div>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </div>
            </nav>

            <div class="content-wrapper">
                <div class="container-xxl flex-grow-1 container-p-y">
                    <?php if (isset($_SESSION['flash'])): ?>
                        <?php foreach ($_SESSION['flash'] as $type => $message): ?>
                            <div class="alert alert-<?php echo $type; ?> alert-dismissible fade show" role="alert">
                                <?php echo htmlspecialchars($message); ?>
                                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                            </div>
                        <?php endforeach; ?>
                        <?php unset($_SESSION['flash']); ?>
                    <?php endif; ?>
