<?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 full name for display
$admin_fullname = '';
$rows = sql($DBH, "SELECT fullname FROM tbl_admin WHERE status = ?", ["Active"], "rows");
if(!empty($rows)) {
    $admin_fullname = $rows[0]['fullname'];
}

// 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";

// Fetch dynamic options for class, term, and year from the database
$query = "SELECT DISTINCT class FROM tbl_student_results ORDER BY class";
$stmt = $DBH->prepare($query);
$stmt->execute();
$classes = $stmt->fetchAll(PDO::FETCH_COLUMN);

$query = "SELECT DISTINCT term FROM tbl_student_results ORDER BY term";
$stmt = $DBH->prepare($query);
$stmt->execute();
$terms = $stmt->fetchAll(PDO::FETCH_COLUMN);

$query = "SELECT DISTINCT year FROM tbl_student_results ORDER BY year DESC";
$stmt = $DBH->prepare($query);
$stmt->execute();
$years = $stmt->fetchAll(PDO::FETCH_COLUMN);

// Initialize variables to store class, term, and year for display
$page_title = '';
$total_results_count = 0;
$class = $term = $year = '';
$class_results = [];

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Extract search data from the form
    $class = strip_tags($_POST['class']);
    $term = strip_tags($_POST['term']);
    $year = strip_tags($_POST['year']);

    // Retrieve uploaded class results from the database based on the search criteria
    $query = "SELECT DISTINCT class, term, year, subject, csv_no FROM tbl_student_results WHERE class = ? AND term = ? AND year = ? ORDER BY subject";
    $stmt = $DBH->prepare($query);
    $stmt->execute([$class, $term, $year]);
    $class_results = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // Set page title
    $page_title = "Class: $class, Term: $term, Year: $year";

    // Count of total uploaded class results
    $total_results_count_query = "SELECT COUNT(DISTINCT csv_no) FROM tbl_student_results WHERE class = ? AND term = ? AND year = ?";
    $stmt = $DBH->prepare($total_results_count_query);
    $stmt->execute([$class, $term, $year]);
    $total_results_count = $stmt->fetchColumn();
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Uploaded Class Results - 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">

    <!-- 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;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background-color: var(--bg-light);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        /* Navbar Styles */
        .navbar {
            background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue));
            box-shadow: 0 0.15rem 1.75rem 0 rgba(26, 75, 140, 0.2);
            position: fixed;
            top: 0;
            width: 100%;
            z-index: 1030;
            color: white;
            border-bottom: 2px solid var(--secondary-gold);
        }

        .navbar-brand {
            font-weight: 800;
            font-size: 1.2rem;
            color: white;
            display: flex;
            align-items: center;
        }

        .navbar-brand img {
            border-radius: 4px;
            margin-right: 10px;
        }

        /* Sidebar Styles */
        .sidebar {
            background: linear-gradient(180deg, var(--dark-blue) 0%, var(--primary-blue) 100%);
            position: fixed;
            top: 56px;
            left: 0;
            width: 250px;
            height: calc(100vh - 56px);
            transition: all 0.3s;
            z-index: 1000;
            overflow-y: auto;
            border-right: 3px solid var(--secondary-gold);
        }

        .sidebar .nav-link {
            color: rgba(255, 255, 255, 0.9);
            font-weight: 500;
            padding: 0.75rem 1rem;
            margin-bottom: 0.2rem;
            transition: all 0.2s;
            border-left: 3px solid transparent;
        }

        .sidebar .nav-link:hover {
            color: white;
            background-color: rgba(255, 215, 0, 0.15);
            border-left-color: var(--secondary-gold);
        }

        .sidebar .nav-link.active {
            color: var(--dark-blue);
            background-color: var(--secondary-gold);
            border-left-color: var(--dark-blue);
            font-weight: 600;
        }

        .sidebar .nav-link i {
            margin-right: 0.5rem;
            width: 20px;
            text-align: center;
        }

        .sidebar-logo {
            text-align: center;
            margin-bottom: 1rem;
            padding: 1rem;
        }

        .logo-text {
            font-size: 1.5rem;
            font-weight: 900;
            color: white;
            letter-spacing: 2px;
            line-height: 1.2;
        }

        .logo-text span {
            color: var(--secondary-gold);
        }

        .logo-tagline {
            font-size: 0.65rem;
            color: var(--secondary-gold);
            letter-spacing: 1px;
        }

        /* Main Content Styles */
        .main-content {
            margin-left: 250px;
            width: calc(100% - 250px);
            transition: all 0.3s;
            min-height: calc(100vh - 56px);
            padding-bottom: 60px;
            padding-top: 56px;
        }

        @media (max-width: 991.98px) {
            .sidebar {
                left: -250px;
            }
            .main-content {
                margin-left: 0;
                width: 100%;
            }
        }

        /* Welcome banner */
        .welcome-banner {
            background: linear-gradient(135deg, var(--primary-blue), var(--light-blue));
            color: white;
            border-radius: 0.5rem;
            padding: 0.75rem 1rem;
            margin: 1rem;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            border-left: 5px solid var(--secondary-gold);
        }

        /* Form Container */
        .form-container {
            background-color: white;
            border-radius: 0.5rem;
            padding: 1.5rem;
            margin: 1rem;
            box-shadow: 0 0.15rem 1.75rem 0 rgba(26, 75, 140, 0.1);
            border: 1px solid rgba(255, 215, 0, 0.2);
        }

        /* Results Container */
        .results-container {
            background-color: white;
            border-radius: 0.5rem;
            padding: 1.5rem;
            margin: 1rem;
            box-shadow: 0 0.15rem 1.75rem 0 rgba(26, 75, 140, 0.1);
            border: 1px solid rgba(255, 215, 0, 0.2);
        }

        /* Admin Info Styles */
        .admin-info {
            display: flex;
            align-items: center;
            background-color: rgba(255, 215, 0, 0.2);
            padding: 8px 12px;
            border-radius: 8px;
            color: white;
        }

        .admin-name {
            margin-right: 5px;
            font-weight: 600;
        }

        .admin-access {
            font-style: italic;
            opacity: 0.8;
            margin-right: 10px;
            font-size: 0.9rem;
        }

        .admin-icon {
            font-size: 1.5rem;
            color: var(--secondary-gold);
        }

        /* Footer styles */
        .footer {
            background: linear-gradient(135deg, var(--dark-blue), var(--primary-blue));
            color: white;
            padding: 12px 0;
            position: fixed;
            bottom: 0;
            width: 100%;
            z-index: 100;
            font-size: 0.8rem;
            border-top: 2px solid var(--secondary-gold);
            margin-left: 250px;
            width: calc(100% - 250px);
        }

        @media (max-width: 991.98px) {
            .footer {
                margin-left: 0;
                width: 100%;
            }
        }

        .copyright-text {
            font-weight: 600;
        }

        .copyright-text i {
            color: var(--secondary-gold);
        }

        .powered-by {
            display: flex;
            align-items: center;
            justify-content: flex-end;
            color: white;
        }

        .powered-by img {
            height: 18px;
            margin-left: 6px;
            filter: brightness(0) invert(1);
        }

        .powered-by-link {
            display: flex;
            align-items: center;
            text-decoration: none;
            color: var(--secondary-gold);
            transition: opacity 0.3s ease;
        }

        .powered-by-link:hover {
            opacity: 0.8;
            color: white;
        }

        /* Support badge */
        .support-badge {
            background: linear-gradient(135deg, var(--secondary-gold), #e6b800);
            color: var(--dark-blue);
            font-weight: 600;
            padding: 0.5rem 1rem;
            border: 1px solid var(--dark-blue);
        }

        /* Breadcrumbs */
        .breadcrumb-custom .breadcrumb {
            background-color: transparent;
            padding: 0;
            margin: 1rem 1rem 0;
        }

        .breadcrumb-custom .breadcrumb-item a {
            color: var(--primary-blue);
            text-decoration: none;
            font-weight: 500;
        }

        .breadcrumb-custom .breadcrumb-item a:hover {
            color: var(--secondary-gold);
        }

        .breadcrumb-custom .breadcrumb-item.active {
            color: var(--dark-blue);
            font-weight: 600;
        }

        .breadcrumb-item + .breadcrumb-item::before {
            color: var(--secondary-gold);
        }

        /* Form Styles */
        .form-label {
            font-weight: 600;
            color: var(--dark-blue);
        }

        .form-label i {
            color: var(--secondary-gold);
        }

        .form-select, .form-control {
            border: 1px solid #ced4da;
            border-radius: 8px;
            padding: 0.5rem;
            transition: all 0.3s;
        }

        .form-select:focus, .form-control:focus {
            border-color: var(--secondary-gold);
            box-shadow: 0 0 0 0.25rem rgba(255, 215, 0, 0.25);
        }

        .btn-primary {
            background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue));
            border-color: var(--secondary-gold);
            color: white;
            padding: 0.5rem 1.5rem;
            font-weight: 600;
        }

        .btn-primary:hover {
            background: linear-gradient(135deg, var(--secondary-gold), #e6b800);
            border-color: var(--dark-blue);
            color: var(--dark-blue);
        }

        .btn-outline-light {
            border-color: var(--secondary-gold);
            color: var(--secondary-gold);
        }

        .btn-outline-light:hover {
            background-color: var(--secondary-gold);
            border-color: var(--dark-blue);
            color: var(--dark-blue);
        }

        /* Table Styles */
        .table-container {
            overflow-x: auto;
            margin-top: 1.5rem;
        }

        .table {
            width: 100%;
            border-collapse: collapse;
            background-color: white;
        }

        .table th {
            background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue));
            color: white;
            font-weight: 600;
            padding: 12px;
            text-align: left;
            border-bottom: 2px solid var(--secondary-gold);
        }

        .table td {
            padding: 10px 12px;
            border-bottom: 1px solid #dee2e6;
            vertical-align: middle;
        }

        .table tbody tr:hover {
            background-color: var(--light-gold);
        }

        .table tbody tr:nth-child(even) {
            background-color: #f9f9f9;
        }

        /* Action Buttons */
        .btn-edit {
            background-color: var(--primary-blue);
            color: white;
            padding: 5px 10px;
            border-radius: 4px;
            text-decoration: none;
            font-size: 0.85rem;
            display: inline-block;
            margin-right: 5px;
            border: 1px solid var(--secondary-gold);
        }

        .btn-edit:hover {
            background-color: var(--dark-blue);
            color: white;
        }

        .btn-delete {
            background-color: #dc3545;
            color: white;
            padding: 5px 10px;
            border-radius: 4px;
            text-decoration: none;
            font-size: 0.85rem;
            display: inline-block;
            border: 1px solid var(--secondary-gold);
        }

        .btn-delete:hover {
            background-color: #c82333;
            color: white;
        }

        /* Count Badge */
        .count-badge {
            background-color: var(--secondary-gold);
            color: var(--dark-blue);
            padding: 5px 10px;
            border-radius: 20px;
            font-weight: 600;
            display: inline-block;
            margin-top: 1rem;
        }

        /* Page Title */
        .page-title {
            color: var(--dark-blue);
            font-weight: 700;
            margin-bottom: 1.5rem;
            padding-bottom: 0.5rem;
            border-bottom: 2px solid var(--secondary-gold);
        }

        /* Empty State */
        .empty-state {
            text-align: center;
            padding: 3rem;
            color: #6c757d;
        }

        .empty-state i {
            color: var(--primary-blue);
            margin-bottom: 1rem;
        }

        /* Responsive */
        @media (max-width: 768px) {
            .form-row {
                flex-direction: column;
            }
            
            .btn-primary {
                width: 100%;
                margin-top: 1rem;
            }
            
            .table th, .table td {
                font-size: 0.85rem;
                padding: 8px;
            }
        }
    </style>
