<?php
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	=>	'FirstName', 
	3	=>	'LastName',
	4	=>	'Username',
	5	=>	'UserType',
	6	=>	'SeqNo'
);  

$mainQuery 		= "SELECT Id, FirstName, LastName, Username, UserType, SeqNo FROM user  ";
$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 (FirstName LIKE '%".htmlentities($ind)."%' "; 
		$sql.=" OR Username LIKE '%".$ind."%' "; 
		$sql.=" OR UserType LIKE '%".$ind."%' ";    
		$sql.=" OR LastName  LIKE '%".htmlentities($ind)."%' )";
	}
}


$mecon->query($sql);
$result			= $mecon->resultset(); 
$totalData		= count($result);
$totalFiltered	=	$totalData;
  
$sql.=" ORDER BY SeqNo ASC, ".$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=users&type=users&action=edit&id='.$result[$a]['Id'].'"><span class="btn btn-xs btn-warning glyphicon glyphicon-pencil" title="User"></span></a>&nbsp;&nbsp;&nbsp;&nbsp;<span onclick="deleteTransaction('.$result[$a]['Id'].')" class="btn btn-xs btn-danger glyphicon glyphicon-trash" title="Delete User"></span>'; 
				$nestedData[]	= $a+1;    
				$nestedData[] = (!empty($result[$a]['FirstName'])) ? $result[$a]['FirstName'] : "";
				$nestedData[] = (!empty($result[$a]['LastName'])) ? $result[$a]['LastName'] : "";
				$nestedData[] = (!empty($result[$a]['Username'])) ? $result[$a]['Username'] : "";
				$nestedData[] = (!empty($result[$a]['UserType'])) ? $result[$a]['UserType'] : ""; 
				$nestedData[] = (!empty($result[$a]['SeqNo'])) ? $result[$a]['SeqNo'] : ""; 
				 
				$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();