<?php
header('content-type:application/json');
include($_SERVER['DOCUMENT_ROOT'] . "/API/connection/conn.php");

$config_type_id = addslashes(isset($_POST['config_type_id']) ? $_POST['config_type_id'] : '');
$name = addslashes(isset($_POST['name']) ? $_POST['name'] : '');
$category = addslashes(isset($_POST['iCategory']) ? $_POST['iCategory'] : '');
$amount = addslashes(isset($_POST['amount']) ? $_POST['amount'] : '');

$query = "SELECT * FROM config WHERE config_type_id = '$config_type_id' AND name = '$name'";
$result = mysqli_query($conn, $query);
$rowCount = mysqli_num_rows($result);
if ($rowCount > 0) {
    $statusCode = 1062;
    $statusMessage = "Entry was not saved. Duplicate entry!";
} else {
    $sql = ("INSERT INTO config (config_type_id, name, category, amount) VALUES ('$config_type_id', '$name','$category', '$amount')");
    if (mysqli_query($conn, $sql)) {
        $statusCode = 200;
        $statusMessage = "Saved Successfully";
    } else {
        $error = "Error: " . mysqli_error($conn);
    }
}

$resultData = array('code' => $statusCode, 'message' => $statusMessage);

echo json_encode($resultData);
