<?php
function SporConfirmList($action,$inputdate,$inspector,$sign, $secret,$page){
    $hash = md5($action.$inputdate.$inspector.$secret);
    if($sign!=$hash){
        $arr ['code'] = 103;
        $arr ['message']='Error sign';
        //$arr ['data'] = $hash;
        $result = json_encode($arr);
        echo $result;
        return;
    }
    $kol=getTotalConfirm($inspector);

    $Spor = getConfirmSpor($inspector,$page);
    $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'],
            "Comment"=> $item['comment'],
            "ApprovDate"=> $item['approved_date'],
            "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'])
			
        ];
    }
    $result_array = ["code"=>200,"message"=>"Success","count"=>$kol, "data"=>$data_array];
    $result = json_encode($result_array);
    echo $result;
    return;
}

function getTotalConfirm($inspector){
    global $con;
    $sql = "SELECT count(s.id) as kol
            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.approved_by='".$inspector ."' and s.status=10";
    $query = sqlsrv_query($con,$sql) or die (print_r(sqlsrv_errors()));
   
   if(sqlsrv_has_rows($query))
   {
        $row = sqlsrv_fetch_array($query,SQLSRV_FETCH_ASSOC);
        return $row['kol'];
   }
    return 0;
}


function getConfirmSpor($inspector,$page){
    global $con;
    $sql = "SELECT s.id, s.account, s.fineid as orderID, s.inputdate, s.reason,s.comment,
            s.approved_date, 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.approved_by='".$inspector ."' and s.status=10
            ORDER by s.id OFFSET($page-1) * 10 ROWS FETCH NEXT 10 ROWS ONLY";
    $query = sqlsrv_query($con,$sql) or die (print_r(sqlsrv_errors()));
    $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;
}
?>
