<?php
require_once(__DIR__."/../includes/sysinit.php");

class Service
{
	// Properties of the class:
	var $hddnServiceId, $selAreaId, $edtServiceName, $edtServiceDuration, $edtServicePrice;
	function __construct(){}
	function __destruct(){}

	function addService()
	{
		if (empty($this->hddnServiceId))
		{
			if ($this->checkServiceExists())
			SableSystem::ajaxResponse(false, "", "This Service has already been added.", 
				5, "edtServiceName", false, "edtServiceName");
			$this->loadService();
		}
		else
		{
			$this->updateService();
		}
	}

	function loadService()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO service (
						service_name, service_duration, service_price, user_code, service_ip, service_date, last_update
						) VALUES (
						'".addslashes($this->edtServiceName)."',
						'".$this->edtServiceDuration."',
						'".$this->edtServicePrice."',
						'".$_SESSION['user']['user_code']."',
						'".SableGeneralFunctions::getRealIpAddr()."',
						'".date('Y-m-d H:i:s')."',
						'".date('Y-m-d H:i:s')."')";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the Service details were successfully added:
		if ($res)
		{
			$this->setServiceId();
			if ($this->storeAreas())
			{
				SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New Service added: '.$this->edtServiceName, $this->hddnServiceId);
				SableSystem::ajaxResponse(true, '/service/', 'The Service was successfully added.', 0, "", 0, "");
			}
			else 
				SableSystem::ajaxResponse(false, "", "Add of the Service Areas failed. Please try again or contact support.", 
				5, "selAreaId", false, "selAreaId");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Service failed. Please try again or contact support.", 
				5, "edtServiceName", false, "edtServiceName");
	}

	function updateService()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnServiceId);
		$updateQuery = "UPDATE service set 
						service_name = '".addslashes($this->edtServiceName)."', 
						service_duration = '".$this->edtServiceDuration."', 
						service_price = '".$this->edtServicePrice."', 
						user_code = '".$_SESSION['user']['user_code']."', 
						service_ip = '".SableGeneralFunctions::getRealIpAddr()."', 
						service_date = '".date('Y-m-d H:i:s')."', 
						last_update = '".date('Y-m-d H:i:s')."' 
						where service_id = '".$code."'";
		$res=$sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			if ($this->storeAreas())
			{
				SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'Service details modified: '.$this->edtServiceName, $code);
				SableSystem::ajaxResponse(true, '/service/', 'The Service was successfully updated.', 0, "", 0, "");
			}
			else 
				SableSystem::ajaxResponse(false, "", "Modification of the Service areas failed. Please try again or contact support.", 
				5, "selAreaId", false, "selAreaId");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Service failed. Please try again or contact support.", 
				5, "edtServiceName", false, "edtServiceName");
	}

	private function getServiceId()
	{
		/**

		**/
		global $sableDB;
		$sql = "select service_id 
				from service 
				where	(service_name = '".addslashes($this->edtServiceName)."') and 
						(service_duration = '".$this->edtServiceDuration."') and 
						(service_price = '".$this->edtServicePrice."')";
		return $sableDB->getValue($sql);
	}

	private function checkServiceExists()
	{
		$cid = $this->getServiceId();
		if (!empty($cid)) return true; else return false;
	}

	private function setServiceId()
	{
		$this->hddnServiceId = $this->getServiceId();
	}

	function viewService()
	{
		$url = '/service/view.php?'.SableSystem::sableDecode($this->hddnServiceId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editService()
	{
		$url = '/service/edit.php?'.SableSystem::sableDecode($this->hddnServiceId);
		SableSystem::ajaxPageRedirect($url);
	}
	
	function copyService()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnServiceId);
		$newId = $sableDB->duplicateRow("service", "service_id", $code);
		if ($newId)
		{
			//now copy all of the areas to the new service:
			$rows = $sableDB->getRows('select area_id from service__area where service_id = '.$code);
			$this->hddnServiceId = $newId;
			$this->areaList = [];
			$tmp = [];
			foreach($rows as $row) 
			{
				$tmp = [];
				$tmp['value'] = $row['area_id'];
				$this->areaList[] = $tmp;
			}
			$this->storeAreas();
			
			SableDBFunctions::loadAuditDetail($_SESSION["user"]["user_code"], "service record duplicated.", $code);
			SableSystem::ajaxPageRedirect("/service/", "The service record was successfully duplicated.");
		}
		else
			SableSystem::ajaxResponse(false, "", "System error: The service failed to duplicate. Please try again later or contact support.", 
										5, "edtServiceName", false, "edtServiceName");
	}

	function deleteService()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnServiceId);
		$rec = $sableDB->getRow("select service_name from service where service_id = ".$code);
		if ($sableDB->recordCount <= 0)
			SableSystem::ajaxResponse(false, "", "Delete failed - invalid Service selected.", 
				5, "edtServiceName", false, "edtServiceName");
		else
		{
			$sql = "delete from service where service_id = '".$code."'";
			$res = $sableDB->updateQuery($sql);
			if ($res)
			{				
				SableDBFunctions::loadAuditDetail($_SESSION["user"]["user_code"], 'Service record "'.$rec['service_name'].'" deleted.', $code);
				
				//delete the area list associated with this service:
				$sql = "delete from service__area where service_id = '".$code."'";
				$res = $sableDB->updateQuery($sql);
				if ($res) 
					SableDBFunctions::loadAuditDetail($_SESSION["user"]["user_code"], 'Areas disassociated from deleted service "'.$rec['service_name'].'".', $code);
				
				SableSystem::ajaxPageRedirect('/service/','"'.$rec['service_name'].'" was successfully deleted.');
			}
			else
			SableSystem::ajaxResponse(false, "", "
			System error: The Service failed to delete. Please try again later or contact support.", 
				5, "edtServiceName", false, "edtServiceName");
		}
	}
	
	private function storeAreas()
	{
		return SableDBFunctions::storeArray('service__area', 'service_id', $this->hddnServiceId, 'area_id', $this->areaList);
	}	

} //classService
?>
