<?php
require_once __DIR__ . '/_auth.php';
header('Content-Type: application/json; charset=utf-8');

$cid = (int)($_GET['course_id'] ?? 0);
$rows = [];
if ($cid > 0) {
  $st = mysqli_prepare($conn, "SELECT id,name,start_date,status
                               FROM batches
                               WHERE course_id=? AND status IN ('Scheduled','Ongoing')
                               ORDER BY start_date");
  mysqli_stmt_bind_param($st, "i", $cid);
  mysqli_stmt_execute($st);
  $r = mysqli_stmt_get_result($st);
  while ($x = mysqli_fetch_assoc($r)) $rows[] = $x;
  mysqli_stmt_close($st);
}
echo json_encode(['ok'=>true, 'rows'=>$rows]);
