<?php

// протокол для создания спор штрафа 
// POST JSON $action=create, $account-номер машины , $fineid-номер штрафа, $inputdate-дата, $sign-подпись 

//Протокол для получения списка споров на штрафы 
//POST json $action -list, $inputdate, $filter ,$sign=$action.$inputdate.$secret, secret=sp0rF!nes

	ini_set('display_errors',1);
	include "dist/dbqueries.php";
	include_once "dist/SporCreate.php";
	include_once "dist/SporList.php";
	include_once "dist/SporStatus.php";
	include_once "dist/SporCancel.php";	
	include_once "dist/SporConfirm.php";
	include_once "dist/SporConfirmList.php";
	include_once "dist/SporCancelList.php";	
	include_once "dist/SporAudit.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 ['data'] = $data;
		$result = json_encode($arr);
		echo $result;
		return;
	}


	$secret = "sp0rF!nes";
	if (isset($json_input['action'])) $action = $json_input['action']; else $action="";
	if (isset( $json_input['account'])) $account = $json_input['account']; else $account="";
	if (isset($json_input['fineid'])) $fineid = $json_input['fineid']; else $fineid="";
    if (isset($json_input['inputdate'])) $inputdate = $json_input['inputdate']; else $inputdate="";
	if (isset($json_input['filter'])) $filter = $json_input['filter']; else $filter="";
	if (isset($json_input['comment'])) $comment = $json_input['comment']; else $comment="";
	if (isset($json_input['reason'])) $reason = $json_input['reason']; else $reason="";
	if (isset($json_input['inspector'])) $inspector = $json_input['inspector']; else $inspector="";
	if (isset($json_input['type'])) $type = $json_input['type']; else $type="";
	if (isset($json_input['sign'])) $sign = $json_input['sign']; else $sign="";

	if (isset($json_input['page'])) $page = $json_input['page']; else $page="1";
	if ($page<1) $page=1;


	switch($action){
		case "create":
			CreateSpor($action,$account,$fineid,$inputdate,$sign, $secret,$reason);
			break;
		case "list":
			SporList($action,$inputdate,$filter,$sign, $secret);
			break;
		case "confirmlist":
			SporConfirmList($action,$inputdate,$inspector,$sign, $secret,$page);
			break;
		case "cancellist":
			SporCancelList($action,$inputdate,$inspector,$sign, $secret,$page);
			break;
		case "status":
			SporStatus($action,$inputdate,$fineid,$sign, $secret);	
			break;
		case "confirm":
			SporConfirm($action,$inspector,$fineid,$inputdate,$comment,$sign,$secret);
			break;
		case "cancel":
			SporCancel($action,$inspector,$fineid,$inputdate,$comment,$sign,$secret);
			break;	
		case "audit":
			SporAudit($action,$inspector,$inputdate,$sign,$secret,$type);
			break;	
		case "auditprocess":
			SporAuditAccept($action,$inspector,$inputdate,$sign,$secret,$type,fineid);
			break;
		default:
			$arr ['code'] = 404;
			$arr ['message'] = 'Undefined action';
			$result = json_encode($arr);
			echo $result;
		return;
	}

	?>