<?php

require_once "config.php";

header('Content-Type: application/json');

$method = $_SERVER['REQUEST_METHOD'];

switch ($method) {
    case 'GET':
        $kode = isset($_GET['kode']) ? $_GET['kode'] : '';

        if (!empty($kode)) {
            $query = $conn->prepare("SELECT ckodekota as ckode, cnamakota as cnama FROM tblokasi WHERE ckodeprovinsi = ? GROUP BY cnamakota ORDER BY cnamakota ASC");
            $query->bind_param("s", $kode);
            $query->execute();
            $result = $query->get_result();
            $data = $result->fetch_all(MYSQLI_ASSOC);
            echo json_encode($data);
        } else {
            echo json_encode(["error" => "No 'kode' parameter provided"]);
        }
        break;
}

mysqli_close($conn);

?>
