<?php
//echo "lol";
//die();
if(stripos($_SERVER['SCRIPT_NAME'],"/admin/")===false?false:true){
	include_once("../connect.php");
	include_once("../session.php");
}else{
	include_once("connect.php");
	include_once("session.php");
}

$function = $_REQUEST['f'];
if($function == "getDeliveryOptionData"){
    $id = $_REQUEST['id'];
	
	getDeliveryOptionData($id);
}elseif($function == "saveDeliveryOptionData"){
   saveDeliveryOptionData($_REQUEST);
}elseif($function == "deleteDeliveryOption"){
	deleteDeliveryOption($_REQUEST['id']);
}elseif($function == "getDeliveryZoneData"){
	getDeliveryZoneData($_REQUEST['zone']);
}elseif($function == "saveDeliveryZoneData"){
	saveDeliveryZoneData($_REQUEST);
}
function getDeliveryOptionData($id){
    global $con;
	$query = "SELECT * FROM DELIVERY_OPTION WHERE id = $id";
	
	$result = mysqli_query($con, $query);
	//var_dump($result);
	if($result){
		//echo $query;
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		
		
		echo json_encode($row);
	}else{
		echo "false";
		
	}
}
function saveDeliveryOptionData($data){
	global $con;
	$deliveredBy = $data['DeliveredBy'];
	$deliveredByColumn = "";
	if($deliveredBy !== ""){
		$deliveredByColumn = "`DeliveredBy`,";
		$deliveredBy = "'" . $deliveredBy . "',";
	}
	$query = "INSERT INTO DELIVERY_OPTION (`id`, `Name`, `StartTime`, `Cutoff`, `Comment`, $deliveredByColumn `FeeRule`) VALUES (" . $data['id'] . ", '" . $data['Name'] . "', '" . $data['StartTime'] . "', '" . $data['Cutoff'] . "', '" . $data['Comment'] . "', $deliveredBy '" . $data['FeeRule'] . "')
			ON DUPLICATE KEY UPDATE `Name` = VALUES(`Name`), `StartTime` = VALUES(StartTime), `Cutoff` = VALUES(Cutoff), `Comment` = VALUES(Comment), `DeliveredBy` = VALUES(DeliveredBy), `FeeRule` = VALUES(FeeRule)";
	//echo $query;
	if(mysqli_query($con, $query)){
		$return['result'] = true;
	}else{
		$return['result'] = false;
		$return['error'] = mysqli_error($con);
		$return['query']= $query;
	}
	echo json_encode($return);
	return $return;
	
}
function deleteDeliveryOption($id){
	global $con;
	$query = "DELETE FROM DELIVERY_OPTION WHERE id = " . $id . ";";
	if(mysqli_query($con, $query)){
		echo "true";
	}else{
		echo mysqli_error($con);
	}
}
function getDeliveryZoneData($zone){
    global $con;
	$query = "SELECT DeliveryID FROM DELIVERY_ZONE WHERE Zone = $zone";
	$result = mysqli_query($con, $query);
	$return['Cities'] = array();
	$return['Routes'] = array();
	while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){		
		array_push($return['Routes'], $row['DeliveryID']);
	}
	$query = "SELECT City FROM DELIVERY_ZONE_CITY WHERE Zone = $zone";
	$result = mysqli_query($con, $query);
	while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){		
		array_push($return['Cities'], $row['City']);
	}
	
	echo json_encode($return);
	
}
function saveDeliveryZoneData($data){
	if($data['ZoneID'] == "" || $data['ZoneID'] == null){
		die("Invalid Zone");
	}
	global $con;
	ob_start();
	deleteDeliveryZone($data['ZoneID']);
	ob_end_clean();
	$result = true;
	$return = array();
	foreach($data['Routes'] as $key => $value){
		$query = "INSERT INTO DELIVERY_ZONE (`DeliveryID`, `Zone`) VALUES (" . $value . ", " . $data['ZoneID'] . ")";
		$result = $result && mysqli_query($con, $query);
	}
	foreach($data['Cities'] as $key => $value){
		$query = "INSERT INTO DELIVERY_ZONE_CITY (`City`, `Zone`) VALUES ('" . $value . "', " . $data['ZoneID'] . ")";
		$result = $result && mysqli_query($con, $query);
	}
	$return['result'] = $result;
	if($result){
		
		echo json_encode($return);
	}
	
}
function deleteDeliveryZone($zone){
	global $con;
	$query = "DELETE FROM DELIVERY_ZONE WHERE Zone = " . $zone . ";";
	$query2 = "DELETE FROM DELIVERY_ZONE_CITY WHERE Zone = " . $zone . ";";
	if(mysqli_query($con, $query) && mysqli_query($con, $query2)){
		echo "true";
	}else{
		echo mysqli_error($con);
	}
}

?>