<?php
// Set timezone at the VERY TOP before anything else
date_default_timezone_set('Africa/Lagos');

require_once('source/dbconfig.php');
require_once('source/function.php');

session_start();

// Debug: Check current time
$currentTime = time();
$currentDateTime = date('Y-m-d H:i:s');
$currentDateOnly = date('Y-m-d');

// Redirect if not logged in
if (!isset($_SESSION['student']) || !isset($_SESSION['exam_info'])) {
    header("Location: cbt_index.php");
    exit();
}

// 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";
$schoolMotto = "Excellence, Integrity, Service";

// Initialize
$student = $_SESSION['student'];
$exam_info = $_SESSION['exam_info'];
$availableExams = [];
$futureExams = [];
$pastExams = [];
$completedExams = [];
$error = null;
$debug = [];

// Function to get assessment type badge class
function getAssessmentBadgeClass($type) {
    switch($type) {
        case 'CA1': return 'badge-ca1';
        case 'CA2': return 'badge-ca2';
        case 'CA3': return 'badge-ca3';
        case 'Term Exam': return 'badge-term';
        case 'Mock Exam': return 'badge-mock';
        default: return 'badge-secondary';
    }
}

// Function to get assessment contribution percentage
function getAssessmentContribution($type) {
    switch($type) {
        case 'CA1':
        case 'CA2':
        case 'CA3':
            return 20;
        case 'Term Exam':
            return 60;
        case 'Mock Exam':
            return 100;
        default:
            return 0;
    }
}

// Function to get assessment display name
function getAssessmentDisplayName($type) {
    switch($type) {
        case 'CA1':
        case 'CA2':
        case 'CA3':
            return 'Continuous Assessment';
        case 'Term Exam':
            return 'Terminal Examination';
        case 'Mock Exam':
            return 'Mock Examination';
        default:
            return 'Examination';
    }
}

