<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
include "../include/config.php";
$userlogin = $_SESSION['studentid'];
$intake = $_SESSION['intake'];

// Helper function to determine grade class
function getGradeClass($score) {
    if (!is_numeric($score)) return 'average';
    if ($score >= 80) return 'excellent';
    if ($score >= 70) return 'good';
    if ($score >= 60) return 'average';
    if ($score >= 50) return 'average';
    return 'poor';
}

// Helper function to extract course code
function getCourseCode($courseName) {
    return substr($courseName, 0, min(7, strlen($courseName)));
}

if (isset($_SESSION['studentid'])) {
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Results</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #049836;
            --secondary-color: #2c3e50;
            --accent-color: #ff8a65;
            --light-color: #f8f9fa;
            --dark-color: #343a40;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #e4efe9 100%);
            color: #333;
            line-height: 1.6;
            min-height: 100vh;
        }
        
        .content {
            padding: 20px;
        }
        
        .results-container {
            background: white;
            border-radius: 16px;
             box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            overflow: hidden;
            margin-bottom: 30px;
        }
        
        .results-header {
            background: var(--primary-color)
            color: white;
            padding: 25px 30px;
            border-bottom: none;
        }
        
        .results-header h2 {
            margin: 0;
            font-weight: 600;
            font-size: 1.5rem;
        }
        
        .results-header p {
            margin: 5px 0 0 0;
            opacity: 0.9;
            font-size: 0.95rem;
        }
        
        .term-section {
            margin-bottom: 0;
            border-bottom: 1px solid #e9ecef;
        }
        
        .term-section:last-child {
            border-bottom: none;
        }
        
        .term-header {
            background: linear-gradient(135deg, #6c757d, #5a6268);
            color: white;
            padding: 20px 25px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
        }
        
        .term-title {
            display: flex;
            align-items: center;
            gap: 12px;
            font-weight: 600;
            font-size: 1.1rem;
        }
        
        .term-badge {
            background: rgba(255,255,255,0.2);
            padding: 6px 12px;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 500;
        }
        
        .results-table {
            width: 100%;
            border-collapse: collapse;
            margin: 0;
        }
        
        .results-table th {
            background: linear-gradient(135deg, #f8f9fa, #e9ecef);
            color: var(--secondary-color);
            font-weight: 600;
            padding: 18px 20px;
            border-bottom: 2px solid #dee2e6;
            font-size: 0.9rem;
            text-align: left;
        }
        
        .results-table td {
            padding: 18px 20px;
            border-bottom: 1px solid #f1f3f4;
            vertical-align: middle;
            transition: background-color 0.2s ease;
        }
        
        .results-table tr:last-child td {
            border-bottom: none;
        }
        
        .results-table tr:hover td {
            background-color: #f8fafc;
        }
        
        .course-code {
            font-weight: 700;
            color: var(--secondary-color);
            font-size: 0.95rem;
        }
        
        .course-name {
            color: #555;
        }
        
        /* Grade Colors */
        .grade-excellent { 
            color: #28a745; 
            font-weight: 600;
        }
        
        .grade-good { 
            color: #17a2b8; 
            font-weight: 600;
        }
        
        .grade-average { 
            color: #ffc107; 
            font-weight: 600;
        }
        
        .grade-poor { 
            color: #dc3545; 
            font-weight: 600;
        }
        
        .score-cell {
            text-align: center;
            font-weight: 500;
        }
        
        .total-score {
            text-align: center;
            font-weight: 700;
            font-size: 1rem;
        }
        
        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #6c757d;
        }
        
        .empty-state i {
            font-size: 4rem;
            margin-bottom: 20px;
            opacity: 0.5;
        }
        
        .empty-state h4 {
            margin-bottom: 10px;
            color: #495057;
        }
        
        .empty-state p {
            font-size: 1rem;
            max-width: 400px;
            margin: 0 auto;
        }
        
        /* Responsive Design */
        @media (max-width: 768px) {
            .content {
                padding: 15px;
            }
            
            .results-header {
                padding: 20px;
            }
            
            .results-header h2 {
                font-size: 1.3rem;
            }
            
            .term-header {
                padding: 15px 20px;
            }
            
            .term-title {
                font-size: 1rem;
            }
            
            .results-table th,
            .results-table td {
                padding: 12px 15px;
                font-size: 0.85rem;
            }
            
            .course-code {
                font-size: 0.9rem;
            }
        }
        
        @media (max-width: 576px) {
            .content {
                padding: 10px;
            }
            
            .results-header {
                padding: 15px 20px;
                text-align: center;
            }
            
            .term-header {
                flex-direction: column;
                align-items: flex-start;
                gap: 10px;
            }
            
            .term-title {
                font-size: 0.95rem;
            }
            
            .results-table {
                font-size: 0.8rem;
            }
            
            .results-table th,
            .results-table td {
                padding: 10px 12px;
            }
            
            .grade-excellent,
            .grade-good,
            .grade-average,
            .grade-poor {
                font-size: 0.85rem;
            }
        }
        
        /* Animation */
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .fade-in {
            animation: fadeIn 0.5s ease-out;
        }
    </style>
</head>
<body>
    <?php include "../include/nav.php"; ?>

    <div class="content">
        <div class="container-fluid">
            <div class="results-container fade-in">
                <div class="results-header">
                    <h2><i class="fas fa-chart-line me-2"></i>Continuous Assessment Results</h2>
                    <p>Grouped by Academic Year and Semester</p>
                </div>
                
                <div class="results-body">
                    <?php
                    $query = "SELECT id, studentid, course, term, year, intake, ca, ca1, ca2, exam
                              FROM results 
                              WHERE studentid = '$userlogin' 
                              ORDER BY year DESC, term DESC, course ASC";
                    
                    $result = mysqli_query($conn, $query);
                    
                    if (mysqli_num_rows($result) > 0) {
                        $grouped = [];
                        
                        // Group results by year and term
                        while ($row = mysqli_fetch_assoc($result)) {
                            $year = trim($row['year']);
                            $term = trim($row['term']);
                            $key = $year . '_' . $term;
                            $grouped[$key][] = $row;
                        }
                        
                        // Sort groups by year and term in descending order
                        krsort($grouped);
                        
                        // Display grouped data
                        foreach ($grouped as $group => $rows) {
                            list($year, $term) = explode('_', $group);
                            $term_course_count = count($rows);
                            
                            echo '<div class="term-section">';
                            
                            // Term header
                            echo '<div class="term-header">';
                            echo '<div class="term-title">';
                            echo '<i class="fas fa-calendar-alt"></i>';
                            echo 'Academic Year ' . $year . ' - Semester ' . $term;
                            echo '</div>';
                            echo '<div class="term-badge">' . $term_course_count . ' courses</div>';
                            echo '</div>';
                            
                            // Results table
                            echo '<table class="results-table">';
                            echo '<thead>';
                            echo '<tr>';
                            echo '<th>Course Name</th>';
                            echo '<th class="text-center">CA1</th>';
                            echo '<th class="text-center">CA2</th>';
                            echo '<th class="text-center">CA3</th>';
                            echo '<th class="text-center">Total CA</th>';
                            echo '</tr>';
                            echo '</thead>';
                            echo '<tbody>';
                            
                            // Course rows
                            foreach ($rows as $r) {
                                $ca = is_numeric($r['ca']) ? $r['ca'] : 0;
                                $ca1 = is_numeric($r['ca1']) ? $r['ca1'] : 0;
                                $ca2 = is_numeric($r['ca2']) ? $r['ca2'] : 0;
                                
                                $total_ca_score = $ca + $ca1 + $ca2;
                                $grade_class = getGradeClass($total_ca_score);
                                
                                echo '<tr>';
                               
                                echo '<td><span class="course-name">' . strtoupper($r['course']) . '</span></td>';
                                echo '<td class="score-cell grade-' . getGradeClass($ca) . '">' . ($ca > 0 ? $ca : '-') . '</td>';
                                echo '<td class="score-cell grade-' . getGradeClass($ca1) . '">' . ($ca1 > 0 ? $ca1 : '-') . '</td>';
                                echo '<td class="score-cell grade-' . getGradeClass($ca2) . '">' . ($ca2 > 0 ? $ca2 : '-') . '</td>';
                                echo '<td class="total-score grade-' . $grade_class . '">' . $total_ca_score . '</td>';
                                echo '</tr>';
                            }
                            
                            echo '</tbody>';
                            echo '</table>';
                            echo '</div>';
                        }
                    } else {
                        echo '<div class="empty-state">';
                        echo '<i class="fas fa-inbox"></i>';
                        echo '<h4>No assessment results available</h4>';
                        echo '<p>Your continuous assessment marks will appear here once they are published.</p>';
                        echo '</div>';
                    }
                    ?>
                </div>
            </div>
        </div>
        <br><br><br>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // Add smooth hover effects
            const rows = document.querySelectorAll('.results-table tbody tr');
            rows.forEach(row => {
                row.addEventListener('mouseenter', function() {
                    this.style.transform = 'translateX(5px)';
                    this.style.transition = 'all 0.2s ease';
                });
                
                row.addEventListener('mouseleave', function() {
                    this.style.transform = 'translateX(0)';
                });
            });
        });
    </script>
</body>
</html>
<?php
} else {
    header("location:../index.php");
}