<?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['code'])){

$code			= isset($_POST['code'])			? str_replace('_',' ',htmlentities(addslashes(strip_tags(trim($_POST['code']))))) 		: ""; 
$fullname		= isset($_POST['fullname'])		? str_replace('_',' ',htmlentities(addslashes(strip_tags(trim($_POST['fullname']))))) 	: "";  
//$firstname		= isset($_POST['firstname'])	? str_replace('_',' ',htmlentities(addslashes(strip_tags(trim($_POST['firstname']))))) 	: "";  
//$middlename		= isset($_POST['middlename'])	? str_replace('_',' ',htmlentities(addslashes(strip_tags(trim($_POST['middlename']))))) : "";  
$realty			= isset($_POST['realty'])		? str_replace('_',' ',htmlentities(addslashes(strip_tags(trim($_POST['realty']))))) 	: "";  
 
$dateapplicationreceived 	= isset($_POST['dateapplicationreceived'])	? strip_tags(trim($_POST['dateapplicationreceived'])) : "";   
$dateappliedatmetrobank		= isset($_POST['dateappliedatmetrobank'])	? strip_tags(trim($_POST['dateappliedatmetrobank'])) : "";   
 
$brokeragent		= isset($_POST['brokeragent'])		? strip_tags(trim($_POST['brokeragent'])) : "";    
$cashcardnumber		= isset($_POST['cashcardnumber'])	? $_POST['cashcardnumber'] : "";   
$accountnumber		= isset($_POST['accountnumber'])	? $_POST['accountnumber'] : "";   
$remarks			= isset($_POST['remarks'])			? strip_tags(trim($_POST['remarks'])) : "";   
 
$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();
$output 		= "";

$mainArr['Code']						=	$code;
$mainArr['Fullname']					=	$fullname;
//$mainArr['FirstName']					=	$firstname;
//$mainArr['MiddleName']					=	$middlename; 
$mainArr['Realty']						=	$realty; 
$mainArr['BrokerOrAgent']				=	$brokeragent; 
$mainArr['DateApplicationReceived']		=	date("Y-m-d", strtotime($dateapplicationreceived));
$mainArr['DateAppliedAtMetrobank']		=	date("Y-m-d", strtotime($dateappliedatmetrobank)); 
$mainArr['CashCardNumber']				=	$cashcardnumber; 
$mainArr['AccountNumber']				=	$accountnumber;
$mainArr['Remarks']						=	$remarks; 
 

$condArr = array();
if($task=="create"):
	
	$brokerAgentId	 			= $mecon->getNextId("Id","brokeragent"); 
	$mainArr['Id']				= $brokerAgentId;
	$mainArr['CreatedDate'] 	= $timeStamp;
	$mainArr['CreatedBy'] 		= $currentUser;
	$output						= $mecon->insertSingleQuery($mainArr,"brokeragent"); 	 
	
			//LOG TASK 
			$actArr['UserType']			= "documentation_staff,documentation_supervisor";
			$actArr['Remarks']			= "Created New Broker/Agent : $code";
			$actArr['Action']			= "create";
			$actArr['ActionType']		= "brokeragent";
			$actArr['ActionContent']	= json_encode($mainArr);
			$actArr['ActionBy'] 		= $currentUser;	
			$actArr['ActionDate']		= $timeStamp;
			$actArr['TransId']			= $brokerAgentId;
			$utility->encodeActivityLog($actArr);

elseif($task=="edit"):
	$mainArr['UpdatedDate'] 		= $timeStamp;
	$mainArr['UpdatedBy'] 			= $currentUser;
	$condArr['Id']					= $mainid;
	$output 						= $mecon->updateQuery($mainArr,"brokeragent",$condArr); 
	
	//LOG TASK 
			$actArr['UserType']			= "documentation_staff,documentation_supervisor";
			$actArr['Remarks']			= "Updated Broker/Agent : $code";
			$actArr['Action']			= "edit";
			$actArr['ActionType']		= "brokeragent";
			$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();