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

	error_log ( "### save_nrd " );

	$id		 		= -1;
	
	if ( isset ( $_POST['id'] ) 			)	$id			 	= $_POST['id'];
		
	$pdo = getPDO ();
	
	$response = -1;
	
	if ( $pdo )
	{
		$sql = '';

		if ( $id == -1 )
		{
			$type 		= 'CT';
			$valuetype 	= 'CTDIvol';

			$sql = "insert into nrd(type,valuetype,age_begin,age_end,validity_begin,value) values (:type, :valuetype,0,2400,'1900-01-01',0) returning nrd_id;";

			$stmt = $pdo->prepare ( $sql );
			$stmt->bindParam ( ':type', $type );
			$stmt->bindParam ( ':valuetype', $valuetype );
		}
		else
		{
			$name			= "";
			$type 			= "";
			$value 			= "";
			$nb_acqui		= "";
			$age_begin		= "";
			$age_end		= "";
			$weight_begin	= "";
			$weight_end		= "";

			if ( isset ( $_POST['name'] )			)	$name		 		= $_POST['name'];
			if ( isset ( $_POST['combo_valuetype'] 	))	$combo_valuetype	= $_POST['combo_valuetype'];
			if ( isset ( $_POST['value'] )			)	$value		 		= $_POST['value'];
			if ( isset ( $_POST['nb_acqui'] )		)	$nb_acqui		 	= $_POST['nb_acqui'];
			if ( isset ( $_POST['age_begin'] )		)	$age_begin		 	= $_POST['age_begin'];
			if ( isset ( $_POST['age_end'] )		)	$age_end		 	= $_POST['age_end'];
			if ( isset ( $_POST['weight_begin'] )	)	$weight_begin	 	= $_POST['weight_begin'];
			if ( isset ( $_POST['weight_end'] )		)	$weight_end		 	= $_POST['weight_end'];

			if ( $nb_acqui == "" 		)	$nb_acqui 		= null;
			if ( $age_begin == "" 		)	$age_begin 		= null;
			if ( $age_end == "" 		)	$age_end 		= null;
			if ( $weight_begin == "" 	)	$weight_begin 	= null;
			if ( $weight_end == "" 		)	$weight_end 	= null;

			switch ( $combo_valuetype )
  			{
    			case 'CTCTDIvol':   $type = 'CT'; 	$valuetype = 'CTDIvol'; 	break;
    			case 'CTDLP': 		$type = 'CT';	$valuetype = 'DLP'; 		break;
    			case 'RXDAP': 		$type = 'RX';	$valuetype = 'DAP'; 		break;
    			case 'MGDGM': 		$type = 'MG';	$valuetype = 'DGM'; 		break;
    			case 'RIRPDOSE': 	$type = 'RI';	$valuetype = 'RPDOSE'; 		break;
    			case 'RISCOPIE': 	$type = 'RI';	$valuetype = 'SCOPIE'; 		break;
    			case 'RIDAP': 		$type = 'RI';	$valuetype = 'DAP'; 		break;
    			case 'NMACTIVITY': 	$type = 'NM';	$valuetype = 'ACTIVITY'; 	break;
    			case 'NMSACTIVITY': $type = 'NM';	$valuetype = 'SACTIVITY'; 	break;
    			default:
  			}

			$sql = 'update nrd';
  			$sql .= ' set alertname = :name';
  			$sql .= ' ,type = :type';
  			$sql .= ' ,valuetype = :valuetype';
  			$sql .= ' ,value = :value';
  			$sql .= ' ,nacquisition = :nb_acqui';
  			$sql .= ' ,age_begin = :age_begin';
  			$sql .= ' ,age_end = :age_end';
  			$sql .= ' ,weight_begin = :weight_begin';
  			$sql .= ' ,weight_end = :weight_end';
  			$sql .= ' where nrd_id =  :id;';

  			$stmt = $pdo->prepare ( $sql );

  			$stmt->bindParam ( ':name',		 		$name 			);
  			$stmt->bindParam ( ':type',		 		$type 			);
  			$stmt->bindParam ( ':valuetype', 		$valuetype		);
  			$stmt->bindParam ( ':value',	 		$value 			);
  			$stmt->bindParam ( ':nb_acqui', 		$nb_acqui		);
  			$stmt->bindParam ( ':age_begin', 		$age_begin		);
  			$stmt->bindParam ( ':age_end',	 		$age_end		);
  			$stmt->bindParam ( ':weight_begin',		$weight_begin	);
  			$stmt->bindParam ( ':weight_end',		$weight_end	);
  			$stmt->bindParam ( ':id',				$id		);
  		}

		$stmt->execute ();
		if ( $id == -1 )	// Insert
		{
			if ( isset ( $_POST['link_id'] )		)	$link_id	 	= $_POST['link_id'];

			$response = $stmt->fetchColumn ();

			$sql = "insert into link_nrd(type,link_id,nrd_id) values (1,:link_id,:nrd_id) returning nrd_id;";
			$stmt = $pdo->prepare ( $sql );
			$stmt->bindParam ( ':link_id', 	$link_id	);
			$stmt->bindParam ( ':nrd_id', 	$response 	);

			$stmt->execute ();

			$response = $stmt->fetchColumn ();
		}
		else
			$response = $id;
	}
	
	header ('Content-type: text/xml' );
	
	echo $response;
?>