<?php
	header('Content-Type: application/json; charset=utf-8');
	ini_set("display_errors",1);
	include "dist/function.php";
	include "dist/dbqueries.php";

	$jsonData = file_get_contents('php://input');
	$data = json_decode($jsonData, true);
	$account = $data['account'];
	$action = $data['action'];
	$sign = $data['sign'];
	$secret = "rbdabyphone";
	$mysign = md5($account.$action.$secret);
	if($mysign<>$sign){
			
			$arr = [
				"code"=>100,
				"comment"=>"ErrorSign"
			];
			echo json_encode($arr);
			return;
			
	}
	
	if($action=="phone"){
		$getallfines = GetallfinesbyPhone($account);
		if($getallfines['code']=='100'){
			
			$arr = [
				"code"=>100,
				"comment"=>"Not found"
			];
			echo json_encode($arr);
			return;
			
		}
		
		$arr = [];
		//dd($getallfines);
		foreach ($getallfines as $item){
			$arr []=  [
				"plate"=>$item['account'],
				"summa"=>$item['summa'],
				"metaPlate"=>str_replace('http://217.11.177.108:9907','https://bda.dc.tj',$item['metaPlate']),
				"photo"=>str_replace('http://217.11.177.108:9907','https://bda.dc.tj',$item['photo']),
				"additionalPhoto"=>str_replace('http://217.11.177.108:9907','https://bda.dc.tj',$item['additionalPhoto']),
				"finedate"=>$item['Inputdate'],
				"finetime"=>Vaqt($item['Inputdate']),
				"fineday"=>Sana($item['Inputdate']),
				"finename"=>$item['Name'],
				"address"=>$item['Address'],
				"video"=>$item['metalinks'],
				"value"=>$item['Value'],
				"orderid"=>$item['orderID'],
				"systemname"=>$item['systemName'],
				"sporstatus"=>$item['spostat'] // 0-В очереди на рассмотрении;2- Нет заявки можно выводить;3- Отклонено;10- Обработан;
			]; 
			
		}		
		$proceed_result  = ["code"=>"200","comment"=>"Success","fines"=>$arr];
		echo json_encode($proceed_result);
	}
	
	
	
	
	if($action=="orderid"){
		$getallfines = Getallfines($account,'2');
		if($getallfines['code']=='100'){
			
			$arr = [
				"code"=>100,
				"comment"=>"Not found"
			];
			echo json_encode($arr);
			return;
			
		}
		
		$arr = [];
		//dd($getallfines);
		foreach ($getallfines as $item){
			$arr [] =  [
				"plate"=>$item['account'],
				"summa"=>$item['summa'],
				"metaPlate"=>str_replace('http://217.11.177.108:9907','https://bda.dc.tj',$item['metaPlate']),
				"photo"=>str_replace('http://217.11.177.108:9907','https://bda.dc.tj',$item['photo']),
				"additionalPhoto"=>str_replace('http://217.11.177.108:9907','https://bda.dc.tj',$item['additionalPhoto']),
				"finedate"=>$item['Inputdate'],
				"finename"=>$item['Name'],
				"video"=>$item['metalink'],
				"value"=>$item['Value'],
				"total"=>$item['totalsumma'],
				"totalkol"=>$item['totalkol'],
				"orderid"=>$item['orderID'],
				"shtrafdate"=>$item['finedate']
			]; 
			
		}		
		
		echo json_encode($arr);
	}
	
	
	if($action=="gethistory"){
		$gethistory = History($account);
		if($gethistory['code']=='100'){
			
			$arr = [
				"code"=>100,
				"comment"=>"Not found"
			];
			echo json_encode($arr);
			return;
			
		}
		
		$arr = [];
		//dd($getallfines);
		foreach ($gethistory as $item){
			$arr [] =  [
				"summa"=>$item['summa'],
				"orderid"=>$item['orderID'],
				"typeFine"=>$item['typeFine'],
				"Inputdate"=>SanaTime($item['Inputdate']),
				"dateFine"=>$item['dateFine'],
				"tip"=>$item['tip'],
				"name"=>$item['Name']
		];
			
		}		
		
		echo json_encode($arr);
	}
	
	
	
?>