<?php

require_once "config.php";

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

$method = $_SERVER['REQUEST_METHOD'];

switch ($method) {
    case 'GET':

        $harga_input = isset($_GET['harga']) ? intval($_GET['harga']) : 0;

        if ($harga_input > 0) {

            $query = $conn->prepare("
                SELECT npotonganharga
                FROM tbdiskonongkir
                WHERE nharga <= ?
                ORDER BY nharga DESC
                LIMIT 1
            ");
            $query->bind_param("i", $harga_input);
            $query->execute();
            $result = $query->get_result();
            $data = $result->fetch_assoc();

            if ($data) {
                echo json_encode([
                    "potongan" => (int)$data['npotonganharga']
                ]);
            } else {
                echo json_encode([
                    "potongan" => 0
                ]);
            }

        } else {
            echo json_encode([
                "potongan" => 0
            ]);
        }

        break;
}

mysqli_close($conn);

?>