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

	$result = array (
			"status"	=> 'KO',
			"results"	=> -1
	);
	
	$pdo = getPDO ();
		
	if ( $pdo )
	{
		$dicom_id				= -1;
		$group_id				= -1;
		$modalitytitle 			= "";
		$sopuid		 			= "";
				
		if ( isset($_POST['dicom_id']) 		)		$dicom_id 		= $_POST['dicom_id'];
		if ( isset($_POST['group_id']) 		)		$group_id 		= $_POST['group_id'];
		if ( isset($_POST['modalitytitle']) )		$moalitytitle	= $_POST['modalitytitle'];
		if ( isset($_POST['sopuid']) 		)		$sopuid			= $_POST['sopuid'];

		$sql = "insert into info_to_extract_init(tag,group_id,condition,modalitytitle,sopuid,extract_order,description,unit,parse_function,cvt_factor,match_id)
          select tag,'".$group_id."',condition, '".$modalitytitle."','".$sopuid."',coalesce((select max(extract_order)+1 from info_to_extract_init where modalitytitle = '".$modalitytitle."' and sopuid = '".$sopuid."' and match_id = (select match_id from dicom_tag where dicom_id = ".$dicom_id.")),0),description,'','',null,match_id from dicom_tag where dicom_id = ".$dicom_id." returning init_id;";

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

		if ( $s0->execute () == true )
		{
			$result['status'] = 'OK';
			$result['results'] = $s0->fetchColumn ();
		}
	}
	
	header('Content-type: application/json');
	
	echo json_encode ( $result );
?>