<?php
error_reporting(0);
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();
session_start();
session_cache_limiter('private');
$cache_limiter 	= session_cache_limiter();
session_cache_expire(60);
$cache_expire 	= session_cache_expire(); 
date_default_timezone_set('Asia/Manila'); 
$CURRENT_DATE 	= date('Y-m-d H:i:s');
include_once('../config/minho.php');
include_once('../config/autoloadClass.php');
$autoload	=	new autoloadClass();
$mecon		=	new condbClass();
$utility	=	new utilityClass(); 
if(isset($_POST['UnitBlock'])){
$block		= isset($_POST['UnitBlock']) ? str_replace('_',' ',htmlentities(addslashes(strip_tags(trim($_POST['UnitBlock']))))) : "";   
$lot		= isset($_POST['UnitLot'])   ? str_replace('_',' ',htmlentities(addslashes(strip_tags(trim($_POST['UnitLot'])))))   : "";   
$task		= isset($_POST['action'])	 ? trim($_POST['action']) : "";
$mainid		= isset($_POST['mainid'])	 ? trim($_POST['mainid']) : "";

$currentUser 				= 	$_SESSION['castalia_username'];
$timeStamp					= 	date('Y-m-d H:i:s');

$mainArr					= 	array();  
$mainUArr					= 	array();  
$output 					= 	""; 
$blockArr  					= 	array();  
$lotArr  					= 	array();  


$condArr3  					= 	array();

if($task=="edit"):
	//delete existing and reinsert again for the block and lots under the unit group
	$condArr3['UnitId']	= $mainid;
	$output = $mecon->deleteQuery('units_block_lot',$condArr3);
endif;


		 $number = count($_POST["UnitBlock"]);  
		 if($number > 0)  
		 {  
		      for($i=0; $i<$number; $i++)  
		      {    
		            $mainUArr['Id']			= $mecon->getNextId("Id","units_block_lot");
				if($task=="edit"):	
					$unitsId				= $mainid; 
				else:	
					$unitsId				= $mecon->getNextId("Id","units"); 
				endif;	

					if(!empty($_POST["UnitBlock"][$i]) && !empty($_POST["UnitLot"][$i])){
						$mainUArr['UnitId'] 	= $unitsId;
					    $mainUArr['Block']		= $_POST["UnitBlock"][$i];
					    $mainUArr['Lot'] 		= $_POST["UnitLot"][$i];
					    $output					= $mecon->insertSingleQuery($mainUArr,"units_block_lot"); 	 
					    $blockArr[] 			= $_POST["UnitBlock"][$i];
					    $lotArr[] 				= $_POST["UnitLot"][$i];	
					}
				    
		      }   
		 }    
 
		
		sort($blockArr); 
		sort($lotArr);  
		$mainArr['Block']				=	implode(',',array_unique($blockArr));
		$mainArr['Lot']					=	implode(',',array_unique($lotArr));


$condArr = array();
if($task=="create"):
	$mainArr['Id']				= $unitsId; 
	$mainArr['CreatedDate'] 	= $timeStamp;
	$mainArr['CreatedBy'] 		= $currentUser;
	$output						= $mecon->insertSingleQuery($mainArr,"units"); 	 
	
			//LOG TASK 
			$actArr['UserType']			= "liaison";
			$actArr['Remarks']			= "Created New Units : $block - $lot";
			$actArr['Action']			= "create";
			$actArr['ActionType']		= "units";
			$actArr['ActionContent']	= json_encode($mainArr);
			$actArr['ActionBy'] 		= $currentUser;	
			$actArr['ActionDate']		= $timeStamp;
			$actArr['TransId']			= $unitsId;
			$utility->encodeActivityLog($actArr);
elseif($task=="edit"):
	$mainArr['UpdatedDate'] 		= $timeStamp;
	$mainArr['UpdatedBy'] 			= $currentUser;
	$condArr['Id']					= $mainid;
	$output 						= $mecon->updateQuery($mainArr,"units",$condArr); 
	
			//LOG TASK 
			$actArr['UserType']			= "liaison";
			$actArr['Remarks']			= "Updated Units : $block - $lot";
			$actArr['Action']			= "edit";
			$actArr['ActionType']		= "units";
			$actArr['ActionContent']	= json_encode($mainArr);
			$actArr['ActionBy'] 		= $currentUser;	
			$actArr['ActionDate']		= $timeStamp;
			$actArr['TransId']			= $mainid;
			$utility->encodeActivityLog($actArr);
endif;

$resultArr 						= array();
$resultArr['output']	  		= $output;
echo json_encode($resultArr);
}
exit();
ob_end_flush();