<?php

chdir('../');

	require_once("public/connection.php");
	require_once("public/functions.php");

	if (isset($_POST['class'])) {
	    $class = trim($_POST['class']); 

	    // Step 1: Get the level of the selected class from `classes` table
	    $stmt1 = $connection->prepare("SELECT level FROM classes WHERE classes = ?");
	    $stmt1->bind_param("s", $class);
	    $stmt1->execute();
	    $result1 = $stmt1->get_result();

	    if ($result1->num_rows === 0) {
	        echo "<option>No level found for class</option>";
	        exit();
	    }

	    $row = $result1->fetch_assoc();
	    $level = $row['level'];
	    $stmt1->close();

	    // Step 2: Use the level to get subjects from `school_subject_levels`
	    $stmt2 = $connection->prepare("SELECT subject FROM school_subject_levels WHERE level = ? ORDER BY subject ASC");
	    $stmt2->bind_param("s", $level);
	    $stmt2->execute();
	    $result2 = $stmt2->get_result();

	    if ($result2->num_rows > 0) {
	        echo "<option selected>Select a Subject</option>";
	        while ($sub = $result2->fetch_assoc()) {
	            $subject = htmlspecialchars($sub['subject']);
	            echo "<option>{$subject}</option>";
	        }
	    } else {
	        echo "<option>No subjects available for this class level</option>";
	    }

	    $stmt2->close();
	}
?>

