<?php
session_start();
require_once 'includes/database.php';
$conn = new Database();

if(!isset($_SESSION['username'])) {
    echo '<option value="">Unauthorized</option>';
    exit();
}

if(!isset($_POST['class_id'])) {
    echo '<option value="">Invalid request</option>';
    exit();
}

$classId = $_POST['class_id'];

// Get subjects for this class
$sql = "SELECT id, subject_name FROM subjects 
        WHERE class_id = :class_id 
        ORDER BY subject_name";
$conn->query($sql);
$conn->bind(':class_id', $classId);
$subjects = $conn->fetchMultiple();

if($conn->rowCount() > 0) {
    echo '<option value="">Select Subject</option>';
    foreach($subjects as $subject) {
        echo '<option value="'.$subject->id.'">'.$subject->subject_name.'</option>';
    }
} else {
    echo '<option value="">No subjects found for this class</option>';
}