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

	require( '../GetDataFromDB.php' );

	$toProcess = true;

	// $pat_id		 	= $_GET ['pat_id'];

	$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 	= ' and study.stu_id = -42';
	$othersite 		= '';

	 if(isset($_GET['patid']) && $_GET['patid'] != '')
	 {
	 	$conn = get_dbconnection();

		 $whereclause = "and study.pat_id = {$_GET['patid']}";

		 $sql = "select id,his_id from patient,institution where pat_id = {$_GET['patid']} and institution.inst_id = patient.inst_id;";

		  if($_GET['patid'] == '-1' || $query=pg_query($conn,$sql))
		  {

		 	 if($_GET['patid'] == '-1' || $row = pg_fetch_row($query))
		 	 {
				 $nocondition = '';
				 if($_GET['patid'] == '-1')
				 {
					 $nocondition = ' and false';
					 $row = array();
					 $row[0] = '';
					 $row[1] = '-1';
				 }
				 $requestlist = array();
		 		 $idpatient = pg_escape_string($conn,$row[0]);
				 $his_id = $row[1];
				 $sessionGLOBALS['eoDACS_FDW']= $GLOBALS['eoDACS_FDW'];

		 	 	 foreach ($sessionGLOBALS['eoDACS_FDW'] as $key => &$site ) {

					 if($site['online'] == false)
					 	continue;

						$sessionGLOBALS['eoDACS_FDW'][$key]['dblinkconnectionname'] = $sessionGLOBALS['eoDACS_FDW'][$key]['servername'];
						if(!array_key_exists($key, $requestlist))
							$requestlist[$key] = array();
						$groupname = pg_escape_string($conn,$site["groupname"]);
		 	 		 	$requestlist[$key][] = "select '{$key}_'||stu_id as stu_id,v_modality_config.modalitytitle,to_char(study.date,'DD/MM/YYYY') as date, '{$groupname}' as groupname,accessionnumber,description_auto as description
				 	 from {$site["schemaremote"]}.study,{$site["schemaremote"]}.v_modality_config,{$site["schemaremote"]}.patient,{$site["schemaremote"]}.institution where study.pro_id = v_modality_config.pro_id
					 {$nocondition} and study.pat_id  = patient.pat_id and patient.id = '{$idpatient}' and institution.inst_id = patient.inst_id and his_id = $his_id";
		 	 	 }
		 	 	 $othersite = get_full_fdw_request($conn,$requestlist,$sessionGLOBALS);
		 	 }
		  }
	}

	$psql = $othersite;

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


?>