<?php

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: GET, OPTIONS");
header("Content-Type: application/json");

if ($_SERVER["REQUEST_METHOD"] === "OPTIONS") {
    exit;
}

require "../../config/database.php";
require "../../helpers/response.php";

$sppg_id = isset($_GET["sppg_id"]) ? (int) $_GET["sppg_id"] : 0;

if ($sppg_id <= 0) {
    response(false, "ID SPPG tidak valid", []);
}

$sql = "
    SELECT
        id,
        sppg_id,
        nama_sekolah,
        jenjang,
        jumlah_siswa,
        created_at
    FROM sekolah
    WHERE sppg_id = ?
    ORDER BY id DESC
";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $sppg_id);
$stmt->execute();

$result = $stmt->get_result();

$data = [];

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

response(true, "Data sekolah berhasil diambil", $data);

?>