<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: OPTIONS,GET,POST,PUT,DELETE,REQUEST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include 'config.php';
date_default_timezone_set("Africa/Lagos");
$date_t = date("d-M-Y h:i:s");
$today = date("d-m-Y");
$time = date("d-M-Y-h:i:s");

if(isset($_POST['user_id']) && isset($_POST['id']))
{


function check_input($par){
  $par = trim($par);
  $par = stripslashes($par);
  $par = htmlspecialchars($par);
  return $par;
}

$user_id     =  check_input($_POST['user_id']);
$id     =  check_input($_POST['id']);
$auth      =  check_input($_POST['auth']);
$lat      =  check_input($_POST['lat']);
$lng      =  check_input($_POST['lng']);

$user_id  = mysqli_real_escape_string($con, $user_id);
$id  = mysqli_real_escape_string($con, $id);
$auth   = mysqli_real_escape_string($con, $auth);
$lat   = mysqli_real_escape_string($con, $lat);
$lng   = mysqli_real_escape_string($con, $lng);







if(empty($user_id))
{
    $status = false;
    $msg = "User Not Defined";
}

else if(empty($lat))
{
    $status = false;
    $msg  ="Lat is Empty";
}

else if(empty($lng))
{
    $status = false;
    $msg  ="Lng is Empty";
}


else{
      
      
$sql="SELECT * FROM users WHERE user_id = '$user_id' AND auth = '$auth'";
$result=$con->query($sql) or die ("error: Server error ".mysqli_error($con));
$count=mysqli_num_rows($result);

if ($count == 0)
{
        $suc = "User Does Not Exist"; 
         $status = false; 


}
else
{
      
 include 'src/get_locality.php';
$address= getaddress($lat,$lng);
  
$sqlx="UPDATE movements SET status = 'Completed', location = '$address', lat = '$lat', lng = '$lng'  WHERE user_id = '$user_id' AND id = '$id'";
$resultx=$con->query($sqlx) or die ("error2: Server error ".mysqli_error($con));

      

 
 if($resultx)
 {
 
 
       $status = true;
        $msg = "Successful";
 }
 else{
     
     
     $status = false;
     $msg = "Failed to Update";
 }

}

}

$con->close();


 $output = array('status'=>$status, 'success'=>$msg);
 echo json_encode($output); //output JSON data

}
?>