<?php
// admin/unit_api.php
require_once "../includes/db.php";
$conn=db();
header("Content-Type: application/json");

if(isset($_GET['listCourses'])){
  $q=$conn->query("SELECT course_name FROM courses ORDER BY course_name");
  $out=[]; while($r=$q->fetch_assoc()) $out[]=$r['course_name'];
  echo json_encode($out); exit;
}

$course=$_GET['course']??'';
$out=[];
if($course!=""){
  $stmt=$conn->prepare("SELECT unit_name FROM units WHERE course_name=? ORDER BY unit_name");
  $stmt->bind_param("s",$course);
  $stmt->execute();
  $res=$stmt->get_result();
  while($r=$res->fetch_assoc()) $out[]=$r['unit_name'];
}
echo json_encode($out);
