<?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']);
	}
	$staff_member_id = $_SESSION["id"];
	$whereclause = "where t1.staff_member_id!={$staff_member_id}";
  
  	$id 		= ( isset ($_GET['id']) )? $_GET['id'] : -1;

	// if ( $id != -1 )  	
		$whereclause .= ' and task_id='.$id;

 	$psql = "with t1 as ( with t as (select * from task_link tl left join statuslist s on tl.status_id =s.id ) select * from t left join staff_member sm on t.staff_member_id=sm.id ) select task_id,lastname,firstname, UPPER(lastname) || ' ' || firstname as member,comment,status_name,status_id,to_char(status_date, 'YYYYMMDD') as status_date from t1 {$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;
	
 
?>