<?php 
$form_name = "Lab Test Master";
$table_name = "custom_lab_master";
$primary_key = "lab_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":
		if(isset($_REQUEST['id']) && $_REQUEST['id'] != '')
		{
			$qry = "SELECT * FROM $table_name WHERE $primary_key = '".$_REQUEST['id']."'";
			$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			$cnt = count($rec);
		}
		else
			$cnt = 0;
		
        $spot_test = isset($_REQUEST['spot_test']) ? $_REQUEST['spot_test'] : 'N';
        $status = isset($_REQUEST['status']) ? $_REQUEST['status'] : 'N';
		
		if($cnt == 0)
		{
			$_query = sprintf("insert into $table_name values(0, '%s', '%s', '%s', '%s', '%s', 'N', now(), '%s', 0, 0)",
				mysql_escape_string($_REQUEST['dept_id']),
				mysql_escape_string($_REQUEST['test_code']),
				mysql_escape_string($_REQUEST['test_name']),
				mysql_escape_string($spot_test),
				mysql_escape_string($status),
				mysql_escape_string(USR)
			);
			$rec = $common->sql->query($_query);
            $last_id = mysql_insert_id();
			$_SESSION['msg'] = 'Lab Test Master has been Created Successfully';
			$common->UserLog($form_name, $table_name, $last_id, 'Insert');
		}
		else
		{
			$_query = "UPDATE $table_name   SET 	lab_dept_code   = '".$_REQUEST['dept_id']."',
													lab_test_code   = '".$_REQUEST['test_code']."',
													lab_test_name   = '".$_REQUEST['test_name']."',
													lab_spot_test   = '".$spot_test."',
													status		    = '".$status."',
													update_time     = now(),
													update_user     = '".USR."'
											WHERE   $primary_key	= '".$_REQUEST['id']."'";
			$rec = $common->sql->exeQuery($_query);
			$_SESSION['msg'] = 'Lab Test Master has been Updated Successfully';
			$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Update');
		}
		
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		header("location:".$link);
		exit;
	break;
	
	case "edit":
		$query = "Select * FROM $table_name WHERE $primary_key = '".$_REQUEST['id']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
                
		$id = $rec[0][$primary_key];
		$dept_id = $rec[0]['lab_dept_code'];
		$test_code = $rec[0]['lab_test_code'];
		$test_name = $rec[0]['lab_test_name'];
		$spot_test = $rec[0]['lab_spot_test'];
		$status = $rec[0]['status'];
		
		if($status == "Y")
			$status_chk = "checked";
		else
			$status_chk = "";
		
		if($spot_test == "Y")
			$spot_test_chk = "checked";
		else
			$spot_test_chk = "";
		
		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 $primary_key IN ('".$id."')";
		
		$query = "UPDATE ".$table_name." SET del_status = 'Y', status = 'N' WHERE $primary_key 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;
}
?>