<? error_reporting(0);
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();
session_start();  
include_once('../config/minho.php');
include_once('../config/autoloadClass.php');
$autoload		=	new autoloadClass(); 
$mecon			=	new condbClass();
$requestData	=	$_REQUEST; 
$columns = array(
	0	=>	'Id',
	//1	=>	'Id',
	1	=>	'Code', 
	2	=>	'Fullname', 
	3	=>	'Realty',
	4	=>	'DateApplicationReceived',
	5	=>	'DateAppliedAtMetrobank',
	6	=>	'BrokerOrAgent',
	7	=>	'CashCardNumber',
	8	=>	'AccountNumber',
	9	=>	'Remarks' 
);  

$mainQuery 		= "SELECT Id, Code, Fullname, Realty, DateApplicationReceived, DateAppliedAtMetrobank, BrokerOrAgent, CashCardNumber, AccountNumber, Remarks, Status FROM brokeragent ";
$sql			=	$mainQuery."  WHERE Status = 'active' ";
if(!empty($requestData['search']['value']) ) { 
	$arr = explode(" ",$requestData['search']['value']);  
	foreach($arr as $ind){
		$ind = str_replace(',','',$ind);
		$sql.=" AND (Code LIKE '%".htmlentities($ind)."%' "; 
		$sql.=" OR Fullname LIKE '%".$ind."%' ";  
		$sql.=" OR Realty LIKE '%".$ind."%' ";    
		$sql.=" OR UPPER(DATE_FORMAT(DateApplicationReceived,'%m/%d/%Y')) LIKE '".$ind."%' "; 
		$sql.=" OR UPPER(DATE_FORMAT(DateAppliedAtMetrobank,'%m/%d/%Y')) LIKE '".$ind."%' "; 
		$sql.=" OR CashCardNumber LIKE '%".$ind."%' ";    
		$sql.=" OR AccountNumber LIKE '%".$ind."%' ";    
		$sql.=" OR Remarks LIKE '%".$ind."%' ";    
		$sql.=" OR BrokerOrAgent  LIKE '%".htmlentities($ind)."%' )";
	}
}


$mecon->query($sql);
$result			= $mecon->resultset(); 
$totalData		= count($result);
$totalFiltered	=	$totalData;
  
$sql.=" ORDER BY ".$columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']."   "; 


$mecon->query($sql);
$data = array();
$result = $mecon->resultset();

			 for($a=0; $a<count($result); $a++){
			 $nestedData=array();
				$nestedData[]	= '<a href="index.php?pg=settings&type=brokeragent&action=edit&id='.$result[$a]['Id'].'"><span class="btn btn-xs btn-primary glyphicon glyphicon-pencil" title="Update Broker Agent"></span></a>&nbsp;<span class="btn btn-xs btn-danger glyphicon glyphicon-trash" title="Delete Broker Agent" onclick="deleteTransaction('.$result[$a]['Id'].')" ></span>'; 
				//$nestedData[]	= $a+1;   
				$nestedData[] = '<a style="text-align:left" href="index.php?pg=settings&type=brokeragent&action=view&id='.$result[$a]['Id'].'">'.strtoupper(trim(html_entity_decode(stripslashes($result[$a]['Code'])))).'</a>';   
				$nestedData[] = (!empty($result[$a]['Fullname'])) 		? strtoupper(html_entity_decode(str_replace('&NTILDE;','&Ntilde;',$result[$a]['Fullname']))) : ""; 
				$nestedData[] = (!empty($result[$a]['Realty'])) 		? strtoupper(html_entity_decode(str_replace('&NTILDE;','&Ntilde;',$result[$a]['Realty']))) : ""; 
				$nestedData[] =  (($result[$a]['DateApplicationReceived']=='0000-00-00') || ($result[$a]['DateApplicationReceived']=='1970-01-01') || ($result[$a]['DateApplicationReceived']=='')) ? "" : date('m/d/Y', strtotime($result[$a]['DateApplicationReceived'])); 
				$nestedData[] =  (($result[$a]['DateAppliedAtMetrobank']=='0000-00-00') || ($result[$a]['DateAppliedAtMetrobank']=='1970-01-01') || ($result[$a]['DateAppliedAtMetrobank']=='')) ? "" : date('m/d/Y', strtotime($result[$a]['DateAppliedAtMetrobank']));  
				$nestedData[] = (!empty($result[$a]['BrokerOrAgent'])) 		? ucfirst($result[$a]['BrokerOrAgent']) : ""; 
				$nestedData[] = (!empty($result[$a]['CashCardNumber']))		? $result[$a]['CashCardNumber'] : ""; 
				$nestedData[] = (!empty($result[$a]['AccountNumber'])) 		? $result[$a]['AccountNumber'] : ""; 
				$nestedData[] = (!empty($result[$a]['Remarks'])) 			? $result[$a]['Remarks'] : ""; 
 

				$data[] = $nestedData;
			} 
$json_data = array(
			"draw"            => intval( $requestData['draw'] ),
			"recordsTotal"    => intval( $totalData ),
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data
			);
			 
echo json_encode($json_data);
exit();
ob_end_flush();