<?php
require("lib/config.php");
require_once 'vendor/eway/eway-rapid-php/include_eway.php';
// =============== Get customer user api ===================================
$data  = $_REQUEST;
$code_value = "";
$sql = "SELECT * FROM customers where email = '".$_REQUEST['username']."' and password = '".MD5($_REQUEST['pass'])."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) 
{
    
    $Res = processLogin($result);
	
} 
else 
{
    if(isset($_REQUEST["google_id"]) && $_REQUEST["google_id"] != "") {
    $sql = "SELECT * FROM google_id where google_id = '".$_REQUEST["google_id"]."'";
    $result = $conn->query($sql);
    
        if ($result->num_rows > 0) 
        {
            $row = $result->fetch_assoc();
            $cid = $row["cid"];
            
            $sql = "SELECT * FROM customers where id = ".$cid;
            $result = $conn->query($sql);
            if ($result->num_rows > 0) 
            {
            
            $Res = processLogin($result);
            
            } else {
            
            $Res["message"] = "Google account not connected to an active account"; 
	        $Res["status"] = "missing_account"; 
            
            }
        	
        } else {
            
            $Res["message"] = "Google account not connected to an active account"; 
	        $Res["status"] = "missing_account"; 
            
        }
    
    
    } else if(isset($_REQUEST["facebook_id"]) && $_REQUEST["facebook_id"] != "") {
    $sql = "SELECT * FROM facebook_id where facebook_id = '".$_REQUEST['facebook_id']."'";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) 
        {
            $row = $result->fetch_assoc();
            $cid = $row["cid"];
            
            $sql = "SELECT * FROM customers where id = ".$cid;
            $result = $conn->query($sql);
            if ($result->num_rows > 0) 
            {
            
            $Res = processLogin($result);
            
            } else {
            
            $Res["message"] = "Facebook account not connected to an active account"; 
	        $Res["status"] = "missing_account"; 
            
            }
            
            
        	
        } else {
            
            $Res["message"] = "Facebook account not connected to an active account"; 
	        $Res["status"] = "missing_account"; 
            
        }
    
    } else {
    
    
	$Res["message"] = "Username or password not correct"; 
	$Res["status"] = "error"; 
	
    }
}
if($_REQUEST['firebase_token'] != ""){
	$sql = "UPDATE customers SET firebase_token='".$_REQUEST["firebase_token"]."' WHERE email = '".$_REQUEST['username']."'";
	$result = $conn->query($sql);
	}

echo json_encode(new ArrayObject($Res), true); 

$conn->close();

function processLogin($result) {
    
    global $conn;
    
    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 `promo_codes` where `code` = '".$row["promo_code"]."'"; 
		$result = $conn->query($sql);
			if($result->num_rows > 0) {
			$values = $result->fetch_assoc();
			$code_value = $values["value"];
			} else {
			$sql = "SELECT * FROM referrals where code = '".$row["promo_code"]."'";
				$result = $conn->query($sql);
				if ($result->num_rows > 0) {
					$row = $result->fetch_assoc();
					$code_value = $row['value'];
					}
			
			}
		
		$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;
			}
		}
    }
	$Res["message"] = "Login Successful"; 
	$Res["status"] = "OK"; 
	
	$sql = "SELECT * FROM `new_lang_lat`"; 
	$result = $conn->query($sql);
	if ($result->num_rows > 0) 
	{
		while($lang_lat = $result->fetch_assoc()) 
		{
			$lang_lati[] = $lang_lat;
		}
	}
	
	
	$sql = "SELECT * FROM `flag_val`"; 
	$result = $conn->query($sql);
	if ($result->num_rows > 0) 
	{
		while($flag_val = $result->fetch_assoc()) 
		{
			$flag_value[] = $flag_val;
		}
	}
	
	

	
	$sql = "SELECT * FROM `prices` WHERE id = 0";
	$result = $conn->query($sql);
	if ($result->num_rows > 0) 
	{
		while($prices = $result->fetch_assoc()) 
		{
			$HKbase = $prices['base'];
			$HKkm = $prices['km'];
			$HKlarge = $prices['large_km'];
			$HKtime = $prices['time'];
		}
	}
	$sql = "SELECT * FROM `prices` WHERE id = 1";
	$result = $conn->query($sql);
	if ($result->num_rows > 0) 
	{
		while($prices = $result->fetch_assoc()) 
		{
			$SIbase = $prices['base'];
			$SIkm = $prices['km'];
			$SIlarge = $prices['large_km'];
			$SItime = $prices['time'];
		}
	}
	$sql = "SELECT * FROM `prices`";
	$result = $conn->query($sql);
	if ($result->num_rows > 0) 
	{
		while($prices = $result->fetch_assoc()) 
		{
			$priceMatrix[] = $prices;
			
		}
	}
	
	if(isset($_REQUEST["all"]))
	{
		$Res["data"] = $customer;
		$Res["cust_detail"] = $cust_detail;
		$Res["cust_token"] = $cust_token;
	}
	else
	{
		$Res["data"]["name"] = $customer[0]["name"];
		$Res["data"]["mobile"] = $customer[0]["mobile"];
		$Res["data"]["email"] = $customer[0]["email"];
		$Res["data"]["address"] = $cust_detail[0]["address"];
		$Res["data"]["addr_lat"] = $cust_detail[0]["addr_lat"];
		$Res["data"]["addr_long"] = $cust_detail[0]["addr_long"];
		
		$Res["data"]["lang_lati"] = $lang_lati;
		$Res["data"]["flag_value"] = $flag_value;
		
		$Res["data"]["promo_code"] = $customer[0]["promo_code"];
		$Res["data"]["promo_value"] = $code_value;
		
		$Res["data"]["prices"] = $priceMatrix;
	
	}
	
	return $Res;
	
    
}


?>