<?php include("../cn.php");

//ajax function

if (isset($_POST['email_check'])) {
  	$email = $_POST['email'];
  	$sql = "SELECT * FROM app_users WHERE Email='$email'";
  	$results = mysqli_query($connect, $sql);
  	if (mysqli_num_rows($results) > 0) {
  	  echo "taken";
  	}else{
  	  echo 'not_taken';
  	}
  	exit();
  } else if (isset($_POST['dlcheck'])) {
  	$dlnumber = $_POST['dlnumber'];
  	$sql = "SELECT * FROM driver WHERE dl_number ='$dlnumber'";
  	$results = mysqli_query($connect, $sql);
  	if (mysqli_num_rows($results) > 0) {
  	  echo "taken";
  	}else{
  	  echo 'not_taken';
  	}
  	exit();
  }
//ajax function end

else {
// $driverid = $_REQUEST["driverid"];
$dname = $_REQUEST["dname"];
$contact = $_REQUEST["contact"];
$dlnumber = $_REQUEST["dl_number"];
$email = $_REQUEST["email"];
$password = $_REQUEST["password"];
$address = $_REQUEST["address"];
// $gender = filter_input(INPUT_POST, 'gender', FILTER_SANITIZE_STRING);
$gender = "Male";
$status = "yes";
$enable = "enable";
$Emailverified ="1";

$img_name = date("Ymdis");
$photo=($_FILES["photo"]["name"]!="")?"drivers/driver_".$img_name.".".pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION):"";
if($_FILES["photo"]["name"]!="")
{
    move_uploaded_file($_FILES["photo"]["tmp_name"],"../".$photo);

}

$query = "INSERT INTO `driver` (`driver_name`,`dl_number`,`driver_phone`, `driver_address`, `driver_gender`, `Image`, `driver_availability`, `driver_enable`) 
VALUES('$dname', '$dlnumber', '$contact','$address','$gender', '$photo', '$status', '$enable')";

// $myfile = fopen("testfile.txt", "w");
// fwrite($myfile, $query);
// fclose($myfile);

$result1 = mysqli_query($connect, $query);
$result2;

$id = mysqli_insert_id($connect);

if($id)
{
    $query = "INSERT INTO `app_users` (`Name`,`Password`,`Phone`, `Email`, `Image`, `DriverId`, `emailverified`) VALUES(
        '$dname','$password','$contact','$email','$photo','$id','$Emailverified')";
    $result2 = mysqli_query($connect, $query);
}
else
{
    
}

header("location:../drivers.php");
}
?>