<?php
    date_default_timezone_set('Asia/Kolkata');
    include "../config.php";
    $jsonresponse='';
    
    error_log("Request for vehicle Past Status AccountID:".$_POST['accountID']." VehicleID: ".$_POST['time']." ");
    $user=$_POST['user'];
    $password=$_POST['password'];
    $time=$_POST['time'];
    if(!($user==$apiusername && $password==$apipassword)){
        saveRequest($accountID,$deviceID,0,0,0,time(),"Incorrect API Credentials","Vehicle Past Info");
        echo '{"result": "' ."Incorrect API credentials".$password .'"}';
        return;
   }

    $accountID=isset($_POST['accountID'])?$_POST['accountID']:"";
    $deviceID=isset($_POST['deviceID'])?$_POST['deviceID']:"";
    if($accountID!="" && $deviceID!=""){
         saveRequest($accountID,$deviceID,0,0,0,time(),"success","Vehicle Past Info");
        $result=getLatLong($accountID,$deviceID);
        $result["Status"]="success";
        $result["ReceivedTime"]=date('d-M-Y h:i:s a',time());
        $jsonresponse= json_encode(utf8ize($result));
        header('Content-Type: application/json');
        echo $jsonresponse;
    }else {
        saveRequest($accountID,$deviceID,0,0,0,time(),"provid Valid options","Vehicle Past  Info");
        $jsonresponse= '{"Status":"fail","ReceivedTime":"'.date('d-M-Y h:i:s a',time()).'","result":"Please Provide Valid Options"}';
        header('Content-Type: application/json');
        echo $jsonresponse;
   }
  function getLatLong($accountID,$deviceID,$time) {
      global $gtsserver,$gtsusername,$gtspassword,$gtsdb,$LiveEventDBserver;
      $gtsconnect = mysqli_connect($LiveEventDBserver,$gtsusername, $gtspassword) or die ("Unable to connect to the database1 : " . mysqli_error($gtsconnect));
      mysqli_select_db($gtsconnect, $gtsdb);

      $query="SELECT deviceID,statusCode,speedKPH,from_unixtime(timestamp, '%Y-%m-%d %H:%i:%s' ) as time1,odometerOffsetKM+odometerKM as 'odometerKM',latitude,longitude,address,heading as direction FROM EventData where accountID='$accountID' and deviceID='$deviceID' and timestamp <=$time andlatitude!=0 and longitude!=0 ORDER BY timestamp Desc Limit 1";
      $qry_result2 = mysqli_query($gtsconnect, $query) or die(mysqli_error($gtsconnect));
      $data=array();
      while($row1 = mysqli_fetch_assoc($qry_result2)) {
            $data["Last Sync Lat/Long"]=round($row1['latitude'],5).",".round($row1['longitude'],5);
            $data["Last Sync Time"]=$row1["time1"];
            $data["Speed"]=$row1["speedKPH"];
     }
     /*

      $sql = "Select * from Device where accountID='$accountID' and deviceID='".$deviceID."' ";
      $qry_result = mysqli_query($gtsconnect, $sql) or die(mysqli_error($gtsconnect));
      $data=array();
      while($row = mysqli_fetch_assoc($qry_result)) {
          $data["Last Sync Lat/Long"]=round($row['lastValidLatitude'],5).",".round($row['lastValidLongitude'],5);
          $data["Speed"]=$row['lastValidSpeedKPH'];
          $data["Last Sync Time"]=date('d-M-Y H:i:s',$row['lastEventTimestamp']);
      }*/
     mysqli_close($gtsconnect);
      return $data;
 }


?>
