<?php

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

switch ($method) {
    case 'GET':
        $ckodebrg = $_GET["ckodebrg"];
        $query = $conn->query("SELECT nQtyStock FROM vwstock WHERE ckodebrg = '$ckodebrg'");
        
        if ($query->num_rows > 0) {
            $result = $query->fetch_assoc();
            $nQtyStock = (int) $result['nQtyStock'];
            
            if ($nQtyStock <= 0) {
                echo json_encode(['nQtyStock' => 0]);
            } else {
                echo json_encode(['nQtyStock' => $nQtyStock]);
            }
        } else {
            echo json_encode(['nQtyStock' => 0]);
        }
        break;

    default:
        http_response_code(405);
        echo json_encode(['error' => 'Method not allowed']);
        break;
}

mysqli_close($conn);

?>
