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

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

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

	if ( !isset($_GET['mserdid']) || $_GET['mserdid'] == '' )
  	{
    	$_GET['mserdid'] = 'null';
  	}

  	if($_GET['linked'] == '1' && $_GET['mserdid']!='-1')
  	{
  		$whereclause = "and rsd.mserd_id = {$_GET['mserdid']}";
    }
    else
    {
     	$whereclause = "and case when(m2.modalitytitle in ('CT','MG','NM','XA')) then m2.modalitytitle else 'RX' end = themodtype.mastertype and rsd.mserd_id != {$_GET['mserdid']}";

     	if ( isset($_GET['datecode']) )
     	{
     		
     		switch ( $_GET['datecode'] )
     		{
     			/*case 0:
     				break;
*/
     			case 1:
     				
     				$whereclause .= " and date >= (CURRENT_DATE::date - '1 month'::interval)";

     				break;

     			case 2:
     				
     				$whereclause .= " and date >= (CURRENT_DATE::date - '2 month'::interval)";
     				break;

     			case 6:
     				
     				$whereclause .= " and date >= (CURRENT_DATE::date - '6 month'::interval)";
     				break;
				default: break;
     		}
     	}

     	if ( isset($_GET['withoutms']) )
     	{
     		if ( $_GET['withoutms'] == 'true' )
     			$whereclause .= " and rsd.mserd_id=-1";
     	}
  	}

  	$union = "";
  	if ( isset($_GET['union']) )
  		$union = $_GET['union'];

	$psql =
		"with
			themodtype as (select type as mastertype from  master_serie_description where mserd_id = {$_GET['mserdid']}),
			themodtype1 as (select type as mastertype from  master_serie_description where mserd_id = 8)
		select
			distinct string_agg(rsd.rserd_id::text,',') over (PARTITION by m2.modalitytitle,rsd.mserd_id,rsd.description) as rserd_id,
			to_char(max(date) over (partition by  m2.modalitytitle,rsd.mserd_id,rsd.description), 'YYYYMMDD') as date,
			msd.description as mdescription,
			string_agg(m2.name,',') over (PARTITION by m2.modalitytitle, rsd.mserd_id, rsd.description) as name,
			m2.modalitytitle,rsd.description as description,
			rsd.mserd_id,
			0 as newrow
		from
			modality_identity mi, modality m2,themodtype,raw_serie_description as rsd
		left
			join master_serie_description as msd on rsd.mserd_id = msd.mserd_id
		where rsd.description != '' and mi.mod_id = m2.mod_id and mi.modident_id = rsd.modident_id {$whereclause}
		union
		select
			distinct string_agg(rsd.rserd_id::text,',') over (PARTITION by m2.modalitytitle,rsd.mserd_id,rsd.description) as rserd_id,
			to_char(max(date) over (partition by  m2.modalitytitle,rsd.mserd_id,rsd.description), 'YYYYMMDD') as date,
			msd.description as mdescription,
			string_agg(m2.name,',') over (PARTITION by m2.modalitytitle, rsd.mserd_id, rsd.description) as name,
			m2.modalitytitle,rsd.description as description,
			rsd.mserd_id,
			1 as newrow
		from
			modality_identity mi, modality m2,themodtype1,raw_serie_description as rsd
		left
			join master_serie_description as msd on rsd.mserd_id = msd.mserd_id
		where rsd.description != '' and mi.mod_id = m2.mod_id and mi.modident_id = rsd.modident_id  and rsd.mserd_id = 8
		";

	

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

		$result = json_encode ( $arr );

		echo $result;

?>