<?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(); 
 
$task		= isset($_POST['action'])		? trim($_POST['action']) : "";
$mainid		= isset($_POST['mainid'])		? trim($_POST['mainid']) : "";
$block		= isset($_POST['block'])		? trim($_POST['block']) : "";
$lot		= isset($_POST['lot'])			? trim($_POST['lot']) : "";

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

$mainArr	= array();  
$output 	= ""; 
$condArr = array();

if($task=="edit"):

	$existUnit = $utility->checkExistField('UnitId','construction_and_utilities','unitid',$mainid);
	if($existUnit===false){
		//insert 
			$mainArr['UnitId'] 					= $mainid;
			$mainArr['Block'] 					= $block;
			$mainArr['Lot'] 					= $lot;
			$mainArr['Road'] 					= isset($_POST['road'])				? trim($_POST['road']) : "no";
			$mainArr['SideWalkGutter']  		= isset($_POST['side_walk'])		? trim($_POST['side_walk']) : "no";
			$mainArr['Meralco'] 				= isset($_POST['meralco'])			? trim($_POST['meralco']) : "no";
			$mainArr['Water'] 					= isset($_POST['water'])			? trim($_POST['water']) : "no";
			$mainArr['HouseConstruction']		 = isset($_POST['house_construction'])			? trim($_POST['house_construction']) : "no";
			$mainArr['HouseConstructionPercentage'] = isset($_POST['house_construction_percentage'])			? trim($_POST['house_construction_percentage']) : "";
			$mainArr['CreatedDate'] 	= $timeStamp;
			$mainArr['CreatedBy'] 		= $currentUser; 
			$output 					= $mecon->insertSingleQuery($mainArr,"construction_and_utilities"); 

	}else{
		//unit exist update record
			$mainArr['Block'] 					= $block;
			$mainArr['Lot'] 					= $lot;
			$mainArr['Road'] 			= isset($_POST['road'])				? trim($_POST['road']) : "no";
			$mainArr['SideWalkGutter']  = isset($_POST['side_walk'])		? trim($_POST['side_walk']) : "no";
			$mainArr['Meralco'] 		= isset($_POST['meralco'])			? trim($_POST['meralco']) : "no";
			$mainArr['Water'] 			= isset($_POST['water'])			? trim($_POST['water']) : "no";
			$mainArr['HouseConstruction'] = isset($_POST['house_construction'])			? trim($_POST['house_construction']) : "no";
			$mainArr['HouseConstructionPercentage'] = isset($_POST['house_construction_percentage']) ? trim($_POST['house_construction_percentage']) : "";
			$mainArr['UpdatedDate'] 	= $timeStamp;
			$mainArr['UpdatedBy'] 		= $currentUser;
			$condArr['UnitId']			= $mainid;
			$output 					= $mecon->updateQuery($mainArr,"construction_and_utilities",$condArr);

	} 

  
			//LOG TASK 
			$actArr['UserType']			= "liaison";
			$actArr['Remarks']			= "Updated Construction and Utilities : $block - $lot";
			$actArr['Action']			= "edit";
			$actArr['ActionType']		= "construction_and_utilities";
			$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();