<?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']."' and password = '".MD5($_REQUEST['pass'])."'";
$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 `promo_codes` where `code` = '".$row["promo_code"]."'"; 
		$result = $conn->query($sql);
			$values = $result->fetch_assoc();
			$code_value = $values["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["staus"] = "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 array FROM `homekey_lat_lng` WHERE `id`=1";
	$result = $conn->query($sql);
	if ($result->num_rows > 0) 
	{
		while($homekey_lang_lat = $result->fetch_assoc()) 
		{
			$homekey_lang_lati = $homekey_lang_lat['array'];
		}
	}
	$sql = "SELECT array FROM `homekey_lat_lng` WHERE `id`=2";
	$result = $conn->query($sql);
	if ($result->num_rows > 0) 
	{
		while($zone1 = $result->fetch_assoc()) 
		{
			$homekey_zone1 = $zone1['array'];
		}
	}
	$sql = "SELECT array FROM `homekey_lat_lng` WHERE `id`=3";
	$result = $conn->query($sql);
	if ($result->num_rows > 0) 
	{
		while($zone2 = $result->fetch_assoc()) 
		{
			$homekey_zone2 = $zone2['array'];
		}
	}
	
	$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 `homekey_hours`";
	$result = $conn->query($sql);
	if ($result->num_rows > 0) 
	{
		while($hours = $result->fetch_assoc()) 
		{
			$start_time = $hours['time_start'];
			$end_time = $hours['time_end'];
		}
	}
	$sql = "SELECT * FROM `terminal_details`";
	$result = $conn->query($sql);
	if ($result->num_rows > 0) 
	{
		while($terminals[] = $result->fetch_assoc()) {
		
		}
		
	}
	$sql = "SELECT `lead_time` FROM `lead_time_poly` WHERE zone = 1";
	$result = $conn->query($sql);
	if ($result->num_rows > 0) 
	{
		while($leadtimes = $result->fetch_assoc()) 
		{
			$zone1 = $leadtimes['lead_time'];
		}
	}
	$sql = "SELECT `lead_time` FROM `lead_time_poly` WHERE zone = 2";
	$result = $conn->query($sql);
	if ($result->num_rows > 0) 
	{
		while($leadtimes = $result->fetch_assoc()) 
		{
			$zone2 = $leadtimes['lead_time'];
		}
	}$sql = "SELECT `lead_time` FROM `lead_time_poly` WHERE zone = 3";
	$result = $conn->query($sql);
	if ($result->num_rows > 0) 
	{
		while($leadtimes = $result->fetch_assoc()) 
		{
			$zone3 = $leadtimes['lead_time'];
		}
	}
	$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'];
		}
	}
	
	
	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"]["no_pax"] = $cust_detail[0]["no_pax"];
		$Res["data"]["no_rf"] = $cust_detail[0]["no_rf"];
		$Res["data"]["no_ff"] = $cust_detail[0]["no_ff"];
		$Res["data"]["no_b"] = $cust_detail[0]["no_b"];
		$Res["data"]["lang_lati"] = $lang_lati;
		$Res["data"]["flag_value"] = $flag_value;
		$Res["data"]["homekey_lat_lng"] = $homekey_lang_lati;
		$Res["data"]["homekey_zone1"] = $homekey_zone1;
		$Res["data"]["homekey_zone2"] = $homekey_zone2;
		$Res["data"]["homekey_start"] = $start_time;
		$Res["data"]["homekey_end"] = $end_time;
		$Res["data"]["zone_1"] = $zone1;
		$Res["data"]["zone_2"] = $zone2;
		$Res["data"]["zone_3"] = $zone3;
		$Res["data"]["promo_code"] = $customer[0]["promo_code"];
		$Res["data"]["promo_value"] = $code_value;
		$Res["data"]["price_base"] = $HKbase;
		$Res["data"]["price_km"] = $HKkm;
		$Res["data"]["price_large"] = $HKlarge;
		$Res["data"]["price_time"] = $HKtime;
		$Res["data"]["std_base"] = $SIbase;
		$Res["data"]["std_km"] = $SIkm;
		$Res["data"]["std_large"] = $SIlarge;
		$Res["data"]["std_time"] = $SItime;
		$Res["data"]["t1_txt"] = $terminals[0]["name"];
		$Res["data"]["t2_txt"] = $terminals[1]["name"];
		$Res["data"]["t3_txt"] = $terminals[2]["name"];
		$Res["data"]["t1_lat"] = $terminals[0]["lat"];
		$Res["data"]["t2_lat"] = $terminals[1]["lat"];
		$Res["data"]["t3_lat"] = $terminals[2]["lat"];
		$Res["data"]["t1_lon"] = $terminals[0]["long"];
		$Res["data"]["t2_lon"] = $terminals[1]["long"];
		$Res["data"]["t3_lon"] = $terminals[2]["long"];
		
		
	}
	
	
	
} 
else 
{
	$Res["message"] = "Username or password not correct"; 
	$Res["staus"] = "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)); 
//echo "<pre>";
//print_r($Res);
//print_r($response);
//echo "</pre>";
$conn->close();
// =============== New customer user api End Here ==========================


?>