<?php
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");
    header("Access-Control-Allow-Methods: POST");
    header("Access-Control-Max-Age: 3600");
    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
 
    include_once "../config/database.php";
    include_once "../objects/product.php";

    date_default_timezone_set("Asia/Jakarta");

    $database = new Database();
    #$db = $database->getConnection();
    $db = $database->getConnection("sapdash");

    $product = new Product($db);

    $data = json_decode(file_get_contents("php://input"));
    $json1 = json_decode("[".file_get_contents("php://input")."]", true);

    /*
    if (
        #!empty($data->TrsCount) &&
        #!empty($data->RateOut)
        !empty($data->RateOut) &&
        !empty($data->IsCust)
    
    ) {
    */
    if(!$product->is_obj_empty($json1)){

        $product->dash_locId = $_GET['loc_id'];
        $product->label = date('Y-m-d H:i:s');;
        //$product->y = $data->RateOut;  
        //$product->trs_count = $data->TrsCount;      

        if ($product->create($json1)) {
            http_response_code(201);
            echo json_encode(array("message" => "Product was created"));
        }
        else {
            http_response_code(503);
            echo json_encode(array("message " => "Unable to create product"));
        }
    } else {
        http_response_code(400);
        echo json_encode(array("message " => "Unable to create product. Data is incomplete. "));
        echo $data;
    }

?>