<?php

include ("config.inc");

// Check if user already exists without submitting form | CodeWithAwa
// https://codewithawa.com/posts/check-if-user-already-exists-without-submitting-form

if (isset($_POST['email_check'])) {
//   	$email = $_POST['customer_email'];
  	$email = $_POST['email'];
  	$sql = "SELECT * FROM app_users WHERE Email='$email'";
  	$results = mysqli_query($connect, $sql);
  	if (mysqli_num_rows($results) > 0) {
  	  $data["msg"]="taken";
  	  $result = mysqli_fetch_assoc($results);
  	  $data["name"]=$result["Name"];
  	  $data["address"]=$result["address"];
  	  $data["phone"]=$result["Phone"];
  	  echo json_encode($data);
  	}else{
  	  $data["msg"]="not_taken";
  	  echo json_encode($data);
  	}
  	exit();
 }
else
{


$customer_name = $_GET['customer_name'];
$customer_email = $_GET['customer_email'];
$customer_phone = $_GET['customer_phone'];
$customer_address = $_GET['customer_address'];
$customer_password = $_GET['customer_password'];


$sql = "SELECT * FROM app_users WHERE Email='$customer_email'";
  	$results = mysqli_query($connect, $sql);
  	if (mysqli_num_rows($results) > 0) {
  	  echo "Email Already Taken...";
  	  header("location: ../guest/register.php");
  	}
  	else
  	
  	{

//hash code for email verification
// https://code.tutsplus.com/tutorials/how-to-implement-email-verification-for-new-members--net-3824
$hash = md5( rand(0,1000) ); 

// $query = "INSERT into customers(customer_name,customer_username,customer_email,customer_phone,customer_address,customer_password,ehash) VALUES('" . $customer_name . "','" . $customer_username . "','" . $customer_email . "','" . $customer_phone . "','" . $customer_address ."','" . $customer_password ."','" . $hash ."')";
// $success = $connect->query($query);

$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";


$query = "INSERT INTO `guest` (`guest_name`,`guest_phone`, `guest_address`, `guest_gender`, `guest_availability`, `guest_enable`) 
VALUES('$customer_name', '$customer_phone','$customer_address','$gender', '$status', '$enable')";


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

$id = mysqli_insert_id($connect);

if($id)
{
    

    $query = "INSERT into app_users (`Name`,`Email`,`Phone`,`Address`,`Password`, `GuestId`,ehash) 
    VALUES('" . $customer_name . "','" . $customer_email . "','" . $customer_phone . "','" . $customer_address ."','" . $customer_password ."','$id','" . $hash ."')";

    $result = $connect->query($query);
}
else
{
    
}


// $query = "UPDATE customers set app_usersid = '$id' WHERE customer_email = $customer_email";
// $result1 = $connect->query($query);

if (!$result) {
$message="Couldn't Enter Data";

    echo "
    <script type='text/javascript'>
if(!alert('$message')) document.location = 'register.php';
</script>
";
	die("Couldnt enter data: ".$connect->error);
}



// email verification goes here

$to      = $customer_email; // Send email to our user
$subject = 'CCL Limo New Signup | Verification'; // Give the email a subject 
$message = '
  
Thanks for signing up!
Your account has been created, you can login with the following credentials after you have activated your account by pressing the url below.
  
------------------------
Username: '.$customer_email.'
Password: '.$customer_password.'
------------------------
  
Please click this link to activate your account:
https://cclimollc.com/new/guest/verify.php?email='.$customer_email.'&hash='.$hash.'
  
'; // Our message above including the link
                      
$headers = $companyemail . "\r\n"; // Set from headers
mail($to, $subject, $message, $headers); // Send our email

// email verification goes here

/*$connect->close();*/

$message="Thanks for signing up!
Kindly verify your Email before login. You can activate your account by pressing the url sent by email.";

echo $message;

header( "refresh:5;url=login.php" );
}
}
?>