<?php
    include "../config.php";
     $accountID=$_GET['accountID'];
    //$deviceID=isset($_GET['deviceID'])?$_GET['deviceID']:"";
     $sms=str_replace(" ","%20",$_GET['msg']);
     $alerttype=str_replace(" ","%20",$_GET['alerttype']);
     $res=explode(' ',$_GET['msg']);
     $description="";
     $deviceID=strtoupper($res[0]);


    if($accountID=="gvk-up-108"){
       if(!getDeviceServer($deviceID)){ //check vehicle is in present server or not
          $tacserver="up108eastapilocal";
          $gtsserver="up108eastapilocal";
          $LiveEventDBserver="up108eastlocal";
       }
   }

    if($accountID=="gvk-up-102"){
       if(!getDeviceServer($deviceID)){ //check vehicle is in present server or not
          $tacserver="up102eastapilocal";
          $gtsserver="up102eastapilocal";
          $LiveEventDBserver="up102eastlocal";
       }
   }
 


          $deviceID=strtolower($res[0]);
          $geozoneID=strtolower($res[3]);

          $checkres=checkIsVehicleInBusy($accountID,$deviceID);
          if(count($checkres)>0 && $checkres[1]=="assigned"){

              $caseID=$checkres[0];
              $triggerTime=strtotime(date($res[count($res)-4]." ".$res[count($res)-3]));
              if(strpos($geozoneID,"_base")>-1){
                      ////vehicle reached to given Hospital
                     $data=array(
                       "caseID"=> $caseID,
                       "accountID"=>$accountID,
                       "hospitalID"=>$geozoneID,
                       "triggerTime"=> $triggerTime,
                       "triggerType"=>$alerttype
                    );
                   
                   //CallService($data);
                 //  $qry="update caseManager set caseCloseTime='$triggerTime',caseStatus='close' where accountID='$accountID' and caseID='$caseID'";
                  // $tacconnect = mysqli_connect($LiveEventDBserver,$tacusername,$tacpassword) or die ("Unable to connect to the database: " . mysqli_error($tacconnect));
                  // mysqli_select_db($tacconnect,$tacdb);
                  // $qry1 = mysqli_query($tacconnect,$qry) or die(mysqli_error($tacconnect));
                  // mysqli_close($tacconnect);

               }else if(strpos($geozoneID,"_hsptl")>0){//vehicle reached to different Hospital
                   //Call CAD Service
                      $data=array(
                          "caseID"=> $caseID,
                          "accountID"=>$accountID,
                          "hospitalID"=>$geozoneID,
                          "triggerTime"=> $triggerTime,
                          "triggerType"=>$alerttype
                    );
                    //CallService($data);
                     
                  // $qry="update caseManager set destinationReachTimestamp1='$triggerTime',caseStatus='close' where accountID='$accountID' and caseID='$caseID'";
                   //$tacconnect = mysqli_connect($LiveEventDBserver,$tacusername,$tacpassword) or die ("Unable to connect to the database: " . mysqli_error($tacconnect));
                  // mysqli_select_db($tacconnect,$tacdb);
                 // $qry1 = mysqli_query($tacconnect,$qry) or die(mysqli_error($tacconnect));
                 //  mysqli_close($tacconnect);


                }
           }



   function CallService($data){
        //API URL
    /*    $url = $server.'/api/BusTrackService/SendETANotification?apikey='.$apikey;
        //create a new cURL resource
        $ch = curl_init($url);
        //setup request to send json via POST
        $payload = json_encode( $data);
        //attach encoded JSON string to the POST fields
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        //set the content type to application/json
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
        //return response instead of outputting
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //execute the POST request
        $result = curl_exec($ch);
        echo $result;
        //close cURL resource
        curl_close($ch);*/
   } 
   function deleteGeozone($accountID,$geozoneID){
      global $LiveEventDBserver,$gtsusername,$gtspassword,$gtsdb;
       $gtsconnect = mysqli_connect($LiveEventDBserver,$gtsusername,$gtspassword) or die ("Unable to connect to the database: " . mysqli_error($gtsconnect));
       mysqli_select_db($gtsconnect,$gtsdb);
       $deleteqry="delete from Geozone where accountID='$accountID' and geozoneID='$geozoneID'";
      $qry1 = mysqli_query($gtsconnect,$deleteqry) or die(mysqli_error($gtsconnect));
       mysqli_close($gtsconnect);


   }
   

   function checkIsVehicleInBusy($accountID,$deviceID){
        global $LiveEventDBserver,$tacusername,$tacpassword,$tacdb;
        $qry="select caseStatus,caseID,deviceID,caseTimestamp from caseManager where accountID='$accountID' and deviceID='$deviceID' order by caseTimestamp desc limit 1";
        $tacconnect = mysqli_connect($LiveEventDBserver,$tacusername,$tacpassword) or die ("Unable to connect to the database: " . mysqli_error($tacconnect));
        mysqli_select_db($tacconnect,$tacdb);
        $qryres = mysqli_query($tacconnect,$qry) or die(mysqli_error($tacconnect));
        $result=array();
        while($row=mysqli_fetch_assoc($qryres)){
             $result[0]=$row["caseID"];
             $result[1]=$row["caseStatus"];
             $result[2]=$row["deviceID"];
             $result[3]=$row["caseTimestamp"];
               
        } 
        mysqli_close($tacconnect);

        return $result;
   }
 
 
 

?>
