<?php
require_once __DIR__ . "/staff_auth.php";
require_once __DIR__ . "/../includes/db.php";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn->set_charset('utf8mb4');

$course_id = (int)($_GET['course_id'] ?? 0);
$branch_id = (int)($_GET['branch_id'] ?? 0);

echo '<option value="">Select an Opti...</option>';

if ($course_id <= 0) exit;

if ($branch_id > 0) {
  $st = $conn->prepare("
    SELECT id, batch_code, batch_name
    FROM batches
    WHERE status='Active' AND course_id=? AND branch_id=?
    ORDER BY batch_name
  ");
  $st->bind_param("ii", $course_id, $branch_id);
} else {
  $st = $conn->prepare("
    SELECT id, batch_code, batch_name
    FROM batches
    WHERE status='Active' AND course_id=?
    ORDER BY batch_name
  ");
  $st->bind_param("i", $course_id);
}

$st->execute();
$rs = $st->get_result();

while($b = $rs->fetch_assoc()){
  $label = trim(($b['batch_code'] ?? '').' '.($b['batch_name'] ?? ''));
  echo '<option value="'.(int)$b['id'].'">'.htmlspecialchars($label, ENT_QUOTES, 'UTF-8').'</option>';
}