</head>

<body>
    <!-- Top Navigation -->
    <nav class="navbar navbar-expand-lg navbar-dark">
        <div class="container-fluid">
            <button id="sidebarToggle" class="btn btn-link d-lg-none me-2">
                <i class="fas fa-bars" style="color: white;"></i>
            </button>
            <a class="navbar-brand d-none d-lg-block" href="#">
                <img src="coll_logo.png" alt="Henzy Royal International School" height="35" style="border-radius: 4px;">
                HENZY ROYAL INT'L SCHOOL
            </a>
            <div class="ms-auto d-flex align-items-center">
                <div class="admin-info me-3">
                    <span class="admin-name"><?php echo htmlspecialchars($admin_fullname ?: 'Admin'); ?></span>
                    <span class="admin-access">(Administrator)</span>
                    <i class="fas fa-user-circle admin-icon"></i>
                </div>
                <a href="logout.php" class="btn btn-sm btn-outline-light">
                    <i class="fas fa-sign-out-alt"></i> Logout
                </a>
            </div>
        </div>
    </nav>

    <!-- Sidebar -->
    <nav id="sidebar" class="sidebar">
        <div class="position-sticky pt-3">
            <div class="sidebar-logo d-none d-lg-block">
                <div class="logo-text">
                    H.R.<span>I.S</span>
                </div>
                <div class="logo-tagline">
                    THE PRIDE OF EXCELLENCE
                </div>
                <hr style="border-color: var(--secondary-gold); opacity: 0.3;">
                <p class="text-white small px-2">
                    <i class="fas fa-map-marker-alt" style="color: var(--secondary-gold);"></i><br>
                    <?php echo $schoolAddress; ?>
                </p>
            </div>

            <ul class="nav flex-column">
                <li class="nav-item">
                    <a class="nav-link" href="main.php">
                        <i class="fas fa-fw fa-tachometer-alt"></i>
                        Admin Dashboard
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="download_broadsheet.php">
                        <i class="fas fa-fw fa-file-pdf"></i>
                        Result Broadsheet
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link active" href="uploaded_class_results.php">
                        <i class="fas fa-fw fa-list"></i>
                        Uploaded Results
                    </a>
                </li>
            </ul>
        </div>
    </nav>

    <!-- Main Content -->
    <main class="main-content">
        <!-- Welcome Banner -->
        <div class="welcome-banner">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h4 class="mb-1"><i class="fas fa-crown me-2" style="color: var(--secondary-gold);"></i>Uploaded Class Results</h4>
                    <p class="mb-0 small">Henzy Royal International School - View all uploaded results by class, term, and session</p>
                </div>
                <div class="d-none d-sm-inline-block">
                    <span class="badge support-badge p-1">
                        <i class="fas fa-database me-1"></i> Result Management
                    </span>
                </div>
            </div>
        </div>

        <!-- Breadcrumbs -->
        <div class="breadcrumb-custom">
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="main.php">Admin Dashboard</a></li>
                    <li class="breadcrumb-item active">Uploaded Results</li>
                </ol>
            </nav>
        </div>

        <!-- Search Form -->
        <div class="form-container">
            <h5 class="mb-3"><i class="fas fa-search me-2" style="color: var(--secondary-gold);"></i>Search Uploaded Results</h5>
            <form method="POST">
                <div class="row g-3">
                    <div class="col-md-3">
                        <label for="class" class="form-label"><i class="fas fa-building me-1"></i>Class:</label>
                        <select id="class" name="class" class="form-select" required>
                            <option value="">Select Class</option>
                            <?php foreach ($classes as $class_option) : ?>
                                <option value="<?php echo htmlspecialchars($class_option); ?>" <?php echo ($class_option === $class) ? 'selected' : ''; ?>><?php echo htmlspecialchars($class_option); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="col-md-3">
                        <label for="term" class="form-label"><i class="fas fa-calendar-alt me-1"></i>Term:</label>
                        <select id="term" name="term" class="form-select" required>
                            <option value="">Select Term</option>
                            <?php foreach ($terms as $term_option) : ?>
                                <option value="<?php echo htmlspecialchars($term_option); ?>" <?php echo ($term_option === $term) ? 'selected' : ''; ?>><?php echo htmlspecialchars($term_option); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="col-md-3">
                        <label for="year" class="form-label"><i class="fas fa-calendar me-1"></i>Year:</label>
                        <select id="year" name="year" class="form-select" required>
                            <option value="">Select Year</option>
                            <?php foreach ($years as $year_option) : ?>
                                <option value="<?php echo htmlspecialchars($year_option); ?>" <?php echo ($year_option === $year) ? 'selected' : ''; ?>><?php echo htmlspecialchars($year_option); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="col-md-3 d-flex align-items-end">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="fas fa-search me-1"></i> Search
                        </button>
                    </div>
                </div>
            </form>
        </div>

        <!-- Results Section -->
        <?php if (!empty($class_results)): ?>
        <div class="results-container">
            <div class="d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="fas fa-list me-2" style="color: var(--secondary-gold);"></i><?php echo htmlspecialchars($page_title); ?></h5>
                <span class="count-badge">
                    <i class="fas fa-file-upload me-1"></i> Total Uploads: <?php echo $total_results_count; ?>
                </span>
            </div>

            <div class="table-container">
                <table class="table">
                    <thead>
                        <tr>
                            <th>S/N</th>
                            <th>Class</th>
                            <th>Term</th>
                            <th>Year</th>
                            <th>Subject</th>
                            <th>CSV Number</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $serial_number = 1; ?>
                        <?php foreach ($class_results as $record): ?>
                            <tr>
                                <td><?php echo $serial_number++; ?></td>
                                <td><i class="fas fa-building me-1" style="color: var(--secondary-gold);"></i><?php echo htmlspecialchars($record['class']); ?></td>
                                <td><i class="fas fa-calendar-alt me-1" style="color: var(--secondary-gold);"></i><?php echo htmlspecialchars($record['term']); ?></td>
                                <td><i class="fas fa-calendar me-1" style="color: var(--secondary-gold);"></i><?php echo htmlspecialchars($record['year']); ?></td>
                                <td><i class="fas fa-book me-1" style="color: var(--secondary-gold);"></i><?php echo htmlspecialchars($record['subject']); ?></td>
                                <td><code><?php echo htmlspecialchars($record['csv_no']); ?></code></td>
                                <td>
                                    <a href="edit_subject.php?class=<?php echo urlencode($record['class']); ?>&term=<?php echo urlencode($record['term']); ?>&year=<?php echo urlencode($record['year']); ?>&subject=<?php echo urlencode($record['subject']); ?>" target="_blank" class="btn-edit">
                                        <i class="fas fa-edit me-1"></i> Edit
                                    </a>
                                    <a href="delete_class_result_subject.php?csv_no=<?php echo urlencode($record['csv_no']); ?>" class="btn-delete" onclick="return confirm('Are you sure you want to delete all records with CSV Number: <?php echo htmlspecialchars($record['csv_no']); ?>? This action cannot be undone.')">
                                        <i class="fas fa-trash-alt me-1"></i> Delete
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php elseif ($_SERVER["REQUEST_METHOD"] == "POST"): ?>
        <div class="results-container">
            <div class="empty-state">
                <i class="fas fa-file-excel fa-3x mb-3"></i>
                <h5>No Results Found</h5>
                <p class="text-muted">There are no uploaded results for the selected class, term, and year combination.</p>
            </div>
        </div>
        <?php else: ?>
        <div class="results-container">
            <div class="empty-state">
                <i class="fas fa-search fa-3x mb-3"></i>
                <h5>Search for Uploaded Results</h5>
                <p class="text-muted">Use the form above to search for uploaded class results by class, term, and academic year.</p>
            </div>
        </div>
        <?php endif; ?>
    </main>

    <!-- Footer -->
    <footer class="footer">
        <div class="container-fluid">
            <div class="row align-items-center">
                <div class="col-md-6">
                    <span class="copyright-text">
                        <i class="fas fa-copyright me-1"></i> <?php echo date("Y"); ?> Henzy Royal International School
                    </span>
                </div>
                <div class="col-md-6">
                    <div class="powered-by">
                        <a href="https://dataswift.com.ng" target="_blank" rel="noopener noreferrer" class="powered-by-link">
                            <span>Powered by DataSwift Coders</span>
                            <img src="datalogo.png" alt="DataSwift Logo">
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </footer>

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

    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <!-- Custom Scripts -->
    <script>
        // Toggle sidebar on mobile
        document.getElementById('sidebarToggle').addEventListener('click', function() {
            document.getElementById('sidebar').classList.toggle('active');
            document.querySelector('.main-content').classList.toggle('active');
            document.querySelector('.footer').classList.toggle('active');
        });

        // Close sidebar when clicking outside on mobile
        document.addEventListener('click', function(event) {
            const sidebar = document.getElementById('sidebar');
            const sidebarToggle = document.getElementById('sidebarToggle');
            
            if (window.innerWidth < 992 && 
                !sidebar.contains(event.target) && 
                !sidebarToggle.contains(event.target) && 
                sidebar.classList.contains('active')) {
                sidebar.classList.remove('active');
                document.querySelector('.main-content').classList.remove('active');
                document.querySelector('.footer').classList.remove('active');
            }
        });

        // Auto-hide alerts after 5 seconds
        setTimeout(function() {
            $('.alert').fadeOut('slow');
        }, 5000);
    </script>
</body>

</html>