<?php
//filename: ajax/get_students_for_class_year.php 

include('../includes/db.php');

if (isset($_POST['classId']) && isset($_POST['academicYear'])) {
    $classId = (int)$_POST['classId'];
    $academicYear = $conn->real_escape_string($_POST['academicYear']);
    
    // Get students for this class and academic year from student_class_section_data
    $query = "SELECT s.student_id, s.regd_id, s.name, s.gender, s.dob, s.email,
                     s.phone, s.parent_name, s.parent_phone,
                     scsd.section_id, sec.section_name, scsd.status as section_status
              FROM students s
              JOIN student_class_section_data scsd ON s.student_id = scsd.student_id
              LEFT JOIN sections sec ON scsd.section_id = sec.section_id
              WHERE scsd.class_id = $classId 
              AND scsd.academic_year = '$academicYear'
              AND scsd.status = 'active'
              AND s.status = 'Active'
              ORDER BY s.regd_id ASC";
    
    $result = $conn->query($query);
    
    $students = array();
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $students[] = $row;
        }
    }
    
    echo json_encode($students);
} else {
    echo json_encode([]);
}
?>