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

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


	$recdate_from			= '';
	$recdate_to				= '';
	$modalitytitle			= '';
	$patientname 			= '';
	$stationname 			= '';
	$status		 			= -1;
	$integration_error 		= '';
	$integration_message 	= '';

	if ( isset ($_GET ['recdate_from'] ) )			$recdate_from			= $_GET['recdate_from'];
	if ( isset ($_GET ['recdate_to'] ) )			$recdate_to				= $_GET['recdate_to'];
	if ( isset ($_GET ['modalitytitle'] ) )			$modalitytitle			= $_GET['modalitytitle'];
	if ( isset ($_GET ['patientname'] ) ) 			$patientname 			= $_GET['patientname'];
	if ( isset ($_GET ['stationname'] ) ) 			$stationname 			= $_GET['stationname'];
	if ( isset ($_GET ['status'] ) ) 				$status 				= $_GET['status'];
	if ( isset ($_GET ['integration_error'] ) ) 	$integration_error 		= $_GET['integration_error'];
	if ( isset ($_GET ['integration_message'] ) ) 	$integration_message	= $_GET['integration_message'];

	$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 to_char(recdate,'YYYY-MM-DD') >= '{$recdate_from}'";
	$whereclause .= " and to_char(recdate,'YYYY-MM-DD') <= '{$recdate_to}'";
	if ( $stationname != "" )
		$whereclause .= " and source || '('|| stationname||')' ilike '%{$stationname}%'";
	if ( $modalitytitle != '' )
		$whereclause .= " and modalitytitle ilike '{$modalitytitle}%'";
	if ( $patientname != "" )
		$whereclause .= " and patientname ilike '{$patientname}%'";
	if ( $status != -1 )
		$whereclause .= " and status = {$status}";
	if ( $integration_error != "" )
		$whereclause .= " and integration_error ilike '{$integration_error}%'";
	if ( $integration_message != "" )
		$whereclause .= " and integration_message ilike '{$integration_message}%'";

	$psql =	"select
				min(sto_id) as sto_id,
				to_char(min(recdate),'DD/MM/YYYY HH24:MI:SS') as recdate,
				source || '('|| stationname||')' as source,
				modalitytitle,
				patientname,
				status,
				integration_error,
				integration_message,
				array_agg(sto_id) as liststo
			from
				storagelist
			where
				1=1 {$whereclause} group by stuid,source || '('|| stationname||')',modalitytitle,patientname,status,integration_error,integration_error,integration_message";


	$recordsTotal 		= 1000;
	$recordsFiltered	= 0;

	$data				= Array ();

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