<?php
	ini_set('display_errors',1);
	include "dist/function.php";
	include "dist/dbqueries.php";
	
	// Делаем проверку метода 
	$method = $_SERVER['REQUEST_METHOD'];
	if($method!='POST'){
		$arr ['code'] = 100;
		$arr ['message']='Method not allowed';
		$result = json_encode($arr);
		echo $result;
		return;
	}
	
	// Читаем параметры
	$data = file_get_contents('php://input');
	$json_input = json_decode($data,1);
	if($json_input === null && json_last_error() !== JSON_ERROR_NONE){
		$arr ['code'] = 101;
		$arr ['message']='Error  json syntax, cannot br parsed';
		$arr ['inputdata']=$data;
		$result = json_encode($arr);
		echo $result;
		return;
	}
	
	$login = $json_input['login'];
	$password = $json_input['password'];
	$inputdate = $json_input['inputdate'];
	$plate = $json_input['plate'];
	$sign = $json_input['sign']; 
	
	
	// Находим данные клиента
	$Userdata = GetUserData($login,$password);
	//dd($Userdata);
	if($Userdata['code']=='100'){
		$arr ['code'] = 102;
		$arr ['message']='User not found';
		$result = json_encode($arr);
		echo $result;
		return;
	}
	//dd($Userdata);
	// Проверка хеша
	$secret = $Userdata [0]['Secret'];
	$hash = md5($login.$password.$inputdate.$plate.$secret);
	if($sign!=$hash){
		$arr ['code'] = 103;
		$arr ['message']='Error sign';
		//$arr ['sign']=$hash;
		$result = json_encode($arr);
		echo $result;
		return;
	}
	
	
	// Находим штрафы
	$Fines = GetFines($plate);
	if($Fines['code']=='100'){
		$arr ['code'] = 104;
		$arr ['message']='Fines not found';
		$result = json_encode($arr);
		echo $result;
		return;
	}
	
	$result_array = ["code"=>200,"message"=>"Success"];
	foreach ($Fines as $item){
		$result_array [] = [
			"Order"=>$item['orderID'],
			"Plate"=>$item['Account'],
			"Amount"=>$item['summa'],
			"FineDate"=>$item['dateFine'],
			"FineName"=>$item['Name']
		];
	}
	$result = json_encode($result_array);
	echo $result;
	
	
	
	
	
	
	
	
	
	
	
	
	
?>