<?php
require("lib/config.php");
require_once 'vendor/eway/eway-rapid-php/include_eway.php';
// =============== Get customer user api ===================================
$data  = $_REQUEST;
$sql = "SELECT * FROM customers where email = '".$_REQUEST['username']."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) 
{
    while($row = $result->fetch_assoc()) 
	{
        $customer[] = $row;
		$sql = "SELECT * FROM `customer_details` where `cid` = '".$row["id"]."'"; 
		$result = $conn->query($sql);
		if ($result->num_rows > 0) 
		{
			while($row_details = $result->fetch_assoc()) 
			{
				$cust_detail[] = $row_details;
			}
		}
		
		$sql = "SELECT * FROM `customer_tokens`  where `cid` = '".$row["id"]."'"; 
		$result = $conn->query($sql);
		if ($result->num_rows > 0) 
		{
			while($row_token = $result->fetch_assoc()) 
			{
				$cust_token[] = $row_token;
			}
		}
    }
    
    	$dbhost = "localhost";
	$dbuser = "perthdes_coder";
	$dbpass = "coder1234!@#$";
	$dbname = "perthdes_new_pro_api";
	$conn = new mysqli($dbhost , $dbuser , $dbpass , $dbname );
// Check connection
 
	
	$sql = "SELECT * FROM customers where email = '".$_REQUEST['username']."'";
	$result = $conn->query($sql);
	if ($result->num_rows > 0) 
	{
		$Res["message"] = "Customer Already Exists"; 
		$Res["staus"] = "error";
		} else {


	// Create connection
		$conn = new mysqli($dbhost , $dbuser , $dbpass , $dbname );
		// Check connection
		if ($conn->connect_error) {
  			  die("Connection failed: " . $conn->connect_error);
		} 
		
		$fullname = $customer[0]["name"];
		
    
    			$sql = "INSERT INTO `customers` (`id`, `name`, `mobile`, `email`,`password`, `firebase_token`, `promo_code`, `promo_used`,  			`is_delete`, `created`, `modified`) VALUES (NULL, '".$fullname."', '".$customer[0]["mobile"]."', '".$_REQUEST["username"]."', '".$customer[0]["password"]."', '', 'COPYFC', '0', '0',NOW() ,NOW() )";
    		if ($conn->query($sql) === TRUE) 
			{
			$cid = $conn->insert_id;
				if($conn->insert_id)
				{
			$sql_detail = "INSERT INTO `perthdes_new_pro_api`.`customer_details` (`id`, `cid`, `address`, `addr_lat`, `addr_long`) VALUES (NULL, '".$cid."', '".$cust_detail[0]["address"]."', '".$cust_detail[0]["addr_lat"]."','".$cust_detail[0]["addr_long"]."')";
					
					if ($conn->query($sql_detail) === TRUE) 
					{
						//$Res["customer_detail_id"] = $conn->insert_id;
						//$Res["customer_detail_status"] = "OK";
						$success[] = "Ok" ;
					}
					else
					{
						//$Res["customer_detail_status"] = "Error";
						$error[] = "Error";
						
					}
				$sql_token = "INSERT INTO `perthdes_new_pro_api`.`customer_tokens` (`id`, `cid`, `access_token_value`, `status`, `created`, `modified`) VALUES (NULL, '".$cid."', '".$cust_token ."', '1', NOW(), NOW())";
							
							if ($conn->query($sql_token) === TRUE) 
							{
								//$Res["customer_token_id"] = $conn->insert_id;
								//$Res["customer_token_status"] = "OK";
								$success[] = "Ok" ;
							}
							else
							{
								//$Res["customer_token_status"] = "Error";
								$error[] = "Error";
							}
					}
			
			
			
    	if(!$error) {
	$Res["message"] = "Details Copied to HomeKey App"; 
	$Res["staus"] = "OK"; 
	curlObj = curl_init();
	curl_setopt($curlObj, CURLOPT_URL, 'https://hooks.slack.com/services/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');
	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'));
	curl_setopt($curlObj, CURLOPT_POST, 1);
	curl_setopt($curlObj, CURLOPT_POSTFIELDS, '{"text": "New Customer '.$fullname.' created from FamCab App"}');

	$response = curl_exec($curlObj);

	curl_close($curlObj);
	
	} else {
	$Res["message"] = "Error Copying Payment Details"; 
	$Res["staus"] = "error"; 
	}
		
	} else {
	$Res["message"] = "Error Copying Customer Details"; 
	$Res["staus"] = "error"; 
	error_log("add to customers error: ".$conn->error);
	}
	
	}

	
	
	
} 
else 
{
	$Res["message"] = "Error Accessing Details"; 
	$Res["staus"] = "error"; 
}
	
	
echo json_encode(new ArrayObject($Res)); 
/*echo "<pre>";
print_r($Res);
//print_r($response);
echo "</pre>";*/
$conn->close();
// =============== New customer user api End Here ==========================


?>