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

if (!isset($_SESSION['email'])) {
    header("location:../index.php");
    exit;
}

// Get all distinct courses from results table on page load
$courses = [];
$courseQuery = "SELECT DISTINCT course FROM results ORDER BY course ASC";
$courseResult = mysqli_query($conn, $courseQuery);
if ($courseResult && mysqli_num_rows($courseResult) > 0) {
    while ($row = mysqli_fetch_assoc($courseResult)) {
        $courses[] = $row['course'];
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.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 rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        :root {
           
            --primary-light: #34a37e;
            --primary-dark: #1c6b4d;
            --secondary: #238665;
            --accent: #ff6b35;
            --light: #f8f9fa;
            --dark: #343a40;
            --gray: #6c757d;
            --success: #28a745;
            --warning: #ffc107;
            --danger: #dc3545;
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Poppins', sans-serif;
            background-color: #f5f7fa;
            color: #333;
            line-height: 1.6;
        }
        
        .content {
            margin-left: 250px;
            padding: 20px;
            transition: all 0.3s ease;
        }
        
        @media (max-width: 992px) {
            .content {
                margin-left: 0;
            }
        }
        
        /* Dashboard Header */
        .dashboard-header {
            background: var(--secondary) !important;
            color: white;
            padding: 25px;
            border-radius: 15px;
            margin-bottom: 25px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .dashboard-header h1 {
            font-weight: 700;
            margin-bottom: 5px;
            font-size: 28px;
        }
        
        .dashboard-header p {
            opacity: 0.9;
            margin-bottom: 0;
            font-size: 16px;
        }
        
        /* Form Styles */
        .filter-form {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
            margin-bottom: 30px;
        }
        
        .form-label {
            font-weight: 500;
            color: var(--dark);
            margin-bottom: 8px;
        }
        
        .form-control, .form-select {
            border-radius: 8px;
            padding: 12px 15px;
            border: 1px solid #e1e5eb;
            transition: all 0.3s;
        }
        
        .form-control:focus, .form-select:focus {
            border-color: var(--primary);
            box-shadow: 0 0 0 0.2rem rgba(35, 134, 101, 0.15);
        }
        
        .btn {
            background:var(--primary) !important;
            color: white !important;
            border: none !important;
            padding: 12px 30px !important;
            border-radius: 8px !important;
            font-weight: 500 !important;
            transition: all 0.3s ease !important;
        }
        
        .btn-submit:hover {
            background: #038030;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(4, 152, 54, 0.3);
        }
        
        /* Alert Styles */
        .alert {
            border-radius: 10px;
            padding: 15px 20px;
            margin-bottom: 20px;
            border: none;
            font-weight: 500;
        }
        
        .alert-success {
            background-color: #d4edda;
            color: #155724;
        }
        
        .alert-danger {
            background-color: #f8d7da;
            color: #721c24;
        }
        
        /* Info Box */
        .info-box {
            background: #f8f9fa;
            border-left: 4px solid var(--primary);
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        
        .stats-box {
            background: white;
            border-radius: 10px;
            padding: 15px;
            margin-bottom: 20px;
            box-shadow: 0 3px 10px rgba(0,0,0,0.05);
        }
        
        .stats-item {
            display: flex;
            justify-content: space-between;
            padding: 8px 0;
            border-bottom: 1px solid #f0f0f0;
        }
        
        .stats-item:last-child {
            border-bottom: none;
        }
        
        .stats-label {
            color: var(--gray);
        }
        
        .stats-value {
            font-weight: 600;
            color: var(--primary);
        }
    </style>
</head>
<body>

<?php include "../include/nav.php"; ?>

<div class="content" style="padding:16px">
    
    <div class="dashboard-header">
        <h1><i class="fas fa-chalkboard-teacher me-2"></i>Enter Results</h1>
        <p>Manage CA and Exam records for your assigned courses</p>
    </div>
    
    <!-- Alerts for success/error messages -->
    <?php if (isset($_GET['success'])): ?>
    <div class="alert alert-success">
        <i class="fas fa-check-circle me-2"></i> <?php echo htmlspecialchars($_GET['success']); ?>
    </div>
    <?php endif; ?>
    
    <?php if (isset($_GET['error'])): ?>
    <div class="alert alert-danger">
        <i class="fas fa-exclamation-circle me-2"></i> <?php echo htmlspecialchars($_GET['error']); ?>
    </div>
    <?php endif; ?>
    
    
    
    <!-- Statistics -->
    
    
    <div class="filter-form">
        <form method="post" action="viewhod.php">
            <h5 class="mb-4" style="color: var(--primary); border-bottom: 2px solid var(--light); padding-bottom: 10px;">
                <i class="fas fa-filter me-2"></i>Filter Students for  Entry
            </h5>
            
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label for="program" class="form-label">Program <span class="text-danger">*</span></label>
                    <select name="program" id="program" class="form-select" required>
                        <option value="">Select Program</option>
                        <?php
                        // Get distinct programs from programs table
                        $programQuery = "SELECT DISTINCT program FROM programs ORDER BY program ASC";
                        $programResult = mysqli_query($conn, $programQuery);
                        
                        if ($programResult && mysqli_num_rows($programResult) > 0) {
                            while ($row = mysqli_fetch_assoc($programResult)) {
                                echo "<option value='" . htmlspecialchars($row['program']) . "'>" . 
                                     htmlspecialchars($row['program']) . "</option>";
                            }
                        } else {
                            // Fallback: get from results table
                            $programQuery2 = "SELECT DISTINCT program FROM results ORDER BY program ASC";
                            $programResult2 = mysqli_query($conn, $programQuery2);
                            while ($row = mysqli_fetch_assoc($programResult2)) {
                                echo "<option value='" . htmlspecialchars($row['program']) . "'>" . 
                                     htmlspecialchars($row['program']) . "</option>";
                            }
                        }
                        ?>
                    </select>
                </div>
                
                <div class="col-md-6 mb-3">
                    <label for="term" class="form-label">Semester <span class="text-danger">*</span></label>
                    <select name="term" id="term" class="form-select" required>
                        <option value="">--Select Semester--</option>
                        <option value="1">Semester 1</option>
                        <option value="2">Semester 2</option>
                    </select>
                </div>
                
              
                
                <div class="col-md-6 mb-3">
                    <label for="year" class="form-label">Year <span class="text-danger">*</span></label>
                    <select name="year" id="year" class="form-select" required>
                        <option value="">Select Year</option>
                        <?php
                        $query = mysqli_query($conn, "SELECT * FROM year ORDER BY id DESC");
                        while ($row = mysqli_fetch_assoc($query)) {
                            echo "<option value='" . htmlspecialchars($row['year']) . "'>" . htmlspecialchars($row['year']) . "</option>";
                        }
                        ?>
                    </select>
                </div>
                
                <div class="col-md-6 mb-4">
                    <label for="intake" class="form-label">Intake <span class="text-danger">*</span></label>
                    <select name="intake" id="intake" class="form-select" required>
                        <option value="">Select Intake</option>
                        <?php
                        // Get distinct intakes from results table
                        $intakeQuery = "SELECT DISTINCT intake FROM results ORDER BY intake DESC";
                        $intakeResult = mysqli_query($conn, $intakeQuery);
                        if ($intakeResult && mysqli_num_rows($intakeResult) > 0) {
                            while ($row = mysqli_fetch_assoc($intakeResult)) {
                                echo "<option value='" . htmlspecialchars($row['intake']) . "'>" . htmlspecialchars($row['intake']) . "</option>";
                            }
                        } else {
                            // Fallback: get from timetable table
                            $intakeQuery2 = "SELECT DISTINCT intake FROM timetable ORDER BY intake DESC";
                            $intakeResult2 = mysqli_query($conn, $intakeQuery2);
                            while ($row = mysqli_fetch_assoc($intakeResult2)) {
                                echo "<option value='" . htmlspecialchars($row['intake']) . "'>" . htmlspecialchars($row['intake']) . "</option>";
                            }
                        }
                        ?>
                    </select>
                </div>
            </div>
            
            <div class="text-end">
                <button type="submit" name="marks" class="btn">
                    <i class="fas fa-search me-2"></i>Filter Students
                </button>
            </div>
        </form>
    </div>
    
    <?php if (empty($courses)): ?>
    <div class="alert alert-warning">
        <i class="fas fa-exclamation-triangle me-2"></i>
        <strong>No courses found!</strong> The results table appears to be empty or doesn't contain any courses. 
        Please add some data to the results table first.
    </div>
    <?php endif; ?>
    
    <br><br><br>
</div>

<!-- Bootstrap JS and Popper.js -->
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.min.js"></script>

</body>
</html>