<?php
require_once('../public/connection.php');

$class = $_POST['class'] ?? '';
$term = $_POST['term'] ?? '';
$session = $_POST['session'] ?? '';

// Build WHERE clause
$where = ["exam_type IN ('ca1', 'ca2', 'exam')"];
if (!empty($class)) $where[] = "class = '" . $conn->real_escape_string($class) . "'";
if (!empty($term)) $where[] = "term = '" . $conn->real_escape_string($term) . "'";
if (!empty($session)) $where[] = "session = '" . $conn->real_escape_string($session) . "'";

$where_sql = "WHERE " . implode(" AND ", $where);

// Join to get subject name (optional but recommended)
$query = "
  SELECT exams.id, exams.exam_type, school_subjects.subjects AS subject_name
  FROM exams
  LEFT JOIN school_subjects ON exams.subject_id = school_subjects.id
  $where_sql
  ORDER BY exams.id DESC
";

$result = $conn->query($query);

$exams = [];
while ($row = $result->fetch_assoc()) {
  $exams[] = $row;
}

header('Content-Type: application/json');
echo json_encode($exams);
?>


