<? 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();
$user_type 		= 	$_SESSION['castalia_user_type'];
$requestData	=	$_REQUEST; 
if($user_type=='admin'):
	$columns = array(
		0	=>	'Id', 
		1	=>	'ReferenceValue'
	);  
else:
	$columns = array( 
		0	=>	'ReferenceValue'
	);  
endif;

$mainQuery 		= "SELECT Id, ReferenceValue, ReferenceType, Status FROM  reference  WHERE Status = 'active' AND ReferenceType='buyer_profile'  ";
 
$sql			=	$mainQuery;
if(!empty($requestData['search']['value']) ) { 
	$arr = explode(" ",$requestData['search']['value']);  
	foreach($arr as $ind){
		$ind = str_replace(',','',$ind);
		$sql.=" AND (Id LIKE '%".$ind."%' "; 
		$sql.=" OR ReferenceValue  LIKE '%".$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();
			 	if($user_type=='admin'):
					//ADMIN ROLE CAN ENCODE
					$nestedData[]	= '<a href="index.php?pg=settings&type=buyer_profile&action=edit&id='.$result[$a]['Id'].'"><span class="btn btn-xs btn-primary glyphicon glyphicon-pencil" title="Update Buyer Profile"></span></a>&nbsp;<span class="btn btn-xs btn-danger glyphicon glyphicon-trash" title="Delete Buyer Profile" onclick="deleteTransaction('.$result[$a]['Id'].')" ></span>';  
					$nestedData[] = '<a href="index.php?pg=settings&type=buyer_profile&action=view&id='.$result[$a]['Id'].'">'.strtoupper(trim(html_entity_decode(stripslashes($result[$a]['ReferenceValue'])))).'</a>';  
				else:
					//NOT ADMIN CANNOT ENCODE
					$nestedData[] = strtoupper(trim(html_entity_decode(stripslashes($result[$a]['ReferenceValue']))));  	
				endif;	
					
			 	 
				$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();