<? 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',
	2	=>	'Block', 
	3	=>	'Lot'  
);  

$blockFrom 	= isset($_REQUEST['block_from']) 	?  	$_REQUEST['block_from'] 	: "";
$blockTo 	= isset($_REQUEST['block_to'])   	? 	$_REQUEST['block_to']		: "";
$lotFrom 	= isset($_REQUEST['lot_from']) 		?  	$_REQUEST['lot_from'] 		: "";
$lotTo 		= isset($_REQUEST['lot_to'])   		? 	$_REQUEST['lot_to']	  		: "";

$mainQuery  = "SELECT units.Id as Id, units.Block as Block, units.Lot as Lot, units.Status as Status FROM units LEFT JOIN units_block_lot ON 
units_block_lot.UnitId=units.Id ";

if(isset($blockFrom) && !empty($blockFrom) && isset($blockTo) && !empty($blockTo) && isset($lotFrom) && !empty($lotFrom) && isset($lotTo) && !empty($lotTo)){  
	$sql			=	$mainQuery."  WHERE units.Status = 'active' AND units_block_lot.Block BETWEEN $blockFrom AND $blockTo AND units_block_lot.Lot BETWEEN $lotFrom AND $lotTo "; 
}else if(isset($blockFrom) && !empty($blockFrom) && isset($blockTo) && !empty($blockTo)){  
	$sql			=	$mainQuery."  WHERE units.Status = 'active' AND units_block_lot.Block BETWEEN $blockFrom AND $blockTo "; 
}else if(isset($lotFrom) && !empty($lotFrom) && isset($lotTo) && !empty($lotTo)){  
	$sql			=	$mainQuery."  WHERE units.Status = 'active' AND units_block_lot.Lot BETWEEN $lotFrom AND $lotTo "; 
}else{
	$sql			=	$mainQuery."  WHERE units.Status = 'active' ";	
}


if(!empty($requestData['search']['value']) ) { 
	$arr = explode(" ",$requestData['search']['value']);  
	foreach($arr as $ind){
		$ind = str_replace(',','',$ind);
		$sql.=" AND (units.Block LIKE '%".htmlentities($ind)."%' "; 
		$sql.=" OR units.Lot LIKE '%".$ind."%' ";  
		$sql.=" OR units.Status LIKE '%".$ind."%'  )"; 
	}
}

$sql .= " GROUP BY units.Id ";
 
$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']."   "; 


$sql.=" ORDER BY cast(units.Block as unsigned)   ".$requestData['order'][0]['dir'].", cast(units.Lot as unsigned) asc 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=status&type=permits_and_licenses&action=edit&id='.$result[$a]['Id'].'"><span class="btn btn-xs btn-primary glyphicon glyphicon-pencil text-center center" title="Update Permits and Licenses"></span></a>'; 
				//$nestedData[]	= $a+1;    
				$nestedData[] = (!empty($result[$a]['Block'])) ? $result[$a]['Block'] : ""; 
				$nestedData[] = (!empty($result[$a]['Lot'])) ? $result[$a]['Lot'] : ""; 
				$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();