<? 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	=>	'Broker', 
	2	=>	'Landline',
	3	=>	'ContactNo',
	4	=>	'EmailAddress',
	5	=>	'Address' 
);  

$mainQuery 		= "SELECT Id, Broker, ContactNo, ContactPerson, Landline, EmailAddress, Address, Status FROM  broker ";
$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 (Broker LIKE '%".htmlentities($ind)."%' "; 
		$sql.=" OR ContactNo LIKE '%".$ind."%' "; 
		$sql.=" OR EmailAddress LIKE '%".$ind."%' ";   
		$sql.=" OR ContactPerson LIKE '%".$ind."%' ";   
		$sql.=" OR Landline LIKE '%".$ind."%' ";   
		$sql.=" OR Status LIKE '%".$ind."%' ";   
		$sql.=" OR Address  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=broker&action=edit&id='.$result[$a]['Id'].'"><span class="btn btn-xs btn-primary glyphicon glyphicon-pencil" title="Update Broker"></span></a>&nbsp;<span class="btn btn-xs btn-danger glyphicon glyphicon-trash" title="Delete Broker" onclick="deleteTransaction('.$result[$a]['Id'].')" ></span>'; 
				//$nestedData[]	= $a+1;   
				$nestedData[] = '<a style="text-align:left" href="index.php?pg=settings&type=broker&action=view&id='.$result[$a]['Id'].'">'.strtoupper(trim(html_entity_decode(stripslashes($result[$a]['Broker'])))).'</a>';   
				$nestedData[] = (!empty($result[$a]['Landline'])) ? $result[$a]['Landline'] : "";
				$nestedData[] = (!empty($result[$a]['ContactNo'])) ? $result[$a]['ContactNo'] : "";
				$nestedData[] = (!empty($result[$a]['EmailAddress'])) ? $result[$a]['EmailAddress'] : "";
				$nestedData[] = (!empty($result[$a]['Address'])) ? $result[$a]['Address'] : ""; 
				 
				$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();