<?php
// api/endpoints/siswa_get.php
$id      = $_GET['id']      ?? null;
$kelas   = $_GET['kelas']   ?? null;
$jurusan = $_GET['jurusan'] ?? null;
$search  = $_GET['search']  ?? null;

$sql    = "SELECT id,nis,nama,kelas,jurusan,tahun_ajaran,no_hp,alamat,status FROM siswa WHERE status='aktif'";
$params = [];
if ($id)      { $sql .= " AND id=?";                         $params[] = $id; }
if ($kelas)   { $sql .= " AND kelas=?";                      $params[] = $kelas; }
if ($jurusan) { $sql .= " AND jurusan=?";                    $params[] = $jurusan; }
if ($search)  { $sql .= " AND (nama LIKE ? OR nis LIKE ?)";  $params[] = "%$search%"; $params[] = "%$search%"; }
$sql .= " ORDER BY kelas, nama";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll();

$result = [];
foreach ($rows as $r) {
    $result[] = [
        'id'          => (string)$r['id'],
        'nis'         => $r['nis'],
        'nama'        => $r['nama'],
        'kelas'       => $r['kelas'],
        'jurusan'     => $r['jurusan'],
        'tahunAjaran' => $r['tahun_ajaran'] ?? '',
        'noHp'        => $r['no_hp']        ?? '',
        'alamat'      => $r['alamat']       ?? '',
    ];
}

respond(['status' => 'success', 'data' => $result, 'total' => count($result)]);