<?php
require("lib/config.php");

// =============== New customer user api ===================================
$data  = $_REQUEST;




$sql = "SELECT * FROM customers where email = '".$_REQUEST['email']."'";

$result = $conn->query($sql);
$row_details = $result->fetch_assoc();

	$cid = $row_details['id'];
	$name = urlencode($row_details['name']);
	$email = $_REQUEST['email'];


if ($result->num_rows > 0) 
 {
 	$time = time() + 15 * 60;
	$sql = "INSERT INTO password_reset (`id`, `customer_id`, `expiry`) VALUES (NULL, '".$cid."', '".$time."')"; 
	$result = $conn->query($sql);
	if(!$result) {
	error_log($conn->error);
	$Res["message"] = "Error requesting reset email";
	  $Res["status"] = "error";
	} else {
	
	$url = "https://www.bookings.perthdesignateddrivers.com.au/google/google-api-php-client-2.2.1/gmailResetEmail.php?name=".$name."&email=".$email."&type=FA";
	error_log($url);
    			$curlObj = curl_init();

			curl_setopt($curlObj, CURLOPT_URL, $url);
			curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
			curl_setopt($curlObj, CURLOPT_HEADER, 0);
			curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
					
			


			$response = curl_exec($curlObj);

			curl_close($curlObj);
			
			if($response == "OK") {
			$Res["message"] = "Password Reset Email Sent";
	  $Res["status"] = "OK";
			
			
			} else {
			error_log($response);
			$Res["message"] = "error sending email";
	  $Res["status"] = "error";
			}
			
			
	}				
		
 }
 else
 {
	  $Res["message"] = "No such email present in database";
	  $Res["status"] = "error";
 }
 
 
echo json_encode(new ArrayObject($Res));





$conn->close();
// =============== New customer user api End Here ==========================


?>