<?php
function SporList($action,$inputdate,$filter,$sign, $secret){
    $hash = md5($action.$inputdate.$secret);
    if($sign!=$hash){
        $arr ['code'] = 103;
        $arr ['message']='Error sign';
        //$arr ['data'] = $hash;
        $result = json_encode($arr);
        echo $result;
        return;
    }
    $Spor = GetSpor($filter);
    $data_array=[];
    foreach ($Spor as $item){
        $data_array [] = [
            "OrderID"=> $item['orderID'],
            "Plate"=> $item['account'],
            "Reason"=> $item['reason'],
            "InputDate" => $item['inputdate'],
            "Amount"=> $item['summa'],
            "FineDate"=> $item['dateFine'],
            "FineName"=> $item['Name'],
            "Video" => $item['metaLinks'],
			"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'])
        ];
		//https://bda.dc.tj/Fines/invoiceImg.php?login=DC&path=c:/Shohin/2024/07/T-22198479_PhotoPlate.jpg&sign=a2baf76d399c8da91d2c9ed7636a5879
    }
	if(!empty($data_array)) {
		$result_array = ["code"=>200,"message"=>"Success","data"=>$data_array];
		$result = json_encode($result_array);
		echo $result;
		return;
	} else {
		$result_array = ["code"=>203,"message"=>"Success","data"=>"Not Found Data"];
		$result = json_encode($result_array);
		echo $result;
		return;
	}
}

function getSpor($filter){
    global $con;
    $sql = "SELECT TOP (10) s.id, s.account, s.fineid as orderID, s.inputdate, s.reason,  
            i.summa, i.dateFine, i.metaLinks,i.metaPlate,i.photo, i.additionalPhoto,
            t.Name
            FROM spor s with (nolock) 
            Inner join Invoices i on s.fineid = i.orderID
            Inner join TypeFine t on i.typeFine = t.tip
            where  t.systemid=i.systemName and s.status=0";
    if (strlen($filter)>0) $sql = $sql . " And s.account like '" .$filter ."'";

    $query = sqlsrv_query($con,$sql) or die (print_r(sqlsrv_errors()));
    $arr = [];
    if(sqlsrv_has_rows($query)){
        while($row = sqlsrv_fetch_array($query)){
			$arr [] = $row;
		}
        
    }
    return $arr;
}
?>
