<? 
error_reporting(0);
ob_start("ob_gzhandler");
session_start();
session_cache_limiter('private');
$cache_limiter 	= session_cache_limiter();
session_cache_expire(60);
$cache_expire 	= session_cache_expire(); 
date_default_timezone_set('Asia/Manila'); 
$CURRENT_DATE 	= date('Y-m-d H:i:s');
include_once('../config/minho.php');
include_once('../config/autoloadClass.php');
$autoload		=	new autoloadClass(); 
$mecon			=	new condbClass();
$userType       = 	$_SESSION['castalia_user_type'];
$username       = 	$_SESSION['castalia_username'];

$requestData	=	$_REQUEST; 
$columns = array(
	0	=>	'Id',
	1	=>	'ActionDate',
	2	=>	'Remarks',
	3	=>	'ActionBy' 
); 


$rangeStart = isset($_REQUEST['range_start']) ?  $_REQUEST['range_start'] : date('Y-m-d', strtotime(date('01-m-Y')));
$rangeEnd 	= isset($_REQUEST['range_end'])   ? $_REQUEST['range_end']	  : date("Y-m-t", strtotime(date('Y-m-d')));
 
$tobedate 	= isset($_REQUEST['tobe_stat_date'])   ? $_REQUEST['tobe_stat_date']	  : "yes";

$resultQuery 		= "SELECT Id, ActionDate, Remarks, ActionBy FROM activity_log ";  
if($userType=='admin'){
$sql			=	$resultQuery."  WHERE status='active' AND ActionBy!='$username'  ";
}else{
$sql			=	$resultQuery."  WHERE status='active' AND ActionBy!='$username' AND UserType LIKE '%$userType%'  ";
}  


 
if(!empty($requestData['search']['value']) ) { 
	$arr = explode(" ",$requestData['search']['value']);  
	foreach($arr as $ind){
		$ind = str_replace(',','',$ind); 
		$sql.=" AND (Remarks LIKE '%".$ind."%' ";
		$sql.=" OR ActionDate LIKE '%".$ind."%' "; 
		$sql.=" OR ActionBy LIKE '%".$ind."%' "; 
		$sql.=" OR DATE_FORMAT(ActionDate,'%m/%d/%Y H:i:s') LIKE '%".$ind."%' )"; 
	}
}
 
if($tobedate=="yes"){
	if( !empty($rangeStart) && !empty($rangeEnd) ){
		$sql .= " AND ActionDate BETWEEN ('".date('Y-m-d', strtotime($rangeStart))."') AND ('".date('Y-m-d', strtotime($rangeEnd))."')";
	}else if( !empty($rangeStart)){
		$sql .= " AND ActionDate >= ('".date('Y-m-d', strtotime($rangeStart))."') ";
	}else if( !empty($rangeEnd)){
		$sql .= " AND ActionDate <= ('".date('Y-m-d', strtotime($rangeEnd))."') ";
	}
}

$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+1;
				$nestedData[] = (($result[$a]['ActionDate']=='1970-01-01') || ($result[$a]['ActionDate']=='')) ? "" : date('m/d/Y H:i:s', strtotime($result[$a]['ActionDate']));  
				$nestedData[]	= $result[$a]['Remarks']; 
				$nestedData[]	= $result[$a]['ActionBy'];  
				$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();