<?php
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['danvillee_user_type'];
$username       = 	$_SESSION['danvillee_username'];

$requestData	=	$_REQUEST; 
$columns = array(
	0	=>	'Id',
	1	=>	'ReservationDate',
	2	=>	'Buyer',
	3	=>	'Block',
	4	=>	'Lot' 
); 


$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 mf.ReservationId as Id, DATE_FORMAT(r.ReservationDate,'%m/%d/%Y') as ReservationDate, r.Block as Block, r.Lot as Lot, CONCAT(r.LastName,', ',r.FirstName) as Buyer FROM monitoring_folders mf  LEFT JOIN reservation r ON r.Id=mf.ReservationId";  
 
 
$sql			=	$resultQuery."  WHERE mf.MonitoringFolderDetails='[\"req_completed\",\"signed_loan_docs\",\"sworn_cert\",\"tax_dec_land\",\"tax_dec_building\",\"tax_land_receipt\",\"tax_building_receipt\",\"building_permit\",\"electrical_permit\",\"sanitary_permit\",\"occupancy_permit\",\"road\",\"sidewalk_and_gutter\",\"meralco\",\"water\"]'  ";
   


 
if(!empty($requestData['search']['value']) ) { 
	$arr = explode(" ",$requestData['search']['value']);  
	foreach($arr as $ind){
		$ind = str_replace(',','',$ind); 
		$sql.=" AND (CONCAT(r.LastName,', ',r.FirstName) LIKE '%".$ind."%' ";
		$sql.=" OR r.Lot LIKE '%".$ind."%' "; 
		$sql.=" OR r.Block LIKE '%".$ind."%' ) ";  
	}
}
 
if($tobedate=="yes"){
	if( !empty($rangeStart) && !empty($rangeEnd) ){
		$sql .= " AND r.ReservationDate BETWEEN ('".date('Y-m-d', strtotime($rangeStart))."') AND ('".date('Y-m-d', strtotime($rangeEnd))."')";
	}else if( !empty($rangeStart)){
		$sql .= " AND r.ReservationDate >= ('".date('Y-m-d', strtotime($rangeStart))."') ";
	}else if( !empty($rangeEnd)){
		$sql .= " AND r.ReservationDate <= ('".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']."  ,cast(r.Block as unsigned) asc, cast(r.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[]	= $result[$a]['Id']; 
				$nestedData[] = (($result[$a]['ReservationDate']=='1970-01-01') || ($result[$a]['ReservationDate']=='')) ? "" : date('m/d/Y', strtotime($result[$a]['ReservationDate']));  
				$nestedData[]	= "<a href='index.php?pg=reservation&type=monitoring_folders&action=edit&id=".$result[$a]['Id']."'>".$result[$a]['Buyer']."</a>";  
				$nestedData[]	= $result[$a]['Block']; 
				$nestedData[]	= $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();