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

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

	$toProcess = true;

	$type 			= '';
	$description 	= '';
	$anatomicregion	= '';

	if ( isset ($_GET ['type'] ) )		  		$type 			= $_GET['type'];
	if ( isset ($_GET ['description'] ) )  		$description 	= $_GET['description'];
	if ( isset ($_GET ['anatomicregion'] ) ) 	$anatomicregion = $_GET['anatomicregion'];

	$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']);
	}

	$mstudid = -1;
	if ( isset($_GET['mstudid']) )
		$mstudid = $_GET['mstudid'];

	$psql = "select
				link_id,
				nrd.nrd_id as nrd_id,
				nrd.type as type,
				valuetype,
				value as inp_value,
				nacquisition as inp_nacquisition,
				round(age_begin/12.0,1) || '-<' ||round(age_end/12.0,1) as age,
				age_begin,
				age_end,
				weight_begin|| '-<' ||weight_end as weight,
				weight_begin,
				weight_end,
				nrd.type||valuetype as combo_valuetype,
				alertname as inp_alertname
			from
				link_nrd, nrd
   			where
   				link_nrd.nrd_id = nrd.nrd_id
   			and link_nrd.type = 1
   			and link_nrd.link_id = {$mstudid}";

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

?>