<?php
//{"login": "DC","action": "alarm","inputdate": "[inputdate]","data":"[data]","sign":"[sign]"}
header('Content-Type: application/json');
date_default_timezone_set('Asia/Dushanbe');
$jsonData = file_get_contents('php://input');
$data = json_decode($jsonData, true);
$response = ["Code"=>201,"Message"=>"Error params"];
if ($data == null) {
    $response["Message"]="Error decoding JSON data! ";
    echo json_encode($response);
    exit; 
}
$username = @$data['login'];
$action=@$data['action'];
$inputdate= @$data['inputdate'];
$inputs = @$data['data'];
$sign = @$data['sign'];

$res=CheckInputData($action, $username, $inputdate, $inputs, $sign);
if ($res != "") {
    $response["Message"]=$res;
    echo json_encode($response);
    exit; 
}

$the_date = new DateTime($inputs['the_date']);

$id=$inputs['id'];
$plate=$inputs['car_number'];
$fine_date= $the_date->format('YmdHis'); //$inputs['the_date'];
$only_fine_date=$the_date->format('Ymd');
$address=$inputs['address'];
$status=$inputs['sts'];
$fines=$inputs['fines'];

//insert into alarm values id, plate, fine_date, address, status, fines, inputdate
// надо и тонировка, техосмотр, путевой лист и т д

$dbname = "AMI";
$dbhost = "10.10.11.107";
$dblogin = "sa";
$dbpassword = "Rubb!t23";

$dsn = "sqlsrv:server=$dbhost;Database=$dbname";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];

$data = [
    'event_id' => $id,
    'car_number' => $plate,
    'only_date'=>$only_fine_date,
    'fine_date' => $fine_date,
    'fines' => $fines,
    'inputdate'=>date('YmdHis'),
    'address' => $address,
    'status' => $status,
    'techosmotr' => NULL,
    'tonirovka' => NULL,
    'P0'=>NULL
];

try{
    insertIntoDatabase($dsn, $dblogin, $dbpassword, $options, 'alarm', $data);
    $response["Code"]="200";
    $response["Message"]="OK";
}catch(PDOException $e){
    if ($e->getCode() == '23000') { 
        $response["Code"]="200";
        $response["Message"]="Duplicate entry for car_number and only_date.";
    } else{
        $response["Message"]="Error insert: " . $e->getMessage();
    }
}

echo json_encode($response);


function CheckInputData($action, $username, $inputdate, $inputs, $sign)
{
    if (empty($username) or empty($action) or empty($inputdate) or empty($inputs) or empty($sign)){
        return "Input parameters error";
       }
       $password= "DC@mi2401";
       $hash = $action . $inputs['car_number'] . $inputdate . $password;
       $hashmd5 =strtoupper(md5($hash));
       $sign=strtoupper($sign);
       if ($hashmd5!=$sign){
            return "Error sign";
       }
       return "";
}

function insertIntoDatabase($dsn, $username, $password, $options, $tableName, $data) {
        $pdo = new PDO($dsn, $username, $password, $options);
        $columns = implode(", ", array_keys($data));
        $placeholders = ":" . implode(", :", array_keys($data));
        $sql = "INSERT INTO $tableName ($columns) VALUES ($placeholders)";
        $stmt = $pdo->prepare($sql);
        foreach ($data as $key => $value) {
            $stmt->bindValue(":$key", $value);
        }
        $stmt->execute();
        $pdo = null;
}
?>