try {
    // Set MySQL session timezone to match PHP
    $DBH->exec("SET time_zone = '+01:00'");
    
    // Debug: Get MySQL current time
    $stmt = $DBH->query("SELECT NOW() as mysql_now, CURDATE() as mysql_date");
    $mysqlTime = $stmt->fetch(PDO::FETCH_ASSOC);
    $debug['php_time'] = $currentDateTime;
    $debug['php_date'] = $currentDateOnly;
    $debug['mysql_time'] = $mysqlTime['mysql_now'];
    $debug['mysql_date'] = $mysqlTime['mysql_date'];
    
    // Check if exams exist for this class/term/session and assessment type
    $stmt = $DBH->prepare("
        SELECT 
            ec.exam_id,
            e.exam_name,
            e.exam_description,
            COUNT(es.id) AS subject_count
        FROM tbl_exam_class ec
        JOIN tbl_exam e ON ec.exam_id = e.id
        LEFT JOIN tbl_exam_subject es ON ec.exam_id = es.exam_id AND es.assessment_type = :assessment_type
        WHERE ec.class = :class
        AND ec.term = :term
        AND ec.session = :session
        GROUP BY ec.exam_id
        HAVING subject_count > 0
    ");
    
    $stmt->execute([
        'class' => $exam_info['class'],
        'term' => $exam_info['term'],
        'session' => $exam_info['session'],
        'assessment_type' => $exam_info['assessment_type']
    ]);
    
    $exams = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    if (empty($exams)) {
        $availableExams = [];
    } else {
        // Get all subjects for these exams with matching assessment type and completion status
        $stmt = $DBH->prepare("
            SELECT 
                es.id AS exam_subject_id,
                s.id AS subject_id,
                s.subject,
                es.exam_date,
                es.duration_minutes,
                es.assessment_type,
                cbt.id AS cbt_result_id,
                cbt.score AS cbt_score,
                cbt.date_taken,
                cbt.correct_answers,
                cbt.total_questions
            FROM tbl_exam_class ec
            JOIN tbl_exam_subject es ON ec.exam_id = es.exam_id
            JOIN tbl_subject s ON es.subject_id = s.id
            LEFT JOIN tbl_cbt_results cbt ON cbt.subject = s.subject
                                         AND cbt.registration_no = :reg_no
                                         AND cbt.term = ec.term
                                         AND cbt.session = ec.session
                                         AND cbt.assessment_type = es.assessment_type
            WHERE ec.class = :class
            AND ec.term = :term
            AND ec.session = :session
            AND es.assessment_type = :assessment_type
            ORDER BY es.exam_date ASC, s.subject ASC
        ");
        
        $stmt->execute([
            'reg_no' => $student['registration_no'],
            'class' => $exam_info['class'],
            'term' => $exam_info['term'],
            'session' => $exam_info['session'],
            'assessment_type' => $exam_info['assessment_type']
        ]);
        
        $allExams = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        // Get current date in PHP (already set to Africa/Lagos)
        $currentDate = new DateTime();
        $currentDate->setTime(0, 0, 0); // Start of today
        
        $debug['current_date_php'] = $currentDate->format('Y-m-d H:i:s');
        
        // Categorize exams
        foreach ($allExams as $exam) {
            // Parse exam date - handle different possible formats
            $examDate = new DateTime($exam['exam_date']);
            
            // Store original for debugging
            $debug['exams'][] = [
                'subject' => $exam['subject'],
                'exam_date_raw' => $exam['exam_date'],
                'exam_date_parsed' => $examDate->format('Y-m-d H:i:s')
            ];
            
            // Set to end of exam day (11:59:59 PM)
            $examDateEnd = clone $examDate;
            $examDateEnd->setTime(23, 59, 59);
            
            $isCompleted = !empty($exam['cbt_result_id']);
            
            if ($isCompleted) {
                $completedExams[] = $exam;
            } elseif ($examDateEnd < $currentDate) {
                // Exam date has passed (end of exam day is before today)
                $pastExams[] = $exam;
            } elseif ($examDate > $currentDate) {
                // Exam date is in the future (start of exam day is after today)
                $futureExams[] = $exam;
            } else {
                // Today's exams that are not yet taken
                // Check if current time is past exam time? 
                // For now, just check if it's today
                $availableExams[] = $exam;
            }
        }
    }
    
} catch (PDOException $e) {
    $error = "Database error: " . $e->getMessage();
} catch (Exception $e) {
    $error = "Error: " . $e->getMessage();
}

// Get contribution for current assessment type
$contribution = getAssessmentContribution($exam_info['assessment_type']);
$assessmentDisplay = getAssessmentDisplayName($exam_info['assessment_type']);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome - <?= $schoolName ?> CBT Portal</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 -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <style>
        :root {
            --primary-blue: #1a4b8c;
            --secondary-gold: #FFD700;
            --dark-blue: #0f2b4f;
            --light-blue: #4a7db5;
            --light-gold: #FFF1B8;
            --bg-light: #f5f9ff;
            --success-green: #28a745;
            --danger-red: #dc3545;
            --warning-orange: #fd7e14;
            --info-teal: #17a2b8;
        }
        
        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #e9ecef 100%);
            color: #333;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }
        
        /* Header Styles */
        .header {
            background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue));
            color: white;
            padding: 1.5rem 0;
            margin-bottom: 2rem;
            box-shadow: 0 4px 20px rgba(26, 75, 140, 0.3);
            border-bottom: 3px solid var(--secondary-gold);
        }
        
        .logo-container {
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .school-logo {
            width: 60px;
            height: 60px;
            object-fit: contain;
            background: white;
            padding: 8px;
            border-radius: 50%;
            box-shadow: 0 4px 15px rgba(0,0,0,0.2);
            border: 2px solid var(--secondary-gold);
            margin-right: 15px;
            transition: all 0.3s ease;
        }
        
        .school-logo:hover {
            transform: rotate(360deg);
        }
        
        .school-title h1 {
            font-weight: 700;
            margin-bottom: 0;
            font-size: 1.5rem;
        }
        
        .school-title small {
            color: var(--secondary-gold);
            font-weight: 500;
        }
        
        /* 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;
            box-shadow: 0 4px 15px rgba(26, 75, 140, 0.1);
        }
        
        .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 h2 {
            color: var(--dark-blue);
            font-weight: 700;
            margin-bottom: 0.5rem;
        }
        
        .info-badge {
            background: linear-gradient(135deg, var(--light-gold), #ffffff);
            border-radius: 50px;
            padding: 0.5rem 1rem;
            margin-right: 0.5rem;
            margin-bottom: 0.5rem;
            display: inline-block;
            border: 1px solid var(--secondary-gold);
            color: var(--dark-blue);
            font-weight: 500;
        }
        
        .info-badge i {
            color: var(--secondary-gold);
            margin-right: 0.5rem;
        }
        
        /* Assessment Type Badge */
        .assessment-badge {
            display: inline-block;
            padding: 0.4rem 1rem;
            border-radius: 50px;
            font-size: 0.85rem;
            font-weight: 600;
            margin-left: 0.5rem;
        }
        
        .badge-ca1 { background: linear-gradient(135deg, #17a2b8, #138496); color: white; }
        .badge-ca2 { background: linear-gradient(135deg, #28a745, #218838); color: white; }
        .badge-ca3 { background: linear-gradient(135deg, #ffc107, #e0a800); color: var(--dark-blue); }
        .badge-term { background: linear-gradient(135deg, #6610f2, #520dc2); color: white; }
        .badge-mock { background: linear-gradient(135deg, #fd7e14, #e56b08); color: white; }
        .badge-secondary { background: linear-gradient(135deg, #6c757d, #545b62); color: white; }
        
        /* Section Headers */
        .section-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 1.5rem;
            padding-bottom: 0.5rem;
            border-bottom: 2px solid var(--secondary-gold);
        }
        
        .section-header h4 {
            color: var(--dark-blue);
            font-weight: 600;
            margin: 0;
        }
        
        .section-header i {
            color: var(--secondary-gold);
            margin-right: 0.5rem;
        }
        
        .section-count {
            background: linear-gradient(135deg, #f8f9fa, #ffffff);
            padding: 0.25rem 1rem;
            border-radius: 50px;
            border: 1px solid var(--secondary-gold);
            color: var(--dark-blue);
            font-weight: 600;
        }
        
        /* Contribution Info */
        .contribution-info {
            background: linear-gradient(135deg, #e3f2fd, #bbdefb);
            border-radius: 10px;
            padding: 0.75rem 1.5rem;
            display: inline-flex;
            align-items: center;
            gap: 0.5rem;
            border-left: 3px solid var(--secondary-gold);
            margin-left: 1rem;
        }
        
        .contribution-info i {
            color: var(--secondary-gold);
        }
        
        /* Exam Cards */
        .exam-card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 4px 15px rgba(26, 75, 140, 0.1);
            transition: all 0.3s ease;
            margin-bottom: 1.5rem;
            border: 1px solid rgba(255, 215, 0, 0.2);
            overflow: hidden;
            position: relative;
        }
        
        .exam-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
        }
        
        .exam-card.available-exam::before {
            background: linear-gradient(90deg, #17a2b8, #138496);
        }
        
        .exam-card.future-exam::before {
            background: linear-gradient(90deg, #ffc107, #e0a800);
        }
        
        .exam-card.past-exam::before {
            background: linear-gradient(90deg, #dc3545, #c82333);
        }
        
        .exam-card.completed-exam::before {
            background: linear-gradient(90deg, #28a745, #218838);
        }
        
        .exam-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(26, 75, 140, 0.2);
            border-color: var(--secondary-gold);
        }
        
        .exam-header {
            padding: 1.5rem;
            background: linear-gradient(135deg, #f8f9fa, #ffffff);
            border-bottom: 1px solid rgba(255, 215, 0, 0.2);
        }
        
        .exam-body {
            padding: 1.5rem;
        }
        
        .subject-title {
            font-weight: 600;
            color: var(--dark-blue);
            margin-bottom: 0.5rem;
        }
        
        .exam-meta {
            display: flex;
            align-items: center;
            gap: 1rem;
            flex-wrap: wrap;
        }
        
        .meta-item {
            display: flex;
            align-items: center;
            color: #666;
            font-size: 0.9rem;
        }
        
        .meta-item i {
            color: var(--secondary-gold);
            margin-right: 0.5rem;
        }
        
        /* Status Badges */
        .status-badge {
            padding: 0.4rem 1rem;
            border-radius: 50px;
            font-weight: 500;
            font-size: 0.85rem;
        }
        
        .status-future {
            background: linear-gradient(135deg, #ffc107, #e0a800);
            color: var(--dark-blue);
        }
        
        .status-available {
            background: linear-gradient(135deg, #17a2b8, #138496);
            color: white;
        }
        
        .status-past {
            background: linear-gradient(135deg, #dc3545, #c82333);
            color: white;
        }
        
        .status-completed {
            background: linear-gradient(135deg, #28a745, #218838);
            color: white;
        }
        
        /* Score Display */
        .score-box {
            background: linear-gradient(135deg, #f8f9fa, #ffffff);
            border-radius: 10px;
            padding: 1rem;
            margin-top: 1rem;
            border-left: 3px solid var(--secondary-gold);
        }
        
        .score-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0.5rem 0;
            border-bottom: 1px solid rgba(255, 215, 0, 0.1);
        }
        
        .score-item:last-child {
            border-bottom: none;
        }
        
        .score-label {
            font-weight: 500;
            color: var(--dark-blue);
        }
        
        .score-value {
            font-weight: 600;
            color: var(--primary-blue);
        }
        
        .term-score {
            background: linear-gradient(135deg, var(--light-gold), #ffffff);
            padding: 0.5rem 1rem;
            border-radius: 50px;
            font-weight: 600;
        }
        
        .term-score i {
            color: var(--secondary-gold);
            margin-right: 0.5rem;
        }
        
        .missed-message {
            background: linear-gradient(135deg, #f8d7da, #f5c6cb);
            border-radius: 10px;
            padding: 1rem;
            color: #721c24;
            border-left: 3px solid #dc3545;
        }
        
        .timer-display {
            background: linear-gradient(135deg, var(--light-gold), #ffffff);
            padding: 0.5rem 1rem;
            border-radius: 50px;
            font-weight: 600;
            color: var(--dark-blue);
            border: 1px solid var(--secondary-gold);
        }
        
        /* Buttons */
        .btn-start {
            background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue));
            border: none;
            color: white;
            padding: 0.6rem 1.5rem;
            border-radius: 50px;
            font-weight: 500;
            transition: all 0.3s ease;
            text-decoration: none;
        }
        
        .btn-start:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(26, 75, 140, 0.3);
            color: white;
        }
        
        .btn-start[disabled] {
            opacity: 0.5;
            cursor: not-allowed;
            pointer-events: none;
        }
        
        .btn-logout {
            background: linear-gradient(135deg, #dc3545, #c82333);
            border: none;
            color: white;
            padding: 0.6rem 2rem;
            border-radius: 50px;
            font-weight: 500;
            transition: all 0.3s ease;
            text-decoration: none;
        }
        
        .btn-logout:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(220, 53, 69, 0.3);
            color: white;
        }
        
        /* Alert Styles */
        .alert {
            border-radius: 10px;
            border: none;
            padding: 1rem 1.5rem;
            margin-bottom: 1.5rem;
            border-left: 4px solid transparent;
        }
        
        .alert-info {
            background: linear-gradient(135deg, #d1ecf1, #bee5eb);
            color: #0c5460;
            border-left-color: #17a2b8;
        }
        
        .alert i {
            margin-right: 0.5rem;
        }
        
        /* Debug Panel */
        .debug-panel {
            background: #2d2d2d;
            color: #f0f0f0;
            padding: 1rem;
            border-radius: 10px;
            margin-bottom: 2rem;
            font-family: monospace;
            font-size: 0.9rem;
        }
        
        .debug-panel h5 {
            color: var(--secondary-gold);
        }
        
        .debug-panel pre {
            color: #a5d6ff;
            margin: 0;
        }
        
        /* Empty State */
        .empty-state {
            text-align: center;
            padding: 3rem;
            background: linear-gradient(135deg, #f8f9fa, #ffffff);
            border-radius: 10px;
            border: 2px dashed rgba(255, 215, 0, 0.3);
            margin-bottom: 1.5rem;
        }
        
        .empty-state i {
            font-size: 3rem;
            color: var(--secondary-gold);
            opacity: 0.5;
            margin-bottom: 1rem;
        }
        
        .empty-state p {
            color: #666;
            margin-bottom: 0;
        }
        
        /* 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) {
            .logo-container {
                flex-direction: column;
                text-align: center;
            }
            
            .school-logo {
                margin-right: 0;
                margin-bottom: 1rem;
            }
            
            .exam-meta {
                flex-direction: column;
                align-items: flex-start;
            }
            
            .contribution-info {
                margin-left: 0;
                margin-top: 0.5rem;
                width: 100%;
            }
            
            .section-header {
                flex-direction: column;
                gap: 0.5rem;
                align-items: flex-start;
            }
        }
    </style>
</head>
<body>
    <!-- Header -->
    <div class="header">
        <div class="container">
            <div class="logo-container">
                <img src="../coll_logo.png" alt="<?= $schoolName ?>" class="school-logo">
                <div class="school-title">
                    <h1><?= $schoolName ?></h1>
                    <small>CBT Examination Portal</small>
                </div>
            </div>
        </div>
    </div>

    <div class="container">
        
        <?php if ($error): ?>
            <div class="alert alert-danger">
                <i class="fas fa-exclamation-circle"></i>
                <?= htmlspecialchars($error) ?>
            </div>
        <?php endif; ?>
        
        <!-- Welcome Banner -->
        <div class="welcome-banner">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <h2>Welcome, <?= htmlspecialchars($student['fullname']) ?>! 👋</h2>
                    <p class="text-muted mb-3">
                        You are logged into the <strong><?= htmlspecialchars($exam_info['assessment_type']) ?></strong> examination portal.
                        <span class="contribution-info">
                            <i class="fas fa-percent"></i> Contributes <?= $contribution ?>% to term result
                        </span>
                    </p>
                    <div class="d-flex flex-wrap">
                        <span class="info-badge">
                            <i class="fas fa-id-card"></i> <?= htmlspecialchars($student['registration_no']) ?>
                        </span>
                        <span class="info-badge">
                            <i class="fas fa-users"></i> <?= htmlspecialchars($exam_info['class']) ?>
                        </span>
                        <span class="info-badge">
                            <i class="fas fa-calendar-alt"></i> <?= htmlspecialchars($exam_info['term']) ?>
                        </span>
                        <span class="info-badge">
                            <i class="fas fa-calendar"></i> <?= htmlspecialchars($exam_info['session']) ?>
                        </span>
                        <span class="assessment-badge <?= getAssessmentBadgeClass($exam_info['assessment_type']) ?>">
                            <?= htmlspecialchars($exam_info['assessment_type']) ?>
                        </span>
                    </div>
                </div>
                <div class="col-md-4 text-md-end mt-3 mt-md-0">
                    <i class="fas fa-graduation-cap fa-4x" style="color: var(--secondary-gold); opacity: 0.5;"></i>
                </div>
            </div>
        </div>

        <?php if (empty($availableExams) && empty($futureExams) && empty($pastExams) && empty($completedExams)): ?>
            <div class="alert alert-info">
                <i class="fas fa-info-circle fa-2x mb-3"></i>
                <h4 class="alert-heading">No Exams Found</h4>
                <p>There are currently no <?= htmlspecialchars($exam_info['assessment_type']) ?> exams scheduled for your class in <?= htmlspecialchars($exam_info['term']) ?> <?= htmlspecialchars($exam_info['session']) ?>.</p>
                <hr>
                <p class="mb-0">Please contact your teacher or the school administration if you believe this is an error.</p>
            </div>
        <?php else: ?>
            <!-- Available Exams Section (Today) -->
            <?php if (!empty($availableExams)): ?>
                <div class="mb-5">
                    <div class="section-header">
                        <h4><i class="fas fa-play-circle"></i> Available Exams - Today</h4>
                        <span class="section-count"><?= count($availableExams) ?> exam(s)</span>
                    </div>
                    
                    <?php foreach ($availableExams as $exam): 
                        $termContributionScore = $exam['correct_answers'] ? round(($exam['correct_answers'] / $exam['total_questions']) * $contribution, 2) : 0;
                    ?>
                        <div class="exam-card available-exam">
                            <div class="exam-header">
                                <div class="d-flex justify-content-between align-items-start">
                                    <div>
                                        <h4 class="subject-title"><?= htmlspecialchars($exam['subject']) ?></h4>
                                        <div class="exam-meta">
                                            <span class="meta-item">
                                                <i class="fas fa-calendar-alt"></i>
                                                <?= date('l, F j, Y', strtotime($exam['exam_date'])) ?> 
                                                <?php 
                                                $examDateCheck = new DateTime($exam['exam_date']);
                                                $today = new DateTime();
                                                if ($examDateCheck->format('Y-m-d') === $today->format('Y-m-d')) {
                                                    echo '<span class="badge bg-warning text-dark ms-2">Today</span>';
                                                }
                                                ?>
                                            </span>
                                            <?php if ($exam['duration_minutes']): ?>
                                                <span class="meta-item">
                                                    <i class="fas fa-clock"></i>
                                                    Duration: <?= $exam['duration_minutes'] ?> minutes
                                                </span>
                                            <?php endif; ?>
                                            <span class="meta-item">
                                                <i class="fas fa-tag"></i>
                                                <?= htmlspecialchars($exam['assessment_type']) ?>
                                            </span>
                                            <span class="meta-item">
                                                <i class="fas fa-percent"></i>
                                                Contributes <?= $contribution ?>%
                                            </span>
                                        </div>
                                    </div>
                                    <span class="status-badge status-available">Available Today</span>
                                </div>
                            </div>
                            
                            <div class="exam-body">
                                <div class="alert alert-info py-3">
                                    <div class="d-flex align-items-center justify-content-between flex-wrap">
                                        <div class="d-flex align-items-center mb-2 mb-sm-0">
                                            <i class="fas fa-play-circle fa-2x me-3" style="color: var(--secondary-gold);"></i>
                                            <div>
                                                <h6 class="mb-1">This <?= htmlspecialchars($exam['assessment_type']) ?> exam is available now!</h6>
                                                <p class="mb-0">Your score will contribute <strong><?= $contribution ?>%</strong> to your term result.</p>
                                            </div>
                                        </div>
                                        <div class="text-end">
                                            <?php if ($exam['duration_minutes']): ?>
                                                <span class="timer-display me-3">
                                                    <i class="fas fa-hourglass-half me-1"></i>
                                                    <?= $exam['duration_minutes'] ?> mins
                                                </span>
                                            <?php endif; ?>
                                            <a href="cbt_exam_now.php?exam_subject_id=<?= $exam['exam_subject_id'] ?>" class="btn-start">
                                                <i class="fas fa-play me-2"></i>Start Exam
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>

            <!-- Future Exams Section -->
            <?php if (!empty($futureExams)): ?>
                <div class="mb-5">
                    <div class="section-header">
                        <h4><i class="fas fa-calendar-plus"></i> Upcoming Exams</h4>
                        <span class="section-count"><?= count($futureExams) ?> exam(s)</span>
                    </div>
                    
                    <?php foreach ($futureExams as $exam): ?>
                        <div class="exam-card future-exam">
                            <div class="exam-header">
                                <div class="d-flex justify-content-between align-items-start">
                                    <div>
                                        <h4 class="subject-title"><?= htmlspecialchars($exam['subject']) ?></h4>
                                        <div class="exam-meta">
                                            <span class="meta-item">
                                                <i class="fas fa-calendar-alt"></i>
                                                <?= date('l, F j, Y', strtotime($exam['exam_date'])) ?>
                                            </span>
                                            <?php if ($exam['duration_minutes']): ?>
                                                <span class="meta-item">
                                                    <i class="fas fa-clock"></i>
                                                    Duration: <?= $exam['duration_minutes'] ?> minutes
                                                </span>
                                            <?php endif; ?>
                                            <span class="meta-item">
                                                <i class="fas fa-tag"></i>
                                                <?= htmlspecialchars($exam['assessment_type']) ?>
                                            </span>
                                            <span class="meta-item">
                                                <i class="fas fa-percent"></i>
                                                Contributes <?= $contribution ?>%
                                            </span>
                                        </div>
                                    </div>
                                    <span class="status-badge status-future">Scheduled</span>
                                </div>
                            </div>
                            
                            <div class="exam-body">
                                <div class="alert alert-warning py-3">
                                    <div class="d-flex align-items-center">
                                        <i class="fas fa-clock fa-2x me-3" style="color: var(--secondary-gold);"></i>
                                        <div>
                                            <h6 class="mb-1">This exam is scheduled for the future</h6>
                                            <p class="mb-0">It will become available on <?= date('l, F j, Y', strtotime($exam['exam_date'])) ?>.</p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>

            <!-- Past/Missed Exams Section -->
            <?php if (!empty($pastExams)): ?>
                <div class="mb-5">
                    <div class="section-header">
                        <h4><i class="fas fa-calendar-times"></i> Missed Exams</h4>
                        <span class="section-count"><?= count($pastExams) ?> exam(s)</span>
                    </div>
                    
                    <?php foreach ($pastExams as $exam): ?>
                        <div class="exam-card past-exam">
                            <div class="exam-header">
                                <div class="d-flex justify-content-between align-items-start">
                                    <div>
                                        <h4 class="subject-title"><?= htmlspecialchars($exam['subject']) ?></h4>
                                        <div class="exam-meta">
                                            <span class="meta-item">
                                                <i class="fas fa-calendar-alt"></i>
                                                <?= date('l, F j, Y', strtotime($exam['exam_date'])) ?>
                                            </span>
                                            <?php if ($exam['duration_minutes']): ?>
                                                <span class="meta-item">
                                                    <i class="fas fa-clock"></i>
                                                    Duration: <?= $exam['duration_minutes'] ?> minutes
                                                </span>
                                            <?php endif; ?>
                                            <span class="meta-item">
                                                <i class="fas fa-tag"></i>
                                                <?= htmlspecialchars($exam['assessment_type']) ?>
                                            </span>
                                            <span class="meta-item">
                                                <i class="fas fa-percent"></i>
                                                Contributes <?= $contribution ?>%
                                            </span>
                                        </div>
                                    </div>
                                    <span class="status-badge status-past">Missed</span>
                                </div>
                            </div>
                            
                            <div class="exam-body">
                                <div class="missed-message">
                                    <div class="d-flex align-items-center">
                                        <i class="fas fa-exclamation-circle fa-2x me-3"></i>
                                        <div>
                                            <h6 class="mb-1">You missed this exam</h6>
                                            <p class="mb-0">This exam was scheduled for <?= date('l, F j, Y', strtotime($exam['exam_date'])) ?>. Please contact your teacher if you need to reschedule.</p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>

            <!-- Completed Exams Section -->
            <?php if (!empty($completedExams)): ?>
                <div class="mb-5">
                    <div class="section-header">
                        <h4><i class="fas fa-check-circle"></i> Completed Exams</h4>
                        <span class="section-count"><?= count($completedExams) ?> exam(s)</span>
                    </div>
                    
                    <?php foreach ($completedExams as $exam): 
                        $termContributionScore = round(($exam['correct_answers'] / $exam['total_questions']) * $contribution, 2);
                    ?>
                        <div class="exam-card completed-exam">
                            <div class="exam-header">
                                <div class="d-flex justify-content-between align-items-start">
                                    <div>
                                        <h4 class="subject-title"><?= htmlspecialchars($exam['subject']) ?></h4>
                                        <div class="exam-meta">
                                            <span class="meta-item">
                                                <i class="fas fa-calendar-alt"></i>
                                                <?= date('l, F j, Y', strtotime($exam['exam_date'])) ?>
                                            </span>
                                            <span class="meta-item">
                                                <i class="fas fa-tag"></i>
                                                <?= htmlspecialchars($exam['assessment_type']) ?>
                                            </span>
                                            <span class="meta-item">
                                                <i class="fas fa-percent"></i>
                                                Contributes <?= $contribution ?>%
                                            </span>
                                        </div>
                                    </div>
                                    <span class="status-badge status-completed">Completed</span>
                                </div>
                            </div>
                            
                            <div class="exam-body">
                                <div class="score-box">
                                    <h6 class="mb-3"><i class="fas fa-chart-bar me-2" style="color: var(--secondary-gold);"></i>Your Performance</h6>
                                    <div class="score-item">
                                        <span class="score-label"><i class="fas fa-calendar-check me-2"></i>Completed on:</span>
                                        <span class="score-value"><?= date('F j, Y \a\t g:i A', strtotime($exam['date_taken'])) ?></span>
                                    </div>
                                    <div class="score-item">
                                        <span class="score-label"><i class="fas fa-percentage me-2"></i><?= $exam['assessment_type'] ?> Score:</span>
                                        <span class="score-value"><?= $exam['cbt_score'] ?>%</span>
                                    </div>
                                    <div class="score-item">
                                        <span class="score-label"><i class="fas fa-check-circle me-2"></i>Correct Answers:</span>
                                        <span class="score-value"><?= $exam['correct_answers'] ?>/<?= $exam['total_questions'] ?></span>
                                    </div>
                                    <div class="score-item" style="border-bottom: none;">
                                        <span class="score-label"><i class="fas fa-star me-2" style="color: var(--secondary-gold);"></i>Term Contribution (<?= $contribution ?>%):</span>
                                        <span class="term-score"><?= $termContributionScore ?>/<?= $contribution ?></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        <?php endif; ?>
        
        <div class="text-center mt-4">
            <a href="logout.php" class="btn-logout">
                <i class="fas fa-sign-out-alt me-2"></i>Logout
            </a>
        </div>
    </div>

    <!-- Footer -->
    <footer class="footer mt-5">
        <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><?= $schoolName ?></strong> - Computer Based Test 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></p>
                    <small>&copy; <?= date('Y') ?> All Rights Reserved</small>
                </div>
            </div>
        </div>
    </footer>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>