<?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']);
$alert=check_input($_POST['alert']);
$lat=check_input($_POST['lat']);
$lng=check_input($_POST['lng']);


$auth = mysqli_escape_string($con, $auth);
$user_id=mysqli_escape_string($con, $user_id);
$alert=mysqli_escape_string($con, $alert);
$lat=mysqli_escape_string($con, $lat);
$lng=mysqli_escape_string($con, $lng);




$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);
$time  = time();

$filex =  $time.'-'.$user_id;

$video = $_FILES["file"]["tmp_name"]; 
            // Compress size and upload image 
            $save_video=$filex.'.mp4';
            $path ='../videos/'.$save_video;
    
if (move_uploaded_file($video, $path))
{
    $video_path = $p_url."/videos/".$save_video;          
}
    
            


  $sqlx=$con->query("INSERT INTO alerts  (user_id, user, firstname, lastname, phone, lat, lng, location, emergency, video,  status, date_t) 
      VALUES ('$user_id', '$email', '$firstname', '$lastname', '$phone', '$lat', '$lng', '$address', '$alert', '$video_path', 'New', '$date_t')") or die("Error: ".mysqli_error($con));
      
      
  
if($sqlx)
{
    
$suc = "Successful"; 
$status = true; 

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

$con->close();

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