<?php
	require_once '../../autoload.php';
	require( '../GetDataFromDB.php' );

	RightsCheck ("VIEW_SCHEDULEDTASK");

	$toProcess = true;
	
	$draw			= $_GET ['draw'];
	$start			= $_GET ['start'];
	$length			= $_GET ['length'];
	
	$columns 		= $_GET ['columns'];
	$order			= $_GET ['order'];
	$orderCol		= $order [0]['column'];
	$orderDir		= $order [0]['dir'];
	$orderColName	= $columns [$orderCol]['data'];
	$orderby		= " ORDER BY ".$orderColName." ".$orderDir;
        
	$limit = '';

	if ( isset($_GET['start']) && $_GET['length'] != -1 ) 
	{
		$limit = " OFFSET ".intval($_GET['start'])." LIMIT ".intval($_GET['length']);
	}

	$whereclause='';
  
  	$dep_id 		= ( isset ($_GET['dep_id']) )? $_GET['dep_id'] : -1;
  	$status_id 		= ( isset ($_GET['status_id']) )? $_GET['status_id'] : -1;
  	$personincharge = ( isset ($_GET['personincharge']) )? $_GET['personincharge'] : -1;
  	$fromdate 		= '';
  	$todate			= '';
  	$title 			= ( isset ($_GET['title']) )? $_GET['title'] : '';
  	$description 	= ( isset ($_GET['description']) )? $_GET['description'] : '';

  	if(isset($_GET['fromdate']))  		$fromdate 	= $_GET['fromdate'];
  	if(isset($_GET['todate']))  		$todate 	= $_GET['todate'];

  	/*
  	if ( $fromdate != '' )
  		$whereclause .= " and report.reportdate >= to_timestamp('{$fromdate} 00:00:00', 'DD/MM/YYYY HH24:MI:SS')";
  	if ( $todate != '' )
  		$whereclause .= " and report.reportdate <= to_timestamp('{$todate} 23:59:59', 'DD/MM/YYYY HH24:MI:SS')";
  	*/

  	if ( $personincharge != -1 )
  		$whereclause = " and action.personincharge={$personincharge}";
  	if ( $dep_id != -1 )
  		$whereclause = " and action.dep_id={$dep_id}";
  	if ( $status_id != -1 )
  		$whereclause = " and status_id={$status_id}";
  	if ( $title != '' )
  		$whereclause = " and title ilike '%{$title}%'";
  	if ( $description != '' )
  		$whereclause = " and description ilike '%{$description}%'";

 	$psql = "with t1 as (with t as (select 
 				task.id, 
 				title,
 				description, 
 				to_char(due_date, 'YYYYMMDD') as due_date,
 				to_char(status_date, 'YYYYMMDD') as status_date,
 				task.status_id,
 				statuslist.status_name as status_name,
 				removed,
 				tl.staff_member_id,
 				to_char(closing_date, 'YYYYMMDD') as closing_date
			from 
				task 
			left join
				statuslist
			on 
				task.status_id=statuslist.id 
			left join task_link tl on tl.task_id=task.id) 
			select id,title,description,due_date, status_date, status_id, status_name, closing_date,removed from t group by id,title, description, due_date, status_date, status_id, status_name, closing_date, removed)
			select id,title,description,due_date, status_id,status_name, closing_date from t1 where t1.removed=false {$whereclause} group by id,title,description,due_date, status_id,status_name, closing_date
			";


	$recordsTotal 		= 1000;
	$recordsFiltered	= 0;
	
	$data				= Array ();
	
	if ( $toProcess == true )
		$data = GetDataFromDB ( $psql, $recordsFiltered, $limit, $orderby );
	
	$arr = array (
			"draw"            => intval( $draw ),
			"recordsTotal"    => intval( $recordsTotal ),
			"recordsFiltered" => intval( $recordsFiltered ),
			"data"            => $data,
			"sql"			  => $psql.$orderby.$limit
	);

	$result = json_encode ( $arr );

	echo $result;
	
 
?>