<?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		= '';

  	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}";

 	$psql = "select 
 				action.id as id, 
 				action.dep_id as dep_id, 
 				v_fullinfo_department.fullname as department, 
 				-1 as shortcut_pin, 
 				description, 
 				coalesce(action.personincharge,-1) as personincharge, 
 				to_char(begdate,'YYYYMMDD') as begdate,
 				status_id, 
 				statuslist.status_name , 
 				origine,
 				to_char(deadline,'YYYYMMDD') as deadline, 
 				to_char(enddate,'YYYYMMDD') as closeddate, 
 				'' as actionlinkid, 
 				staff_member.lastname || ' ' || staff_member.firstname as fullname,
 				action.comment
			from 
				action 
			left join 
				staff_member 
			on 
				(action.personincharge = staff_member.id) left join tag_link on tag_link.type ='department' and tag_link.ext_id = dep_id  left join statuslist on status_id=statuslist.id left join v_fullinfo_department on action.dep_id=v_fullinfo_department.dep_id where action.removed = false {$whereclause}";

	if ( isset($_GET['forselection']) && $_GET['forselection']=='1' )
	{
		error_log ( " scope : " .$_GET['scope'] );
		error_log ( " scopeid : " .$_GET['scopeid'] );
		$psql = "select 
					action.dep_id as dep_id,
					action.id as id, 
					case when action_link.id is null then 0 else 1 end as shortcut_pin, 
					description,
					coalesce(action.personincharge,-1) as personincharge, 
					to_char(begdate,'YYYYMMDD') as begdate,
					status_id,
					origine,
					to_char(deadline,'YYYYMMDD') as deadline, 
					to_char(enddate,'YYYYMMDD') as closeddate, 
					action_link.id as actionlinkid, 
 					staff_member.lastname || ' ' || staff_member.firstname as fullname,
 					action.comment
    			from 
    				action 
    			left join 
    				staff_member 
    			on (action.personincharge = staff_member.id) left join action_link on action_link.scope = '{$_GET['scope']}' and action.id = action_link.action_id and action_link.ext_id = {$_GET['scopeid']}  left join tag_link on tag_link.type ='department' and tag_link.ext_id = dep_id  where action.removed = false {$whereclause}";
	}


	$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;
	
 
?>