<?php
session_start();
$userlogin = $_SESSION['email'];
$role=$_SESSION['role'];
include "../include/config.php";

if (isset($_POST['program']) && isset($_POST['semester']) && isset($_POST['staff_id'])) {
    $program = mysqli_real_escape_string($conn, $_POST['program']);
    $semester = mysqli_real_escape_string($conn, $_POST['semester']);
    $staff_id = mysqli_real_escape_string($conn, $_POST['staff_id']);
    
      if($role=="Admin" || $role=="Superadmin"){
    $query = "SELECT DISTINCT course_code, course_name 
              FROM timetable 
              WHERE program = '$program' 
              
              ORDER BY course_name";
      }else{
          $query = "SELECT DISTINCT course_code, course_name 
              FROM timetable 
              WHERE program = '$program' 
              AND staff_id = '$staff_id'
              ORDER BY course_name"; 
      }
    $result = mysqli_query($conn, $query);
    
    if (mysqli_num_rows($result) > 0) {
        echo '<option value="">Select Course</option>';
        while ($row = mysqli_fetch_assoc($result)) {
            echo '<option value="' . htmlspecialchars($row['course_name']) . '">' 
                 . htmlspecialchars($row['course_code'] . ' - ' . $row['course_name']) . '</option>';
        }
    } else {
        echo '<option value="">No courses found for this program</option>';
    }
} else {
    echo '<option value="">Invalid request</option>';
}
?>