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

	$toProcess = true;
	
	$name		 	= $_GET ['name'];
	
	$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']);
	}

  $name 		= (isset($_GET['name']) && $_GET['name'] != '')? $_GET['name'] : '';   	 
  $taglist 	= (isset($_GET['taglist']) && $_GET['taglist'] != '')? $_GET['taglist'] : '';   	 

  $tagcondition = '';
  if ( $taglist != '')
  {
    	$tagcondition = "and ((alltagidallow) && '{{$_GET['taglist']}}'::integer[])";;
  }
  	/*
	$psql = "select distinct procedure.id as id, name_fr as name, procedure.comment as comment,proc_active_document.doc_id as doc_id from procedure left join
(select document_link.doc_id, document_link.ext_id from v_last_documentstatus, document_link where
v_last_documentstatus.status_id = 1 and v_last_documentstatus.doc_id = document_link.doc_id and document_link.type = 'procedure') as proc_active_document
on proc_active_document.ext_id = procedure.id and procedure.removed = false,v_notremoveddocument,v_taginfo
where v_notremoveddocument.id = proc_active_document.doc_id and procedure.removed = false and v_taginfo.ext_id = procedure.id
and v_taginfo.link_type = 'procedure' and procedure.name_fr ilike '%".$name."%' and ((alltagidallow) && '{{$_SESSION['alltagidallow']}}'::integer[]) {$tagcondition}";
*/
	
	$staff_member_id	= $_SESSION['id'];

	$psql = "with r0 as 
			(
				select distinct procedure.id as id, name_fr as name, procedure.comment as comment,proc_active_document.doc_id as doc_id 
				from 
					procedure 
				left join
					( 
						select document_link.doc_id, document_link.ext_id from v_last_documentstatus, document_link 
						where v_last_documentstatus.status_id = 14 and v_last_documentstatus.doc_id = document_link.doc_id and document_link.type = 'procedure'
					) 	as proc_active_document 
				on 
					proc_active_document.ext_id = procedure.id and procedure.removed = false,v_notremoveddocument,v_taginfo
				where 
					v_notremoveddocument.id = proc_active_document.doc_id and procedure.removed = false 
					and v_taginfo.ext_id = procedure.id and v_taginfo.link_type = 'procedure' 
					and procedure.name_fr ilike '%".$name."%' and ((alltagidallow) && '{{$_SESSION['alltagidallow']}}'::integer[]) {$tagcondition}
			)
			select 
				id, name, comment, r0.doc_id, staff_member_id, action_at 
			from 
				r0 left join action_status_link 
			on r0.doc_id=action_status_link.document_id and staff_member_id=".$staff_member_id." group by id,name,comment,doc_id,staff_member_id,action_at";

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