<?php
include("../connect.php");
include("../session.php");

$isAdmin = $_SESSION['isAdmin'];
if (!$isAdmin) {
	header("Location: logout.php");
}



$function = $_REQUEST['f'];
if($function == "sendToShoppingCart"){
    $estimate = new Estimate($_REQUEST['id']);
	$estimate->sendToShoppingCart();
}elseif($function == "createOrder"){
	$estimate = new Estimate($_REQUEST['id']);
	$return = new stdClass();

	$return->OrderID = $estimate->generateOrder();
	if($return->OrderID){
		echo json_encode($return);
	}else{
		echo "false";
	}

}elseif($function == "deleteEstimate"){
	$estimate = new Estimate($_REQUEST['id']);
	$return = new stdClass();


	if($estimate->deleteEstimate()){
		$return->Result = true;
		echo json_encode($return);
	}else{
		$return->Result = false;
		echo json_encode($return);
	}

}elseif($function == "archiveEstimate"){
	$estimate = new Estimate($_REQUEST['id']);
	$return = new stdClass();


	if($estimate->archiveEstimate()){
		$return->Result = true;
		echo json_encode($return);
	}else{
		$return->Result = false;
		echo json_encode($return);
	}

}elseif($function == "getPendingEstimates"){

	getPendingEstimates();
}elseif($function == "getEstimateCustomerData"){
	$estimate = new Estimate($_REQUEST['eid']);
	echo json_encode($estimate);
}elseif($function == "sendEstimateEmail"){
	//ini_set("display_errors",1);
	//error_reporting(E_ALL);
	//include_once("/functions.php");
	sendEstimate($_REQUEST['eid']);
}else{
    die( "Invalid function");
}


function getPendingEstimates(){
	global $con;

	$query = "SELECT EstimateID FROM ESTIMATE_HEAD WHERE Processed = 0 AND Archive = 0 AND DeleteEstimate = 0 ORDER BY EstimateDate" ;
	$result = mysqli_query($con, $query);
	$return = [];
	while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

		$estimate = new Estimate($row['EstimateID']);
		$estimate->EstimateDate = date("m/d/Y h:i a", strtotime($estimate->EstimateDate));
		$estimate->ViewLink = $estimate->getViewLink();
		($estimate->Items = []);
		array_push($return, $estimate);
	}
	echo json_encode($return);
}



?>
