<?php

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

switch ($method) {
    case 'GET':
        $ckode = $_GET['ckode'];
        $stmt = $conn->prepare("SELECT cKode, cNama, cSat, QtyMin, HargaPerPcs FROM vwqtyhargabarang WHERE cKode = ? ORDER BY QtyMin DESC"); 
        $stmt->bind_param("s", $ckode);
        $stmt->execute();

        $result = $stmt->get_result();
        $data = $result->fetch_all(MYSQLI_ASSOC);

        foreach ($data as &$row) {
            $row['HargaPerPcs'] = rtrim(rtrim($row['HargaPerPcs'], '0'), '.');
            $row['QtyMin'] = rtrim(rtrim($row['QtyMin'], '0'), '.');
            $row['cSat'] = str_replace("pcs", "", $row['cSat']);
        }

        echo json_encode($data);

        $stmt->close();
        break;

    default:
        echo json_encode(array("error" => "Unsupported request method."));
        break;
}

$conn->close();
?>
