<?php

function CreateSpor($action,$account,$fineid,$inputdate,$sign, $secret,$reason){
		// Проверка хеша
		$hash = md5($action.$account.$fineid.$inputdate.$secret);
		if($sign!=$hash){
			$arr ['code'] = 103;
			$arr ['message']='Error sign';
			//$arr ['data'] = $hash;
			$result = json_encode($arr);
			echo $result;
			return;
		}
		
		
		// Находим штрафы
		$spor = DoubleSpor($fineid);
		if($spor['code']=='200'){
			$arr ['code'] = 200;
			$arr ['message'] = 'Double spor';
			$arr['data'] = $spor['id'];
			$result = json_encode($arr);
			echo $result;
			return;
		}
		if($spor['code']=='102'){
			$arr ['code'] = 102;
			$arr ['message'] = $spor['message'];
			$result = json_encode($arr);
			echo $result;
			return;
		}

		$spor = CheckSporAccess($account);
		if($spor['code']=='200'){
			$arr ['code'] = 403;
			$arr ['message'] = 'No access.';
			$arr['data'] = $spor['count'];
            $result = json_encode($arr);
			echo $result;
			return; 
		}
		if($spor['code']=='102'){
			$arr ['code'] = 102;
			$arr ['message'] = $spor['message'];
			$result = json_encode($arr);
			echo $result;
			return;
		}



		$spor=InsertSpor($account,$fineid, $inputdate,$reason);
		$arr=[];
		if($spor['code']=='200'){
			$arr ['code'] = 200;
			$arr ['message'] = 'success';
			$arr['data'] = $fineid;
		}else{
			$arr ['code'] = $spor['code'];
			$arr ['message'] = $spor['message'];
			$result = json_encode($arr);	
		}
		$result = json_encode($arr);
		echo $result;
	}


    function DoubleSpor ($fineid)
	{
		global $con;
		$sql = "SELECT id from spor with (nolock)  
				where fineid = '".$fineid."'";

		try{
			$query = sqlsrv_query($con,$sql);
			$arr = [];
			if(sqlsrv_has_rows($query))
			{
				$row = sqlsrv_fetch_array($query);
				$arr = ["code"=>"200","id"=>$row['id'],"message"=>"Double spor"];
			}
			else{
				$arr = ["code"=>"100","id"=>0,"message"=>"No exists"];
			}
			return $arr;
	
		}catch(Exception $ex){
			echo $sql;
			$arr = ["code"=>"102","id"=>0,"message"=>$ex->getMessage(),"sql"=>$sql];
			return $arr;
		}
	}

	function InsertSpor($account,$fineid, $inputdate,$reason){
		global $con;
		$reason = str_replace("\'","",$reason);
		
		$sql = "Insert into spor (account,fineid,inputdate,status,reason)
			values  ('".$account."','".$fineid."','".$inputdate."','0',N'".$reason."');
			SELECT SCOPE_IDENTITY() AS LastInsertedID;";
		try{
			$query = sqlsrv_query($con,$sql);
			if ($query !== false) {
				// Fetch the last inserted ID
				$result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC);
				$lastInsertedId = @$result['LastInsertedID'];
				$arr = ["code"=>"200","id"=>$lastInsertedId,"message"=>"New spor created!"];
				
			} else {
				echo $sql;
				$arr = ["code"=>"102","id"=>0,"message"=>sqlsrv_errors(),"sql"=>$sql];
			}
			return $arr;
		} catch(Exception $ex){
			echo $sql;
			$arr = ["code"=>"102","count"=>0,"message"=>$ex->getMessage(),"sql"=>$sql];
			return $arr;
		}
	}

	function CheckSporAccess($account)
	{
		global $con;
		$sql = "SELECT count(id) as kol from spor with (nolock)  
				where account = '".$account."' and status='0'";

		try{
			$query = sqlsrv_query($con,$sql);
			$arr = [];
			if(sqlsrv_has_rows($query))
			{
				$row = sqlsrv_fetch_array($query);
				if ($row['kol']>20) 
					$arr = ["code"=>"200","id"=>$row['kol'],"message"=>"Count limit is finish!"];
				else
					$arr = ["code"=>"100","id"=>$row['kol'],"message"=>"Count limit is not finish"];
			}
			else{
				$arr = ["code"=>"100","id"=>0,"message"=>"Spor no exists"];
			}
			return $arr;
	
		}catch(Exception $ex){
			echo $sql;
			$arr = ["code"=>"102","kol"=>0,"message"=>$ex->getMessage(),"sql"=>$sql];
			return $arr;
		}
	}

    ?>