<?php 
$form_name = "Lab Test Mapping";
$table_name = "custom_lab_mapping";
$primary_key = "lab_mapng_id";

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
	
include_once("includes/config.php");
include_once("includes/common_functions.php");

$common = new General_Functions();
$css_class = "x-gray";
// $common->SetCssClass($css_class);

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
$act_type = isset($_REQUEST['act_type']) ? $_REQUEST['act_type'] : "";
$cancel_link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
$action = $_SERVER['SCRIPT_NAME']."?page=".$_REQUEST['page']."&mode=submit";

switch($mode)
{
	case "submit":
		$lab_mstr = $_REQUEST['lab_mstr'];
		$lab_spcmn = $_REQUEST['spcmn_id'];

		if($_REQUEST['id'] != "")
		{
			$qry = "DELETE FROM ".$table_name." WHERE lab_mapng_sur_id = '".$_REQUEST['survey_id']."'";
			$rec = $common->sql->exeQuery($qry);
		}
		
		for($i=0; $i<count($lab_spcmn); $i++)
		{
			$qry = "SELECT * FROM ".$table_name." WHERE lab_mapng_sur_id = '".$_REQUEST['survey_id']."' 
				AND lab_mapng_mstr_id = '".$lab_mstr[$i]."' AND lab_mapng_spcmn_id = '".$lab_spcmn[$i]."'";
			$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			
			if(count($rec) == 0)
			{
				$_query = sprintf("insert into ".$table_name." values(0, '%s', '%s', '%s', 'Y', now(), '%s', 0, 0)",
					mysql_escape_string($_REQUEST['survey_id']),
					mysql_escape_string($lab_mstr[$i]),
					mysql_escape_string($lab_spcmn[$i]),
					mysql_escape_string(USR)
				);
				$exe_rec = $common->sql->query($_query);
				$last_id = mysql_insert_id();
			}
		}
		
		if($exe_rec)
		{
			if($_REQUEST['id'] != "")
			{
				$_SESSION['msg'] = 'Lab Test Mapping has been updated Successfully';
				$common->UserLog($form_name, $table_name, $_REQUEST['survey_id'], 'Update');
			}
			else
			{
				$_SESSION['msg'] = 'Lab Test Mapping has been saved Successfully';
				$common->UserLog($form_name, $table_name, $_REQUEST['survey_id'], 'Insert');
			}
		}
		
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		header("location:".$link);
	break;
	
	case "edit":
		$query = "Select * FROM ".$table_name." WHERE lab_mapng_sur_id = '".$_REQUEST['id']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		
		$id = $_REQUEST['id'];
		$survey_id = $rec[0]['lab_mapng_sur_id'];
		include("edit.php");
	break;
	
	case "delete":
		if(isset($_REQUEST[$prefix.'id']))
			$id = $_REQUEST[$prefix.'id'];
		else
			$id = implode("', '", $_REQUEST[$primary_key]);

		$query = "DELETE FROM ".$table_name." WHERE lab_mapng_sur_id IN ('".$id."')";

		$rec = $common->sql->exeQuery($query);

		if($rec)
		{
			$_SESSION['msg'] = 'Selected record has been deleted successfully';
			if(isset($_REQUEST['id']))
				$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Delete');
			else
				for($i=0; $i<count($_REQUEST[$primary_key]); $i++)
					$common->UserLog($form_name, $table_name, $_REQUEST[$primary_key][$i], 'Delete');
		}

		$query_string = str_replace("&mode=delete","",$_SERVER['QUERY_STRING']);
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		header("location:".$link);
	break;
	
	default:
		echo '<div class="content_bg">';
			include("add.php");
		echo '</div>';
	break;
}
?>