<?php
require_once('../source/session.php');
require_once('../source/error.php');
require_once('../source/dbconfig.php');
require_once('../source/function.php');
require_once('../source/restrict_admin.php');

// Get admin name from session
$adminName = $_SESSION['SESS_FULLNAME_ADMIN'] ?? 'Administrator';

// School information
$schoolName = "Henzy Royal International School";
$schoolAddress = "No 17 Amadi Street, Amakohia, Uratta Owerri Imo State Nigeria";
$supportPhone = "+234 803 292 9756";
$supportEmail = "support@hris.com.ng";

// Session timeout settings (in minutes)
define('WARNING_TIME', 15);
define('TIMEOUT_TIME', 10);

// Update session last activity time
if (!isset($_SESSION['last_activity'])) {
    $_SESSION['last_activity'] = time();
    $_SESSION['warning_shown'] = false;
    $_SESSION['timeout_triggered'] = false;
}

// Get statistics for dashboard using correct table names
try {
    // Count total exams from tbl_exam
    $totalExams = sql($DBH, "SELECT COUNT(*) as count FROM tbl_exam", [], "row")['count'] ?? 0;
    
    // Count total questions from tbl_question
    $totalQuestions = sql($DBH, "SELECT COUNT(*) as count FROM tbl_question", [], "row")['count'] ?? 0;
    
    // Count distinct students who have taken exams from tbl_cbt_results
    $totalParticipants = sql($DBH, "SELECT COUNT(DISTINCT registration_no) as count FROM tbl_cbt_results", [], "row")['count'] ?? 0;
    
    // Count total results/attempts
    $totalAttempts = sql($DBH, "SELECT COUNT(*) as count FROM tbl_cbt_results", [], "row")['count'] ?? 0;
    
    // Count classes with exams
    $totalClasses = sql($DBH, "SELECT COUNT(DISTINCT class) as count FROM tbl_exam_class", [], "row")['count'] ?? 0;
    
    // Count distinct subjects with questions
    $totalSubjects = sql($DBH, "SELECT COUNT(DISTINCT subject_id) as count FROM tbl_question", [], "row")['count'] ?? 0;
    
    // Get average score from results
    $avgScore = sql($DBH, "SELECT AVG(score) as avg FROM tbl_cbt_results", [], "row")['avg'] ?? 0;
    
    // Get pass rate (scores >= 40)
    $passCount = sql($DBH, "SELECT COUNT(*) as count FROM tbl_cbt_results WHERE score >= 40", [], "row")['count'] ?? 0;
    $passRate = ($totalAttempts > 0) ? round(($passCount / $totalAttempts) * 100, 1) : 0;
    
    // Get recent exams (last 5)
    $recentExams = sql($DBH, "SELECT e.exam_name, ec.class, ec.term, ec.session 
                              FROM tbl_exam_class ec 
                              JOIN tbl_exam e ON ec.exam_id = e.id 
                              ORDER BY ec.id DESC LIMIT 5", [], "all");
    
    // Get top performing subjects
    $topSubjects = sql($DBH, "SELECT subject, AVG(score) as avg_score, COUNT(*) as attempts 
                             FROM tbl_cbt_results 
                             GROUP BY subject 
                             ORDER BY avg_score DESC 
                             LIMIT 5", [], "all");
    
} catch (Exception $e) {
    // Tables might not exist yet - set counts to 0
    $totalExams = 0;
    $totalQuestions = 0;
    $totalParticipants = 0;
    $totalAttempts = 0;
    $totalClasses = 0;
    $totalSubjects = 0;
    $avgScore = 0;
    $passRate = 0;
    $recentExams = [];
    $topSubjects = [];
}

// Check session timeout status
$time_elapsed = time() - $_SESSION['last_activity'];
$warning_time_seconds = WARNING_TIME * 60;
$timeout_time_seconds = TIMEOUT_TIME * 60;

$show_warning = false;
$time_until_timeout = 0;

if ($time_elapsed >= $warning_time_seconds && !$_SESSION['warning_shown'] && !$_SESSION['timeout_triggered']) {
    $show_warning = true;
    $_SESSION['warning_shown'] = true;
    $time_until_timeout = $timeout_time_seconds - ($time_elapsed - $warning_time_seconds);
} elseif ($time_elapsed >= ($warning_time_seconds + $timeout_time_seconds) && !$_SESSION['timeout_triggered']) {
    $_SESSION['timeout_triggered'] = true;
    session_destroy();
    header("Location: ../logout.php?timeout=1");
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CBT Admin Dashboard - Henzy Royal International School</title>
    <link rel="icon" type="image/png" href="../coll_logo.png">
    
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <!-- Google Fonts - Poppins -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <!-- Chart.js for analytics -->
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    
    <!-- Custom CSS -->
    <style>
        :root {
            --primary-blue: #1a4b8c;    /* Royal Blue */
            --secondary-gold: #FFD700;    /* Gold */
            --dark-blue: #0f2b4f;         /* Dark Blue */
            --light-blue: #4a7db5;         /* Medium Blue */
            --light-gold: #FFF1B8;          /* Light Gold */
            --bg-light: #f5f9ff;
            --success-green: #28a745;
            --info-teal: #17a2b8;
            --warning-orange: #fd7e14;
            --danger-red: #dc3545;
        }
        
        body {
            font-family: 'Poppins', sans-serif;
            background-color: var(--bg-light);
            color: #333;
            padding-bottom: 60px;
        }
        
        /* Header Styles */
        .admin-header {
            background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue));
            box-shadow: 0 2px 15px rgba(26, 75, 140, 0.3);
            border-bottom: 3px solid var(--secondary-gold);
            color: white;
        }
        
        .logo-container {
            background: white;
            border-radius: 10px;
            padding: 5px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.2);
        }
        
        .img-fluid {
            width: 60px;
            height: 60px;
            object-fit: cover;
            border-radius: 8px;
        }
        
        .header-title h1 {
            font-weight: 700;
            margin-bottom: 0;
            font-size: 1.5rem;
            color: white;
        }
        
        .header-title small {
            color: var(--secondary-gold);
            font-weight: 500;
        }
        
        /* Dropdown Styles */
        .admin-dropdown {
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 215, 0, 0.3);
            color: white;
            padding: 0.5rem 1rem;
            border-radius: 50px;
            transition: all 0.3s ease;
        }
        
        .admin-dropdown:hover {
            background: rgba(255, 215, 0, 0.2);
            border-color: var(--secondary-gold);
        }
        
        .dropdown-menu {
            border: 1px solid var(--secondary-gold);
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
        }
        
        .dropdown-item:hover {
            background-color: var(--light-gold);
            color: var(--dark-blue);
        }
        
        .dropdown-item i {
            color: var(--primary-blue);
        }
        
        /* Modal Styles */
        .modal-content {
            border: none;
            border-radius: 15px;
            overflow: hidden;
            border: 2px solid var(--secondary-gold);
        }
        
        .modal-header {
            background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue));
            color: white;
            border-bottom: 2px solid var(--secondary-gold);
        }
        
        .modal-header .btn-close {
            filter: brightness(0) invert(1);
        }
        
        .modal-footer {
            border-top: 1px solid var(--light-gold);
            padding: 1.5rem;
        }
        
        .modal-body {
            padding: 2rem;
        }
        
        /* Welcome Banner */
        .welcome-banner {
            background: linear-gradient(135deg, #f8f9fa, #ffffff);
            border-radius: 15px;
            padding: 2rem;
            margin-bottom: 2rem;
            border: 1px solid rgba(255, 215, 0, 0.2);
            position: relative;
            overflow: hidden;
        }
        
        .welcome-banner::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, var(--secondary-gold), var(--primary-blue));
        }
        
        .welcome-banner h3 {
            color: var(--dark-blue);
            font-weight: 700;
            margin-bottom: 0.5rem;
        }
        
        .welcome-banner p {
            color: #666;
            margin-bottom: 0;
        }
        
        .welcome-icon {
            width: 80px;
            height: 80px;
            background: linear-gradient(135deg, var(--light-gold), #ffffff);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            border: 3px solid var(--secondary-gold);
        }
        
        .welcome-icon i {
            font-size: 2.5rem;
            color: var(--primary-blue);
        }
        
        /* Stat Cards */
        .stat-card {
            background: white;
            border-radius: 15px;
            padding: 1.5rem;
            box-shadow: 0 4px 20px rgba(26, 75, 140, 0.1);
            border: 1px solid rgba(255, 215, 0, 0.2);
            transition: all 0.3s ease;
            height: 100%;
            position: relative;
            overflow: hidden;
        }
        
        .stat-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 3px;
            background: linear-gradient(90deg, var(--secondary-gold), var(--primary-blue));
        }
        
        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(26, 75, 140, 0.2);
            border-color: var(--secondary-gold);
        }
        
        .stat-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, var(--light-gold), #ffffff);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 1rem;
            border: 2px solid var(--secondary-gold);
        }
        
        .stat-icon i {
            font-size: 1.8rem;
            color: var(--primary-blue);
        }
        
        .stat-number {
            font-size: 2.2rem;
            font-weight: 700;
            color: var(--dark-blue);
            line-height: 1.2;
        }
        
        .stat-label {
            color: #666;
            font-weight: 500;
            font-size: 0.9rem;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        .stat-trend {
            font-size: 0.8rem;
            margin-top: 0.5rem;
            color: var(--success-green);
        }
        
        .stat-trend i {
            margin-right: 0.25rem;
        }
        
        /* Action Cards */
        .action-card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 4px 15px rgba(26, 75, 140, 0.1);
            transition: all 0.3s ease;
            height: 100%;
            background: white;
            border: 1px solid rgba(255, 215, 0, 0.2);
            overflow: hidden;
            position: relative;
        }
        
        .action-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, var(--secondary-gold), var(--primary-blue));
        }
        
        .action-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(26, 75, 140, 0.2);
            border-color: var(--secondary-gold);
        }
        
        .card-icon-wrapper {
            width: 70px;
            height: 70px;
            background: linear-gradient(135deg, var(--light-gold), #ffffff);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 1rem;
            border: 2px solid var(--secondary-gold);
        }
        
        .card-icon {
            font-size: 2rem;
            color: var(--primary-blue);
        }
        
        .card-title {
            font-weight: 600;
            color: var(--dark-blue);
            margin-bottom: 0.5rem;
        }
        
        .card-text {
            color: #666;
            font-size: 0.9rem;
            margin-bottom: 1.5rem;
        }
        
        /* Section Headers */
        .section-header {
            position: relative;
            margin-bottom: 2rem;
            padding-bottom: 0.5rem;
            font-weight: 700;
            color: var(--dark-blue);
        }
        
        .section-header::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 50px;
            height: 3px;
            background: linear-gradient(90deg, var(--secondary-gold), var(--primary-blue));
            border-radius: 3px;
        }
        
        .section-header i {
            color: var(--secondary-gold);
            margin-right: 0.5rem;
        }
        
        /* Buttons */
        .btn-custom {
            padding: 0.6rem 1.5rem;
            border-radius: 50px;
            font-weight: 500;
            transition: all 0.3s ease;
            border: none;
            color: white;
            width: 100%;
            position: relative;
            overflow: hidden;
        }
        
        .btn-custom::after {
            content: '';
            position: absolute;
            top: 50%;
            left: 50%;
            width: 0;
            height: 0;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.2);
            transform: translate(-50%, -50%);
            transition: width 0.3s, height 0.3s;
        }
        
        .btn-custom:hover::after {
            width: 200px;
            height: 200px;
        }
        
        .btn-primary {
            background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue));
        }
        
        .btn-primary:hover {
            background: linear-gradient(135deg, var(--dark-blue), #0a1f3a);
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(26, 75, 140, 0.3);
        }
        
        .btn-success {
            background: linear-gradient(135deg, #28a745, #218838);
        }
        
        .btn-success:hover {
            background: linear-gradient(135deg, #218838, #1e7e34);
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(40, 167, 69, 0.3);
        }
        
        .btn-info {
            background: linear-gradient(135deg, #17a2b8, #138496);
        }
        
        .btn-info:hover {
            background: linear-gradient(135deg, #138496, #117a8b);
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(23, 162, 184, 0.3);
        }
        
        .btn-warning-custom {
            background: linear-gradient(135deg, #fd7e14, #e56b08);
        }
        
        .btn-warning-custom:hover {
            background: linear-gradient(135deg, #e56b08, #c45806);
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(253, 126, 20, 0.3);
        }
        
        .btn-exam {
            background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue));
        }
        
        .btn-questions {
            background: linear-gradient(135deg, #6f42c1, #5a32a3);
        }
        
        .btn-questions:hover {
            background: linear-gradient(135deg, #5a32a3, #4a2a83);
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(111, 66, 193, 0.3);
        }
        
        .btn-results {
            background: linear-gradient(135deg, #e83e8c, #c72d7a);
        }
        
        .btn-results:hover {
            background: linear-gradient(135deg, #c72d7a, #a32263);
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(232, 62, 140, 0.3);
        }
        
        /* Info Cards */
        .info-card {
            background: white;
            border-radius: 15px;
            padding: 1.5rem;
            border: 1px solid rgba(255, 215, 0, 0.2);
            height: 100%;
        }
        
        .info-card h5 {
            color: var(--dark-blue);
            font-weight: 600;
            margin-bottom: 1rem;
            padding-bottom: 0.5rem;
            border-bottom: 2px solid var(--light-gold);
        }
        
        .info-card h5 i {
            color: var(--secondary-gold);
            margin-right: 0.5rem;
        }
        
        .exam-item, .subject-item {
            padding: 0.75rem;
            border-bottom: 1px solid rgba(255, 215, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .exam-item:hover, .subject-item:hover {
            background: var(--light-gold);
            transform: translateX(5px);
        }
        
        .exam-item:last-child, .subject-item:last-child {
            border-bottom: none;
        }
        
        .exam-name {
            font-weight: 600;
            color: var(--dark-blue);
        }
        
        .exam-details {
            font-size: 0.8rem;
            color: #666;
        }
        
        .subject-score {
            font-size: 0.9rem;
            font-weight: 600;
        }
        
        .progress {
            height: 6px;
            border-radius: 3px;
            background-color: #f0f0f0;
        }
        
        .progress-bar {
            background: linear-gradient(90deg, var(--secondary-gold), var(--primary-blue));
            border-radius: 3px;
        }
        
        /* Quick Actions */
        .quick-actions {
            background: linear-gradient(135deg, #f8f9fa, #ffffff);
            border-radius: 15px;
            padding: 1.5rem;
            border: 1px solid rgba(255, 215, 0, 0.2);
            margin-top: 1rem;
        }
        
        .quick-actions h5 {
            color: var(--dark-blue);
            font-weight: 600;
            margin-bottom: 1rem;
        }
        
        .quick-actions h5 i {
            color: var(--secondary-gold);
        }
        
        .quick-action-item {
            display: flex;
            align-items: center;
            padding: 0.75rem;
            border-radius: 10px;
            transition: all 0.3s ease;
            color: var(--dark-blue);
            text-decoration: none;
        }
        
        .quick-action-item:hover {
            background: var(--light-gold);
            transform: translateX(5px);
            color: var(--dark-blue);
        }
        
        .quick-action-item i {
            width: 30px;
            color: var(--primary-blue);
            font-size: 1.2rem;
        }
        
        .quick-action-item span {
            font-weight: 500;
        }
        
        /* Footer */
        .footer {
            background: linear-gradient(135deg, var(--dark-blue), var(--primary-blue));
            color: white;
            padding: 1.5rem 0;
            margin-top: 3rem;
            border-top: 3px solid var(--secondary-gold);
        }
        
        .footer a {
            color: var(--secondary-gold);
            text-decoration: none;
            font-weight: 500;
        }
        
        .footer a:hover {
            text-decoration: underline;
        }
        
        .footer img {
            height: 25px;
            margin-left: 5px;
            filter: brightness(0) invert(1);
        }
        
        /* Responsive */
        @media (max-width: 768px) {
            .header-title h1 {
                font-size: 1.2rem;
            }
            
            .admin-dropdown {
                padding: 0.4rem 0.8rem;
                font-size: 0.9rem;
            }
            
            .stat-number {
                font-size: 1.8rem;
            }
            
            .card-icon-wrapper {
                width: 60px;
                height: 60px;
            }
            
            .card-icon {
                font-size: 1.6rem;
            }
            
            .welcome-banner {
                padding: 1.5rem;
            }
        }
    </style>
</head>
<body>
    <!-- Session Timeout Modal -->
    <div class="modal fade" id="sessionTimeoutModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">
                        <i class="fas fa-clock me-2" style="color: var(--secondary-gold);"></i>
                        Session Timeout Warning
                    </h5>
                </div>
                <div class="modal-body text-center py-4">
                    <i class="fas fa-exclamation-triangle fa-3x mb-3" style="color: var(--secondary-gold);"></i>
                    <h4 class="mb-3">Your session is about to expire!</h4>
                    <p class="mb-2">You have been inactive for <strong><?= WARNING_TIME ?> minutes</strong>.</p>
                    <p class="mb-4">Your session will expire in <strong><span id="timeoutTimer"><?= TIMEOUT_TIME ?>:00</span> minutes</strong> if you don't respond.</p>
                    <div class="progress mb-3" style="height: 10px;">
                        <div id="timeoutProgress" class="progress-bar progress-bar-striped progress-bar-animated" 
                             style="width: 100%; background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue));"></div>
                    </div>
                </div>
                <div class="modal-footer justify-content-center">
                    <button type="button" class="btn btn-primary" onclick="keepSessionAlive()" id="keepAliveBtn">
                        <i class="fas fa-check-circle me-2"></i> I'm Still Here - Don't Time Out
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- Session Expired Modal -->
    <div class="modal fade" id="sessionExpiredModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header bg-danger text-white">
                    <h5 class="modal-title">
                        <i class="fas fa-exclamation-circle me-2"></i>
                        Session Expired
                    </h5>
                </div>
                <div class="modal-body text-center py-4">
                    <i class="fas fa-hourglass-end fa-3x mb-3 text-danger"></i>
                    <h4 class="mb-3">Your session has expired!</h4>
                    <p class="mb-4">You have been logged out due to inactivity.</p>
                </div>
                <div class="modal-footer justify-content-center">
                    <a href="../login.php" class="btn btn-primary">
                        <i class="fas fa-sign-in-alt me-2"></i> Login Again
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Header -->
    <header class="admin-header py-3">
        <div class="container">
            <div class="d-flex justify-content-between align-items-center">
                <div class="d-flex align-items-center">
                    <div class="logo-container me-3">
                        <img src="../coll_logo.png" alt="Henzy Royal International School Logo" class="img-fluid">
                    </div>
                    <div class="header-title">
                        <h1 class="h4 mb-0">HENZY ROYAL INT'L SCHOOL</h1>
                        <small>CBT Admin Dashboard</small>
                    </div>
                </div>
                <div class="dropdown">
                    <button class="btn admin-dropdown dropdown-toggle" type="button" id="adminDropdown" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fas fa-user-circle me-1"></i> <?= htmlspecialchars($adminName) ?>
                    </button>
                    <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="adminDropdown">
                        <li><a class="dropdown-item" href="profile.php"><i class="fas fa-user me-2"></i> Profile</a></li>
                        <li><a class="dropdown-item" href="settings.php"><i class="fas fa-cog me-2"></i> Settings</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item" href="../main.php"><i class="fas fa-tachometer-alt me-2"></i> Main Dashboard</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item" href="../logout.php"><i class="fas fa-sign-out-alt me-2"></i> Logout</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <div class="container py-4">
        <!-- Welcome Banner -->
        <div class="row mb-4">
            <div class="col-12">
                <div class="welcome-banner d-flex align-items-center justify-content-between">
                    <div>
                        <h3>Welcome back, <?= htmlspecialchars($adminName) ?>! 👋</h3>
                        <p class="text-muted mb-0">Manage all computer-based test activities from this dashboard. Here's what's happening with your CBT system today.</p>
                    </div>
                    <div class="welcome-icon d-none d-md-flex">
                        <i class="fas fa-crown"></i>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Statistics Cards -->
        <div class="row mb-4">
            <div class="col-md-3 mb-3">
                <div class="stat-card">
                    <div class="stat-icon">
                        <i class="fas fa-book-open"></i>
                    </div>
                    <div class="stat-number"><?= number_format($totalExams) ?></div>
                    <div class="stat-label">Total Exams</div>
                    <div class="stat-trend">
                        <i class="fas fa-tag"></i> <?= number_format($totalClasses) ?> classes
                    </div>
                </div>
            </div>
            
            <div class="col-md-3 mb-3">
                <div class="stat-card">
                    <div class="stat-icon">
                        <i class="fas fa-question-circle"></i>
                    </div>
                    <div class="stat-number"><?= number_format($totalQuestions) ?></div>
                    <div class="stat-label">Total Questions</div>
                    <div class="stat-trend">
                        <i class="fas fa-book"></i> <?= number_format($totalSubjects) ?> subjects
                    </div>
                </div>
            </div>
            
            <div class="col-md-3 mb-3">
                <div class="stat-card">
                    <div class="stat-icon">
                        <i class="fas fa-users"></i>
                    </div>
                    <div class="stat-number"><?= number_format($totalParticipants) ?></div>
                    <div class="stat-label">Participants</div>
                    <div class="stat-trend">
                        <i class="fas fa-chart-line"></i> <?= number_format($totalAttempts) ?> attempts
                    </div>
                </div>
            </div>
            
            <div class="col-md-3 mb-3">
                <div class="stat-card">
                    <div class="stat-icon">
                        <i class="fas fa-chart-pie"></i>
                    </div>
                    <div class="stat-number"><?= round($avgScore, 1) ?>%</div>
                    <div class="stat-label">Average Score</div>
                    <div class="stat-trend">
                        <i class="fas fa-check-circle text-success"></i> <?= $passRate ?>% pass rate
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Exam Management Section -->
        <div class="row mb-4">
            <div class="col-12">
                <h4 class="section-header"><i class="fas fa-book-open"></i> Exam Management</h4>
            </div>
            
            <div class="col-md-4 mb-3">
                <div class="action-card">
                    <div class="card-body text-center p-4">
                        <div class="card-icon-wrapper">
                            <i class="fas fa-plus-circle card-icon"></i>
                        </div>
                        <h5 class="card-title">Create New Exam</h5>
                        <p class="card-text">Set up a new examination with custom settings and duration</p>
                        <a href="create_exam.php" class="btn btn-custom btn-primary">
                            <i class="fas fa-plus me-2"></i> Create Exam
                        </a>
                    </div>
                </div>
            </div>
            
            <div class="col-md-4 mb-3">
                <div class="action-card">
                    <div class="card-body text-center p-4">
                        <div class="card-icon-wrapper">
                            <i class="fas fa-users-class card-icon"></i>
                        </div>
                        <h5 class="card-title">Assign Classes</h5>
                        <p class="card-text">Manage which classes have access to specific examinations</p>
                        <a href="exam_class.php" class="btn btn-custom btn-primary">
                            <i class="fas fa-tasks me-2"></i> Assign Classes
                        </a>
                    </div>
                </div>
            </div>
            
            <div class="col-md-4 mb-3">
                <div class="action-card">
                    <div class="card-body text-center p-4">
                        <div class="card-icon-wrapper">
                            <i class="fas fa-edit card-icon"></i>
                        </div>
                        <h5 class="card-title">Add Subjects to Exams</h5>
                        <p class="card-text">Add and manage subjects for each examination</p>
                        <a href="add_subject_to_exam.php" class="btn btn-custom btn-primary">
                            <i class="fas fa-list me-2"></i> Add Subjects
                        </a>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Question Management Section -->
        <div class="row mb-4">
            <div class="col-12">
                <h4 class="section-header"><i class="fas fa-question-circle"></i> Question Management</h4>
            </div>
            
            <div class="col-md-4 mb-3">
                <div class="action-card">
                    <div class="card-body text-center p-4">
                        <div class="card-icon-wrapper">
                            <i class="fas fa-plus-square card-icon"></i>
                        </div>
                        <h5 class="card-title">Set New Questions</h5>
                        <p class="card-text">Create new question sets with rich text and math support</p>
                        <a href="set_questions.php" class="btn btn-custom btn-questions">
                            <i class="fas fa-plus me-2"></i> Add Questions
                        </a>
                    </div>
                </div>
            </div>
            
            <div class="col-md-4 mb-3">
                <div class="action-card">
                    <div class="card-body text-center p-4">
                        <div class="card-icon-wrapper">
                            <i class="fas fa-layer-group card-icon"></i>
                        </div>
                        <h5 class="card-title">Add to Existing Sets</h5>
                        <p class="card-text">Add more questions to your current question bank</p>
                        <a href="add_question.php" class="btn btn-custom btn-questions">
                            <i class="fas fa-plus-circle me-2"></i> Add More
                        </a>
                    </div>
                </div>
            </div>
            
            <div class="col-md-4 mb-3">
                <div class="action-card">
                    <div class="card-body text-center p-4">
                        <div class="card-icon-wrapper">
                            <i class="fas fa-edit card-icon"></i>
                        </div>
                        <h5 class="card-title">Edit/Delete Questions</h5>
                        <p class="card-text">Modify or remove existing questions from the database</p>
                        <a href="manage_questions.php" class="btn btn-custom btn-questions">
                            <i class="fas fa-edit me-2"></i> Manage Questions
                        </a>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Results & Analysis Section -->
        <div class="row mb-4">
            <div class="col-12">
                <h4 class="section-header"><i class="fas fa-chart-bar"></i> Results & Analysis</h4>
            </div>
            
            <div class="col-md-4 mb-3">
                <div class="action-card">
                    <div class="card-body text-center p-4">
                        <div class="card-icon-wrapper">
                            <i class="fas fa-poll card-icon"></i>
                        </div>
                        <h5 class="card-title">View CBT Results</h5>
                        <p class="card-text">Analyze student performance with detailed statistics</p>
                        <a href="view_cbt_results.php" class="btn btn-custom btn-results">
                            <i class="fas fa-chart-pie me-2"></i> View Results
                        </a>
                    </div>
                </div>
            </div>
            
            <div class="col-md-4 mb-3">
                <div class="action-card">
                    <div class="card-body text-center p-4">
                        <div class="card-icon-wrapper">
                            <i class="fas fa-upload card-icon"></i>
                        </div>
                        <h5 class="card-title">Upload Other Scores</h5>
                        <p class="card-text">Upload subjective, theory, and practical scores</p>
                        <a href="upload_other_scores.php" class="btn btn-custom btn-results">
                            <i class="fas fa-upload me-2"></i> Upload Scores
                        </a>
                    </div>
                </div>
            </div>
            
            <div class="col-md-4 mb-3">
                <div class="action-card">
                    <div class="card-body text-center p-4">
                        <div class="card-icon-wrapper">
                            <i class="fas fa-chart-line card-icon"></i>
                        </div>
                        <h5 class="card-title">Performance Analytics</h5>
                        <p class="card-text">Deep dive into performance trends and insights</p>
                        <a href="analytics.php" class="btn btn-custom btn-warning-custom">
                            <i class="fas fa-analytics me-2"></i> View Analytics
                        </a>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Recent Activity & Top Subjects -->
        <div class="row">
            <div class="col-md-6 mb-3">
                <div class="info-card">
                    <h5><i class="fas fa-history"></i> Recent Exams</h5>
                    <?php if (!empty($recentExams)): ?>
                        <?php foreach ($recentExams as $exam): ?>
                            <div class="exam-item">
                                <div class="exam-name"><?= htmlspecialchars($exam['exam_name']) ?></div>
                                <div class="exam-details">
                                    <i class="fas fa-users me-1"></i> <?= htmlspecialchars($exam['class']) ?>
                                    <i class="fas fa-calendar-alt ms-2 me-1"></i> <?= htmlspecialchars($exam['term']) ?> <?= htmlspecialchars($exam['session']) ?>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <p class="text-muted text-center py-3">No recent exams found</p>
                    <?php endif; ?>
                </div>
            </div>
            
            <div class="col-md-6 mb-3">
                <div class="info-card">
                    <h5><i class="fas fa-trophy"></i> Top Performing Subjects</h5>
                    <?php if (!empty($topSubjects)): ?>
                        <?php foreach ($topSubjects as $subject): ?>
                            <div class="subject-item">
                                <div class="d-flex justify-content-between align-items-center">
                                    <span class="exam-name"><?= htmlspecialchars($subject['subject']) ?></span>
                                    <span class="subject-score text-success"><?= round($subject['avg_score'], 1) ?>%</span>
                                </div>
                                <div class="d-flex justify-content-between align-items-center mt-1">
                                    <small class="text-muted"><?= $subject['attempts'] ?> attempts</small>
                                    <div class="progress flex-grow-1 ms-2" style="height: 6px;">
                                        <div class="progress-bar" style="width: <?= $subject['avg_score'] ?>%"></div>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <p class="text-muted text-center py-3">No performance data yet</p>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        
        <!-- Quick Actions -->
        <div class="row">
            <div class="col-12">
                <div class="quick-actions">
                    <h5><i class="fas fa-bolt me-2" style="color: var(--secondary-gold);"></i>Quick Actions</h5>
                    <div class="row">
                        <div class="col-md-3">
                            <a href="create_exam.php" class="quick-action-item">
                                <i class="fas fa-plus-circle"></i>
                                <span>Create Exam</span>
                            </a>
                        </div>
                        <div class="col-md-3">
                            <a href="set_questions.php" class="quick-action-item">
                                <i class="fas fa-question-circle"></i>
                                <span>Add Questions</span>
                            </a>
                        </div>
                        <div class="col-md-3">
                            <a href="view_cbt_results.php" class="quick-action-item">
                                <i class="fas fa-poll"></i>
                                <span>View Results</span>
                            </a>
                        </div>
                        <div class="col-md-3">
                            <a href="../main.php" class="quick-action-item">
                                <i class="fas fa-tachometer-alt"></i>
                                <span>Main Dashboard</span>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <footer class="footer">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <p class="mb-1"><i class="fas fa-crown me-2" style="color: var(--secondary-gold);"></i><strong>Henzy Royal International School</strong> - CBT Management System</p>
                    <p class="mb-0 small"><i class="fas fa-map-marker-alt me-1" style="color: var(--secondary-gold);"></i> <?= $schoolAddress ?></p>
                </div>
                <div class="col-md-4 text-md-end">
                    <p class="mb-0">Powered By <a href="https://dataswift.com.ng" target="_blank">DataSwift Coders</a> 
                    <img src="../datalogo.png" alt="DataSwift Logo"></p>
                    <small>&copy; <?= date('Y') ?> All Rights Reserved</small>
                </div>
            </div>
        </div>
    </footer>

    <!-- JavaScript Libraries -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    <script>
        let timeoutTimer;
        let timeoutProgress;

        $(document).ready(function() {
            <?php if ($show_warning): ?>
                showSessionTimeoutModal(<?= $time_until_timeout ?>);
            <?php endif; ?>
        });

        // Session timeout functions
        function showSessionTimeoutModal(secondsLeft) {
            const modal = new bootstrap.Modal(document.getElementById('sessionTimeoutModal'));
            modal.show();
            
            let timeLeft = secondsLeft;
            timeoutTimer = setInterval(function() {
                timeLeft--;
                if (timeLeft <= 0) {
                    clearInterval(timeoutTimer);
                    document.getElementById('sessionTimeoutModal').classList.remove('show');
                    showSessionExpiredModal();
                } else {
                    updateTimeoutDisplay(timeLeft);
                }
            }, 1000);
        }

        function updateTimeoutDisplay(seconds) {
            const minutes = Math.floor(seconds / 60);
            const secs = seconds % 60;
            document.getElementById('timeoutTimer').textContent = 
                `${minutes}:${secs.toString().padStart(2, '0')}`;
            
            const progressPercent = (seconds / (<?= TIMEOUT_TIME ?> * 60)) * 100;
            document.getElementById('timeoutProgress').style.width = progressPercent + '%';
        }

        function keepSessionAlive() {
            $.ajax({
                url: window.location.href,
                method: 'POST',
                data: { keep_session_alive: true },
                success: function(response) {
                    bootstrap.Modal.getInstance(document.getElementById('sessionTimeoutModal')).hide();
                    clearInterval(timeoutTimer);
                }
            });
        }

        function showSessionExpiredModal() {
            const modal = new bootstrap.Modal(document.getElementById('sessionExpiredModal'));
            modal.show();
            setTimeout(function() {
                window.location.href = '../logout.php?timeout=1';
            }, 5000);
        }

        // Auto-refresh statistics (optional)
        function refreshStats() {
            // This could be implemented with AJAX to update stats without page refresh
            console.log('Refreshing statistics...');
        }
        
        // Refresh stats every 5 minutes
        setInterval(refreshStats, 300000);
    </script>
</body>
</html>