<?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: ../booking/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);


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

$result = $connect->query($query);

$id = mysqli_insert_id($connect);

// $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 = 'VTL Luxury Vehicles 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:
http://www.vtl.ae/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" );
}
}
?>