<?php
	require_once '../../autoload.php';
	require( '../GetDataFromDB.php' );
	
	RightsCheck ("VIEW_EXAMLIST_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 = '';
  if(isset($_GET['name']))
  {
     $whereclause = "and examlist.nameid ilike '%{$_GET['name']}%'";
  }

  if(isset($_GET['description']))
  {
     $whereclause = "and examlist.description ilike '%{$_GET['description']}%'";
  }

  $whereclause = '';
  if(isset($_GET['examid']))
  {
     $whereclause = "and examlist.id = {$_GET['examid']}";
  }

	if(isset($_GET['taglist']) && $_GET['taglist'] != '')
	{
		//(alltagidallow||alltagidinducedallow) & '{3,13}'::integer[]
	//  $having = " having (alltagidallow - '{{$_GET['taglist']}}'::integer[]) != '{}'::integer[]";
		if(isset($_GET['idtaglistand']) && $_GET['idtaglistand'] != '')
			$whereclause .= "and ((alltagidallow||alltagidinducedallow)@> '{{$_GET['idtaglistand']}}'::integer[])";

		if(isset($_GET['idtaglistor']) && $_GET['idtaglistor'] != '')
			$whereclause .= " and tag_id = any('{{$_GET['idtaglistor']}}'::integer[])";

	}

	/*
   $psql = "select distinct allexam.id,modality,nameid,description,riskv,taglist,doc_id,doc_otherdoc_id from (select examlist.id,modality,nameid,description,STRING_AGG (examwarning_link.examwarning_id::text,',') as riskv,f_tag_link(examlist.id) as taglist
from examlist left join examwarning_link on examwarning_link.exam_id = examlist.id
where examlist.removed = false and (examwarning_link.examwarning_id is null or examwarning_link.examwarning_id is not null) group by examlist.id,nameid,description) as allexam
left join (select document_link.doc_id, document_link.ext_id from v_last_documentstatus, document_link,v_notremoveddocument where v_notremoveddocument.id = document_link.doc_id and v_last_documentstatus.status_id = 1 and v_last_documentstatus.doc_id = document_link.doc_id and document_link.type = 'examlist') as allexamwithdoc on allexamwithdoc.ext_id = allexam.id
left join (select document_link.doc_id as doc_otherdoc_id, document_link.ext_id from v_last_documentstatus, document_link,v_notremoveddocument where v_notremoveddocument.id = document_link.doc_id and v_last_documentstatus.status_id = 1 and v_last_documentstatus.doc_id = document_link.doc_id
and document_link.type = 'examlist_otherdocument') as allexamwithotherdoc on allexamwithotherdoc.ext_id = allexam.id, v_taginfo
where v_taginfo.link_type ='examlist' and v_taginfo.ext_id = allexam.id and alltagidallow||alltagidinducedallow && '{{$_SESSION['alltagidallow']}}'::integer[] {$whereclause}";
*/

$staff_member_id	= $_SESSION['id'];


$psql = "with r0 as 
		(
			select distinct allexam.id, modality, nameid, description, riskv, taglist, doc_id, doc_otherdoc_id 
			from 
			(
				select examlist.id,modality,nameid,description,STRING_AGG (examwarning_link.examwarning_id::text,',') as riskv,f_tag_link(examlist.id) as taglist
				from examlist left join examwarning_link on examwarning_link.exam_id = examlist.id
				where examlist.removed = false and (examwarning_link.examwarning_id is null or examwarning_link.examwarning_id is not null) group by examlist.id,nameid,description
			) as allexam
			left join 
			(
				select document_link.doc_id, document_link.ext_id from v_last_documentstatus, document_link,v_notremoveddocument where v_notremoveddocument.id = document_link.doc_id and v_last_documentstatus.status_id = 14 and v_last_documentstatus.doc_id = document_link.doc_id and document_link.type = 'examlist'
			) as allexamwithdoc on allexamwithdoc.ext_id = allexam.id
			left join 
			(
				select document_link.doc_id as doc_otherdoc_id, document_link.ext_id from v_last_documentstatus, document_link,v_notremoveddocument where v_notremoveddocument.id = document_link.doc_id and v_last_documentstatus.status_id = 14 and v_last_documentstatus.doc_id = document_link.doc_id
					and document_link.type = 'examlist_otherdocument'
			) as allexamwithotherdoc on allexamwithotherdoc.ext_id = allexam.id, v_taginfo
			where v_taginfo.link_type ='examlist' and v_taginfo.ext_id = allexam.id and alltagidallow||alltagidinducedallow && '{{$_SESSION['alltagidallow']}}'::integer[] {$whereclause}
		),
r1 as
		(
		select id, modality, nameid, description, riskv, taglist, doc_id, doc_otherdoc_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."
		)
		select id, modality, nameid, description, riskv, taglist, doc_id, doc_otherdoc_id, r1.staff_member_id, r1.action_at as read_doc_at, action_status_link.action_at as read_otherdoc_at from r1 left join action_status_link on r1.doc_otherdoc_id=action_status_link.document_id and action_status_link.staff_member_id=".$staff_member_id;

   error_log ( "GetDTExamLight : " .$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;
	
 
?>