<?php
	require_once '../../autoload.php';

	$result = array (
			"status"	=> 'KO',
			"results"	=> array ()
	);
	

	$pdo = getPDO ();
	
	if ( $pdo )
	{
		$tags = $_POST['tags'];
		
		$sql = "with req1 as (select distinct institution.id as inst_id, institution.name, tag.id, '' as department, ('{'||(string_agg(department.id::text,',') over (partition by institution.id))||'}')::int[] <@ array[{$_SESSION['deplistid']}] as alldepininst from institution,tag,tag_link,department where tag_link.type = 'institution'	and tag_link.ext_id = institution.id and tag.id = tag_link.tag_id and tag.removed = false and department.inst_id = institution.id and department.removed = false order by institution.name),
		req2 as (select department.inst_id,institution.name,tag.id,department.id as dep_id,department.name from institution,department,tag,tag_link where department.id in({$_SESSION['deplistid']}) and institution.id =department.inst_id and tag_link.type = 'department' and tag_link.ext_id = department.id and tag.id = tag_link.tag_id and tag.removed = false and institution.removed = false and department.removed = false order by institution.name, tag.name)
		select req1.id as tag_id,department.inst_id, department.id as dep_id, department.name from department,req1 where department.removed = false and department.inst_id=req1.inst_id and req1.id in ({$tags}) union select req2.id as tag_id,department.inst_id, department.id as dep_id, department.name from department,req2 where department.removed = false and department.id=req2.dep_id and req2.id in ({$tags})";
		
		/*
		$sql = "with req1 as (select distinct institution.id as inst_id, institution.name, tag.id, '' as department, ('{'||(string_agg(department.id::text,',') over (partition by institution.id))||'}')::int[] <@ array[{$_SESSION['deplistid']}] as alldepininst from institution,tag,tag_link,department where tag_link.type = 'institution'	and tag_link.ext_id = institution.id and tag.id = tag_link.tag_id and tag.removed = false and department.inst_id = institution.id and department.removed = false order by institution.name),
		req2 as (select department.inst_id,institution.name,tag.id,department.id as dep_id,department.name from institution,department,tag,tag_link where department.id in({$_SESSION['deplistid']}) and institution.id =department.inst_id and tag_link.type = 'department' and tag_link.ext_id = department.id and tag.id = tag_link.tag_id and tag.removed = false and institution.removed = false and department.removed = false order by institution.name, tag.name)
		select req1.id as tag_id,department.inst_id, department.id as dep_id, department.name from department,req1 where department.removed = false and department.inst_id=req1.inst_id and req1.id in (:tags)::int[] union select req2.id as tag_id,department.inst_id, department.id as dep_id, department.name from department,req2 where department.removed = false and department.id=req2.dep_id and req2.id in (:tags)::int[]";
		*/
		$s0 = $pdo->prepare ( $sql );
		// $s0->bindParam ( ':tags', $tags );

		if ( $s0->execute () )
		{
			$result['status'] = 'OK';

			$d0 = $s0->fetchAll ( PDO::FETCH_ASSOC );

			for ( $i=0; $i<count($d0); $i++ )
			{
				error_log ( "Doc : " .$d0[$i]['department_id']. " / " .$d0[$i]['staff_role_id'] );

				array_push ( $result['results'], array ( "tag_id" => $d0[$i]['tag_id'], "inst_id" => $d0[$i]['inst_id'], "dep_id" => $d0[$i]['dep_id'] ) );
			}
		}
	}

	header('Content-type: application/json');
	
	echo json_encode ( $result );
?>