<?php
session_start();
/*use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php'; */

header('Access-Control-Allow-Origin: *');

header('Access-Control-Allow-Methods: GET, POST');

header("Access-Control-Allow-Headers: X-Requested-With");


if(isset($_POST['user_id']))
{
require 'config.php';


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



$auth=check_input($_POST['auth']);
$user_id=check_input($_POST['user_id']);
$lat=check_input($_POST['lat']);
$lng=check_input($_POST['lng']);
$from=check_input($_POST['from']);
$to=check_input($_POST['to']);
$reason=check_input($_POST['reason']);
$ride_company=check_input($_POST['ride_company']);
$driver=check_input($_POST['driver']);
$vehicle_info=check_input($_POST['vehicle_info']);


$auth = mysqli_escape_string($con, $auth);
$user_id=mysqli_escape_string($con, $user_id);
$lat=mysqli_escape_string($con, $lat);
$lng=mysqli_escape_string($con, $lng);
$from=mysqli_escape_string($con, $from);
$to=mysqli_escape_string($con, $to);
$reason=mysqli_escape_string($con, $reason);
$ride_company=mysqli_escape_string($con, $ride_company);
$driver=mysqli_escape_string($con, $driver);
$vehicle_info=mysqli_escape_string($con, $vehicle_info);



// include 'src/get_address.php';


$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 == 1)
{
$rows = mysqli_fetch_array($result);
$phone = $rows['phone'];
$email = $rows['email'];
$firstname = $rows['firstname'];
$lastname = $rows['lastname'];

date_default_timezone_set("Africa/Lagos");

  $date_t = date("d-M-Y h:i:s a"); 
  
 
 include 'src/get_locality.php';
 $address= getaddress($lat,$lng);


  $sqlx=$con->query("INSERT INTO movements  (user_id, user, firstname, lastname, phone, lat, lng, location,  origin, destination, reason, ride_company, driver, vehicle_info, status,  date_t) 
      VALUES ('$user_id', '$email', '$firstname', '$lastname', '$phone', '$lat', '$lng', '$address', '$from', '$to', '$reason', '$ride_company', '$driver', '$vehicle_info', 'In Progress',  '$date_t')") or die("Error: ".mysqli_error($con));
      
      
  
if($sqlx)
{
    

/*$subject = "Your Verification Code!";
$message = '<!DOCTYPE HTML>     
    <html>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
</head>
<body>
<div style="background-color:#eee; border:solid; border-width:thin; border-color:#EEE; position: relative; font-size:13px; padding-top:2em; padding:1em; font-family:Verdana, Geneva, sans-serif">
<div style="background-color:white; width: 80%; margin:auto; dborder:solid; dborder-width:thin; bdorder-color:#EEE; position: relative; font-size:13px; dtext-align: center; line-height: 20px; padding-top:2em; padding:1.5em; font-family:Verdana, Geneva, sans-serif">
<div style="width: 100%; text-align: center;">
</div>
<p>You just created an account on Cordis.</p>
<p>Your verification code is '.$code.'</p>
</div>
</div>

</body>
</html>';




$mail = new PHPMailer();

require 'mail_head.php';

$mail->setFrom('dev@diimdevs.com', 'Diim Devs');
$mail->addAddress($email);
//$mail->addAddress("gbite.aina@gmail.com");
$mail->addBcc("diimtech@gmail.com" );
$mail->Subject = $subject;
$mail->msgHTML($message);


/*$mail1->WordWrap = 50;                                 // set word wrap to 50 characters
//$mail->AddAttachment("/var/tmp/file.tar.gz");         // add attachments
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
$mail1->IsHTML(true);                                  // set email format to HTML */

//$mail->Send();


$suc = "Successful"; 
$status = true; 

}
}
else
{
         $suc = "User Does Not Exist"; 
         $status = false; 


}

$con->close();

$output = array('status' => $status, 'success'=>$suc, 'location' => $location,  'email'=> $email, 'phone'=> $phone);
echo json_encode($output); //output JSON data
}
?>
