<?php

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");
}
mysqli_query($con, "SET time_zone = '-04:00'");
$function = $_REQUEST['f'];
if($function == "getSavedCarts"){
	getSavedCarts();
}elseif($function == "saveCart"){
   saveCart($_REQUEST['name']);
}elseif($function == "loadCart"){
	if(isset($_REQUEST['id'])){
		loadCart($_REQUEST['id'], $_REQUEST['copy']);	
	}
   
}elseif($function == "deleteCart"){
	if(isset($_REQUEST['id'])){
		deleteCart($_REQUEST['id']);
	}
}
elseif($function == "loadReorderToCart"){
	loadReorderToCart($_POST);
}elseif($function == "clearCart"){
	clearCart();
}
//var_dump($_SESSION);

function saveCart($name){
	global $con;
	$isCustomer = $_SESSION['isCustomer']?1:0;
	$isSalesman = $_SESSION['isCustomer']?0:1;
	$accountNumber = $_SESSION['isCustomer']?$_SESSION['AccountNumber']:$_SESSION['SalesmanID'];
	$query = "SELECT COUNT(*) ItemCount FROM SHOPPING_CART WHERE `AccountNumber` LIKE '$accountNumber'";
	$result = mysqli_query($con, $query);
	$row = mysqli_fetch_assoc($result);
	if($row['ItemCount'] > 0){
		if($isSalesman && $_SESSION['setCustomerMode']){
			$selectedCustomer = $_SESSION['setCustomer'];
		}else{
			$selectedCustomer = $_SESSION['AccountNumber'];
		}
		$query = "INSERT INTO SAVED_CART_HEAD (`AccountNumber`,`SelectedAccount`, `SavedCartDate`, `Name`, SalesmanCart) VALUES ('" . $accountNumber . "', '" . $selectedCustomer . "',NOW(),'$name'," . $isSalesman . ");";
		//echo $query;
		if(mysqli_query($con, $query)){
			$id = mysqli_insert_id($con);
			
			$query = "INSERT INTO SAVED_CART_DETAIL (`SavedCartID`, `Group`, `Line`, `SKU`, `Qty`) (SELECT $id, `Group`, `Line`, `SKU`, `Qty` FROM SHOPPING_CART WHERE AccountNumber LIKE '" . $accountNumber . "')";
			if(mysqli_query($con, $query)){
				$query = "DELETE FROM SHOPPING_CART WHERE AccountNumber LIKE '" . $accountNumber . "'";
				//echo $query;
				if(mysqli_query($con, $query)){
					if(!$isCustomer){
						ob_start();
						clearSetCustomer();
						ob_end_clean();
					}
					echo "true";
				}
			}else{
				echo mysqli_error($con) . "<br>" . $query;
			}
		}else{
			echo  mysqli_error($con) . "<br>" . $query;
		}	
	}
	
}
function getSavedCarts(){
	global $con;
	$isCustomer = $_SESSION['isCustomer']?1:0;
	$accountNumber = $_SESSION['isCustomer']?$_SESSION['AccountNumber']:$_SESSION['SalesmanID'];
	
	$query = "SELECT * FROM SAVED_CART_HEAD WHERE AccountNumber LIKE '$accountNumber'";
	$result = mysqli_query($con, $query);
	$carts = array();
	if(mysqli_num_rows($result)){
		while($row = mysqli_fetch_array($result)){
			$item = new stdClass();
			$item->id = $row['id'];
			$item->date = $row['SavedCartDate'];
			$item->name = $row['Name'];
			$item->customer = $row['SelectedAccount'];
			$item->accountnumber = $row['AccountNumber'];
			array_push($carts,$item);
		}
		
	}
	echo json_encode($carts);
	return;
}
function deleteCart($cartID){
	global $con;
	$accountNumber = $_SESSION['isCustomer']?$_SESSION['AccountNumber']:$_SESSION['SalesmanID'];
	$query = "DELETE FROM SAVED_CART_HEAD WHERE id = $cartID;\n";
	$query .= "DELETE FROM SAVED_CART_DETAIL WHERE SavedCartID = $cartID;";
	mysqli_multi_query($con,$query);
	if(mysqli_next_result($con)){
		echo "true";
		return;
	}else{
		echo "false";
		return;
	}
}
function clearCart(){
	global $con;
	$accountNumber = $_SESSION['isCustomer']?$_SESSION['AccountNumber']:$_SESSION['SalesmanID'];
	$query = "DELETE FROM SHOPPING_CART WHERE AccountNumber LIKE '" . $accountNumber . "'";
	if(mysqli_query($con,$query)){
		echo "true";
	}
	
}
function loadCart($cartID, $copy = false){
	global $con;
	$isCustomer = $_SESSION['isCustomer']?1:0;
	$accountNumber = $_SESSION['isCustomer']?$_SESSION['AccountNumber']:$_SESSION['SalesmanID'];
	$query = "DELETE FROM SHOPPING_CART WHERE AccountNumber LIKE '" . $accountNumber . "'";
	mysqli_query($con,$query);
	$query = "SELECT * FROM SAVED_CART_HEAD WHERE id = $cartID";
	$result = mysqli_query($con, $query);
	$row = mysqli_fetch_array($result);
	//var_dump($isCustomer);
	//var_dump(!$isCustomer && ($row['SelectedAccount'] != "" || $row['SelectedAccount'] != null));
	if(!$isCustomer && ($row['SelectedAccount'] != "" || $row['SelectedAccount'] != null)){
		ob_start();
		setCustomer($row['SelectedAccount']);
		ob_end_clean();
	}
	
	$query = "INSERT INTO SHOPPING_CART (AccountNumber, `Group`, `Line`, SKU, Qty, SalesmanCart) (SELECT '$accountNumber', `Group`, `Line`, SKU, Qty, $isCustomer FROM SAVED_CART_DETAIL WHERE SavedCartID = $cartID)";
	//echo $query;
	if($result = mysqli_query($con, $query)){
		$copy = $copy=="false"?false:true;
		
		if(!$copy){
			ob_start();
			deleteCart($cartID);
			ob_end_clean();	
		}
		
	}
	echo "true";
	
	
}
function loadReorderToCart($data){
	global $con;
	//var_dump($data);
	foreach($data as $item){
		$query = "INSERT INTO SHOPPING_CART (Line, `Group`, `SKU`, `Qty`, AccountNumber) VALUES ('" . $item['Line'] . "', '" . $item['Group'] . "', '"  . $item['SKU'] . "', " . $item['Qty'] .  ",'" . $_SESSION['SalesmanID'] . "');";
		mysqli_query($con, $query);
		
	}
	echo "true";
	
}
?>