<?php

require_once "config.php";

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

$method = $_SERVER['REQUEST_METHOD'];

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

        if (!empty($kode)) {
            if (!empty($cnoid)) {
                $query = $conn->prepare("SELECT * FROM tbalamatkirim WHERE ckodecust = ? AND cnoid = ? ORDER BY cnoid DESC");
                $query->bind_param("ss", $kode, $cnoid);
            } else {
                $query = $conn->prepare("SELECT * FROM tbalamatkirim WHERE ckodecust = ? ORDER BY cnoid DESC");
                $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);

?>
