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

	RightsCheck ("VIEW_SCHEDULEDTASK_WORKING");

	$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;
  	$staff_member_id 	= ( isset ($_GET['staff_member_id']) )? $_GET['staff_member_id'] : -1;
  	$fromdate 			= '';
  	$todate				= '';
  	$title 				= ( isset ($_GET['title']) )? $_GET['title'] : '';
  	$description 		= ( isset ($_GET['description']) )? $_GET['description'] : '';
  	$nearest_task_only	= ( isset ($_GET['nearest_task_only']) )? $_GET['nearest_task_only'] : '';

  	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 ( $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}%'";
  	if ( $staff_member_id != -1 )
  		$whereclause .= " and staff_member_id={$staff_member_id}";

  	$psql = "";
  	if ( $nearest_task_only == 1 )
  		$psql = "with t2 as 
			(with t1 as (select 
 				t.id, 
 				t.title,
 				t.description, 
 				due_date,
 				to_char(due_date, 'YYYYMMDD') as due_date_fmt,
 				tl.status_id,
 				statuslist.status_name as status_name,
 				to_char(tl.status_date,'YYYYMMDD') as status_date,
 				tl.staff_member_id,
 				t.removed,
 				tm.recurrence,
 				t.task_master_id,
 				tl.comment
			from 
				task t 
			left join task_master tm on tm.id=t.task_master_id 
			left join task_link tl on tl.task_id=t.id 
			left join
				statuslist
			on 
				tl.status_id=statuslist.id ) 
			select * from t1 where removed=false and t1.staff_member_id={$staff_member_id}) 
			select t3.* from ( select *, row_number() over (partition by t2.task_master_id order by due_date asc) as rn from t2) t3 where t3.rn=1 {$whereclause}";
	else
		$psql = "with t2 as 
			(with t1 as (select 
 				t.id, 
 				t.title,
 				t.description, 
 				due_date,
 				to_char(due_date, 'YYYYMMDD') as due_date_fmt,
 				tl.status_id,
 				statuslist.status_name as status_name,
 				to_char(tl.status_date,'YYYYMMDD') as status_date,
 				tl.staff_member_id,
 				t.removed,
 				tm.recurrence,
 				t.task_master_id,
 				tl.comment
			from 
				task t 
			left join task_master tm on tm.id=t.task_master_id 
			left join task_link tl on tl.task_id=t.id 
			left join
				statuslist
			on 
				tl.status_id=statuslist.id ) 
			select * from t1 where removed=false and t1.staff_member_id={$staff_member_id}) 
			select * from t2 where removed=false {$whereclause}";
  	
	
	error_log ( "psql : " .$psql );
	
	$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;
	
 
?>