<?php
require_once('source/dbconfig.php');
require_once('source/function.php');
session_start();

// Verify results session
if (!isset($_SESSION['exam_completed'])) {
    header("Location: welcome.php");
    exit();
}

// School information
$schoolName = "Henzy Royal International School";
$schoolAddress = "No 17 Amadi Street, Amakohia, Uratta Owerri Imo State Nigeria";
$schoolMotto = "Excellence, Integrity, Service";

$result = $_SESSION['exam_completed'];
$passed = $result['score'] >= 40; // 40% is passing score (E grade)

// Function to get grade based on score
function getGrade($score) {
    if ($score >= 80) return ['A', 'Excellent', 'success'];
    if ($score >= 70) return ['B', 'Very Good', 'success'];
    if ($score >= 60) return ['C', 'Good', 'info'];
    if ($score >= 50) return ['D', 'Credit', 'info'];
    if ($score >= 40) return ['E', 'Pass', 'warning'];
    return ['F', 'Fail', 'danger'];
}

$gradeInfo = getGrade($result['score']);
$grade = $gradeInfo[0];
$remark = $gradeInfo[1];
$gradeColor = $gradeInfo[2];

// Calculate percentage for display
$percentage = round(($result['correct'] / $result['total']) * 100, 1);

// 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';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Exam Results - <?= $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">
    
    <!-- Bootstrap Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.0/font/bootstrap-icons.css">
    
    <!-- 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 {
            background: linear-gradient(135deg, #f5f7fa 0%, #e9ecef 100%);
            font-family: 'Poppins', sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        /* Header */
        .header {
            background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue));
            color: white;
            padding: 1rem 0;
            border-bottom: 3px solid var(--secondary-gold);
            margin-bottom: 2rem;
            width: 100%;
        }
        
        .logo-container {
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .school-logo {
            width: 50px;
            height: 50px;
            object-fit: contain;
            background: white;
            padding: 5px;
            border-radius: 50%;
            border: 2px solid var(--secondary-gold);
            margin-right: 15px;
        }
        
        .school-title h1 {
            font-size: 1.3rem;
            font-weight: 700;
            margin-bottom: 0;
        }
        
        .school-title small {
            color: var(--secondary-gold);
            font-size: 0.8rem;
        }
        
        /* Assessment Type Badge */
        .assessment-badge {
            display: inline-block;
            padding: 0.4rem 1rem;
            border-radius: 50px;
            font-size: 0.8rem;
            font-weight: 600;
        }
        
        .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; }
        
        /* Result Card */
        .result-card {
            max-width: 700px;
            margin: 2rem auto;
            border: none;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(26, 75, 140, 0.15);
            overflow: hidden;
            border: 1px solid rgba(255, 215, 0, 0.2);
        }
        
        .result-header {
            background: linear-gradient(135deg, 
                <?= $passed ? '#28a745' : '#dc3545' ?>, 
                <?= $passed ? '#218838' : '#c82333' ?>);
            color: white;
            padding: 2rem;
            text-align: center;
            position: relative;
            border-bottom: 3px solid var(--secondary-gold);
        }
        
        .result-header::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 5px;
            background: linear-gradient(90deg, var(--secondary-gold), rgba(255,255,255,0.5));
        }
        
        .result-header h2 {
            font-weight: 700;
            margin-bottom: 1rem;
        }
        
        .result-header h2 i {
            color: var(--secondary-gold);
            margin-right: 0.5rem;
        }
        
        .result-header p {
            font-size: 1.1rem;
            opacity: 0.9;
            margin-bottom: 0;
        }
        
        .result-details {
            padding: 2.5rem;
            background: white;
        }
        
        /* Score Display */
        .score-circle {
            width: 150px;
            height: 150px;
            margin: 0 auto 1.5rem;
            border-radius: 50%;
            background: linear-gradient(135deg, #f8f9fa, #ffffff);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            border: 4px solid var(--secondary-gold);
            box-shadow: 0 5px 15px rgba(26, 75, 140, 0.2);
        }
        
        .score-number {
            font-size: 3rem;
            font-weight: 700;
            line-height: 1;
            color: var(--dark-blue);
        }
        
        .score-label {
            font-size: 0.9rem;
            color: #666;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .grade-badge {
            display: inline-block;
            width: 60px;
            height: 60px;
            line-height: 60px;
            border-radius: 50%;
            font-size: 2rem;
            font-weight: 700;
            margin: 0 auto 1rem;
            box-shadow: 0 4px 10px rgba(0,0,0,0.1);
        }
        
        .grade-A { background: linear-gradient(135deg, #28a745, #218838); color: white; }
        .grade-B { background: linear-gradient(135deg, #17a2b8, #138496); color: white; }
        .grade-C { background: linear-gradient(135deg, #ffc107, #e0a800); color: var(--dark-blue); }
        .grade-D { background: linear-gradient(135deg, #fd7e14, #e56b08); color: white; }
        .grade-E { background: linear-gradient(135deg, #ffc107, #e0a800); color: var(--dark-blue); }
        .grade-F { background: linear-gradient(135deg, #dc3545, #c82333); color: white; }
        
        .status-badge {
            font-size: 1rem;
            padding: 0.5rem 1.5rem;
            border-radius: 50px;
            font-weight: 600;
            display: inline-block;
        }
        
        .status-pass {
            background: linear-gradient(135deg, #28a745, #218838);
            color: white;
        }
        
        .status-fail {
            background: linear-gradient(135deg, #dc3545, #c82333);
            color: white;
        }
        
        .progress {
            height: 10px;
            border-radius: 5px;
            background-color: #e0e0e0;
            margin: 1.5rem 0;
            overflow: hidden;
        }
        
        .progress-bar {
            background: linear-gradient(90deg, var(--secondary-gold), var(--primary-blue));
            border-radius: 5px;
        }
        
        /* Details Grid */
        .details-grid {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 1rem;
            margin: 1.5rem 0;
        }
        
        .detail-item {
            background: linear-gradient(135deg, #f8f9fa, #ffffff);
            border-radius: 10px;
            padding: 1rem;
            border-left: 3px solid var(--secondary-gold);
        }
        
        .detail-label {
            font-size: 0.85rem;
            color: #666;
            margin-bottom: 0.25rem;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        .detail-value {
            font-size: 1.1rem;
            font-weight: 600;
            color: var(--dark-blue);
        }
        
        .detail-value i {
            color: var(--secondary-gold);
            margin-right: 0.5rem;
        }
        
        /* Stats Row */
        .stats-row {
            display: flex;
            justify-content: space-around;
            text-align: center;
            margin: 1.5rem 0;
            padding: 1rem;
            background: linear-gradient(135deg, #f8f9fa, #ffffff);
            border-radius: 10px;
        }
        
        .stat-item {
            flex: 1;
        }
        
        .stat-number {
            font-size: 1.8rem;
            font-weight: 700;
            color: var(--primary-blue);
            line-height: 1.2;
        }
        
        .stat-label {
            font-size: 0.85rem;
            color: #666;
            text-transform: uppercase;
        }
        
        /* Action Buttons */
        .action-btns {
            display: flex;
            gap: 1rem;
            justify-content: center;
            margin-top: 2rem;
            flex-wrap: wrap;
        }
        
        .btn-custom {
            padding: 0.75rem 2rem;
            border-radius: 50px;
            font-weight: 500;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 0.5rem;
        }
        
        .btn-primary-custom {
            background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue));
            color: white;
            border: 1px solid var(--secondary-gold);
        }
        
        .btn-primary-custom:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(26, 75, 140, 0.3);
            color: white;
        }
        
        .btn-outline-custom {
            background: transparent;
            border: 2px solid var(--primary-blue);
            color: var(--primary-blue);
        }
        
        .btn-outline-custom:hover {
            background: var(--primary-blue);
            color: white;
            transform: translateY(-2px);
        }
        
        .btn-warning-custom {
            background: linear-gradient(135deg, #fd7e14, #e56b08);
            color: white;
            border: 1px solid var(--secondary-gold);
        }
        
        .btn-warning-custom:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(253, 126, 20, 0.3);
            color: white;
        }
        
        /* Footer */
        .footer {
            text-align: center;
            margin-top: 2rem;
            color: #666;
            font-size: 0.85rem;
        }
        
        /* Responsive */
        @media (max-width: 576px) {
            .result-details {
                padding: 1.5rem;
            }
            
            .details-grid {
                grid-template-columns: 1fr;
            }
            
            .stats-row {
                flex-direction: column;
                gap: 1rem;
            }
            
            .action-btns {
                flex-direction: column;
            }
            
            .btn-custom {
                width: 100%;
                justify-content: center;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 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><?= $schoolMotto ?></small>
                    </div>
                </div>
            </div>
        </div>

        <!-- Result Card -->
        <div class="result-card">
            <div class="result-header">
                <i class="bi bi-<?= $passed ? 'check-circle-fill' : 'exclamation-circle-fill' ?>" style="font-size: 3rem;"></i>
                <h2>Exam Completed</h2>
                <p><?= htmlspecialchars($result['subject']) ?></p>
                <span class="assessment-badge <?= getAssessmentBadgeClass($result['assessment_type'] ?? 'Term Exam') ?> mt-2">
                    <i class="fas fa-tag me-1"></i> <?= $result['assessment_type'] ?? 'Term Exam' ?>
                </span>
            </div>
            
            <div class="result-details">
                <div class="text-center mb-4">
                    <!-- Grade Circle -->
                    <div class="grade-badge grade-<?= $grade ?> mx-auto">
                        <?= $grade ?>
                    </div>
                    
                    <!-- Score Circle -->
                    <div class="score-circle">
                        <span class="score-number"><?= $result['score'] ?></span>
                        <span class="score-label">Total Score</span>
                    </div>
                    
                    <!-- Status Badge -->
                    <span class="status-badge status-<?= $passed ? 'pass' : 'fail' ?>">
                        <?= $passed ? 'PASSED' : 'NOT PASSED' ?>
                    </span>
                    
                    <!-- Progress Bar -->
                    <div class="progress">
                        <div class="progress-bar" 
                             role="progressbar" 
                             style="width: <?= $percentage ?>%"></div>
                    </div>
                    
                    <!-- Remark -->
                    <h5 class="mt-3" style="color: var(--dark-blue);"><?= $remark ?></h5>
                </div>
                
                <!-- Stats Row -->
                <div class="stats-row">
                    <div class="stat-item">
                        <div class="stat-number"><?= $result['correct'] ?>/<?= $result['total'] ?></div>
                        <div class="stat-label">Correct Answers</div>
                    </div>
                    <div class="stat-item">
                        <div class="stat-number"><?= $percentage ?>%</div>
                        <div class="stat-label">Percentage</div>
                    </div>
                </div>
                
                <!-- Details Grid -->
                <div class="details-grid">
                    <div class="detail-item">
                        <div class="detail-label">Class</div>
                        <div class="detail-value">
                            <i class="fas fa-users"></i> <?= htmlspecialchars($result['class']) ?>
                        </div>
                    </div>
                    
                    <div class="detail-item">
                        <div class="detail-label">Term</div>
                        <div class="detail-value">
                            <i class="fas fa-calendar-alt"></i> <?= htmlspecialchars($result['term']) ?>
                        </div>
                    </div>
                    
                    <div class="detail-item">
                        <div class="detail-label">Session</div>
                        <div class="detail-value">
                            <i class="fas fa-calendar"></i> <?= htmlspecialchars($result['session']) ?>
                        </div>
                    </div>
                    
                    <div class="detail-item">
                        <div class="detail-label">Date Completed</div>
                        <div class="detail-value">
                            <i class="fas fa-clock"></i> <?= date('F j, Y') ?>
                        </div>
                    </div>
                </div>
                
                <!-- Action Buttons -->
                <div class="action-btns">
                    <a href="welcome.php" class="btn-custom btn-primary-custom">
                        <i class="bi bi-house-door"></i> Dashboard
                    </a>
                    
                    <?php if (!$passed): ?>
                        <a href="retake_exam.php?exam_subject_id=<?= $result['exam_subject_id'] ?>" class="btn-custom btn-warning-custom">
                            <i class="bi bi-arrow-repeat"></i> Retake Exam
                        </a>
                    <?php endif; ?>
                    
                    <a href="logout.php" class="btn-custom btn-outline-custom">
                        <i class="bi bi-box-arrow-right"></i> Logout
                    </a>
                </div>
                
                <!-- Footer Note -->
                <div class="footer">
                    <i class="fas fa-crown me-1" style="color: var(--secondary-gold);"></i>
                    Powered by DataSwift Coders
                </div>
            </div>
        </div>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    
    <!-- Confetti animation for passed students -->
    <?php if ($passed): ?>
    <script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1"></script>
    <script>
        // Launch confetti for passed students
        document.addEventListener('DOMContentLoaded', function() {
            confetti({
                particleCount: 100,
                spread: 70,
                origin: { y: 0.6 }
            });
        });
    </script>
    <?php endif; ?>
</body>
</html>
<?php
// Clear the results session after displaying
unset($_SESSION['exam_completed']);
?>