<?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['broker_broker'])){
$broker			= isset($_POST['broker_broker'])	? str_replace('_',' ',htmlentities(addslashes(strip_tags(trim($_POST['broker_broker']))))) : "";  
$contact		= isset($_POST['broker_contact'])	? htmlentities(addslashes(strip_tags(trim($_POST['broker_contact'])))) : ""; 
$email			= isset($_POST['broker_email'])		? htmlentities(addslashes(strip_tags(trim($_POST['broker_email'])))) : ""; 
$address		= isset($_POST['broker_address'])	? htmlentities(addslashes(strip_tags(trim($_POST['broker_address'])))) : "";   
$contact_person	= isset($_POST['broker_contact_person'])	? htmlentities(addslashes(strip_tags(trim($_POST['broker_contact_person'])))) : "";   
$landline		= isset($_POST['broker_landline'])	? htmlentities(addslashes(strip_tags(trim($_POST['broker_landline'])))) : "";   
$coordinator	= isset($_POST['broker_coordinator'])	? htmlentities(addslashes(strip_tags(trim($_POST['broker_coordinator'])))) : "";   
$prcno			= isset($_POST['broker_prc_no'])	? htmlentities(addslashes(strip_tags(trim($_POST['broker_prc_no'])))) : "";   
$accredit		= isset($_POST['broker_date_accredited'])	? strip_tags(trim($_POST['broker_date_accredited'])) : "";   


$task		= isset($_POST['action'])		? trim($_POST['action']) : "";
$mainid		= isset($_POST['mainid'])		? trim($_POST['mainid']) : "";

$currentUser 	= $_SESSION['danvillee_username'];
$timeStamp		= date('Y-m-d H:i:s');
$mainArr		= array();
$output 		= "";
$mainArr['Broker']				=	$broker;
$mainArr['ContactNo']			=	$contact;
$mainArr['EmailAddress']		=	$email;
$mainArr['Address']				=	$address; 
$mainArr['ContactPerson']		=	$contact_person; 
$mainArr['Landline']			=	$landline; 
$mainArr['Coordinator']			=	$coordinator; 
$mainArr['BrokerPRCId']			=	$prcno; 
$mainArr['DateAccredited']		=	date("Y-m-d", strtotime($accredit));

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

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