<?php

require_once "config.php";
header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];

switch ($method) {
    case 'GET':
        if(isset($_GET['cjenis'])) {
            $cjenis = $_GET['cjenis'];
            
            $stmt = $conn->prepare("SELECT cjenis AS cnama FROM tbbarang WHERE cdepartment = ? AND cjenis IS NOT NULL AND cjenis != '' AND cFileImage IS NOT NULL AND cFileImage != '' GROUP BY cjenis");
            $stmt->bind_param("s", $cjenis);
            $stmt->execute();
            
            $result = $stmt->get_result();
            $data = $result->fetch_all(MYSQLI_ASSOC);
            echo json_encode($data);
            
            $stmt->close();
        } else {
            echo json_encode(array("error" => "Parameter 'cjenis' is required."));
        }
        break;
}

// Close the database connection
mysqli_close($conn);
?>
