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

// =============== Get credit 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;
        $cid = $row["id"];
		
    }
    
    


	$sql = "SELECT * FROM `referal_claim` WHERE `customer_id` = '".$cid."'";
	$claims = $conn->query($sql);
	if($claims->num_rows >0) {
	$claimsRow = $claims->fetch_assoc();
	$ref_created = $claimsRow["referrals_generated"];
	$ref_claimed = $claimsRow["referrals_claimed"];
	$value = $claimsRow["value"];
	$credits_available = $ref_created - $ref_claimed;
	
	if($credits_available > 0) {
	
	
	$sql = "SELECT * FROM `credits` WHERE `customer_id` = '".$cid."'";
	$credit_exists = $conn->query($sql);
	if($credit_exists->num_rows > 0) {
		$rows = $credit_exists->fetch_assoc();
		$currentCredit = $rows["value"];
		$newCredit = $currentCredit + $credits_available * $value;
		
		$sql = "UPDATE `credits` SET `value` = '".$newCredit."' WHERE `customer_id` = '".$cid."'";
		$updateCredit = $conn->query($sql);
		if(!$updateCredit) {
			error_log("Error updating credit ".$conn->error);
			$Res["message"] = "Error updating credit"; 
			$Res["status"] = "error";
		} else {
		
		$sql = "UPDATE `referal_claim` SET `referrals_claimed` = '".$ref_created."' WHERE `customer_id` = '".$cid."'";
		$updateReferral = $conn->query($sql);
		if(!$updateReferral) {
			error_log("Error updating referral ".$conn->error);
			$Res["message"] = "Error updating credit"; 
			$Res["status"] = "error";
		
		} else {
		$Res["message"] = "Credit will be applied to next payment"; 
		$Res["status"] = "OK";	
	
	} 
	}
	} else {
	
	$sql = "INSERT INTO `credits` (`id`, `customer_id`, `value`, `date_created`, `reference`, `is_used`, `date_used`) VALUES (NULL, '".$cid."', '".$credits_available * $value."', NOW(), '', '0', NULL)";
	$insert = $conn->query($sql);
	if(!$insert) {
		$Res["message"] = "Error generating credit"; 
		$Res["status"] = "error";
		error_log("Error inserting credit ".$conn->error);
	} else {
	
		$sql = "UPDATE `referal_claim` SET `referrals_claimed` = '".$ref_created."' WHERE `customer_id` = '".$cid."'";
		$updateReferral = $conn->query($sql);
		if(!$updateReferral) {
			error_log("Error updating referral ".$conn->error);
			$Res["message"] = "Error updating credit"; 
			$Res["status"] = "error";
		
		} else {
		$Res["message"] = "Credit will be applied to next payment"; 
		$Res["status"] = "OK";
	
		
		} 
		
	}
	
	}
	} else {
	
	$Res["message"] = "No Credits available"; 
	$Res["status"] = "no";
	
	}
	
	
	
	
	
	} else {
	
	$Res["message"] = "No Referrals Found"; 
	$Res["status"] = "no";
	
	
	}
	
	
	
	
	
	

} else {

   
  
	 
	
	
	
	
$Res["message"] = "Error finding customer"; 
$Res["status"] = "error"; 
error_log("add to customers error: ".$conn->error);
}

	

	
	
	

	
	
echo json_encode(new ArrayObject($Res)); 
/*echo "<pre>";
print_r($Res);
//print_r($response);
echo "</pre>";*/
$conn->close();



?>
