<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ page import="com.eactive.eai.rms.common.login.SessionManager"%>
<%@ page import="com.eactive.eai.common.util.SystemUtil"%>
<%
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setHeader("Expires", "0");
%>

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<jsp:include page="/jsp/common/include/css.jsp"/>
<jsp:include page="/jsp/common/include/script.jsp"/>

<script language="javascript" >

var isDetail = false;

var roleString	= "<%=SessionManager.getRoleIdString(request)%>";
var devSvr		= <%=SystemUtil.isDevServer()%>;
var prdSvr		= <%=SystemUtil.isProdServer()%>;
var stgSvr		= <%=SystemUtil.isStagingServer()%>;
var svrType		= '';

function isValid(){
 	if($('input[name=bjobBzwkPrcssDstcd]').val().trim() == ""){
		alert("작업업무처리구분코드를 입력하여 주십시요.");
		return false;
	}else if($('input[name=bjobStgeDstcd]').val().trim() == ""){
		alert("작업단계구분코드를 입력하여 주십시요.");
		return false;
	}
		
	return true;
}

function unformatterFunction(cellvalue, options, rowObject){
	return "";
}

function init(url,key,key2,callback){
	$.ajax({
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'LIST_DETAIL_COMBO'},
		success:function(json){
			new makeOptions("CODE","CODE").setObj($("select[name=telgmClsID]")).setNoValueInclude(true).setData(json.clsIDList).rendering();
			setSearchable("telgmClsID");
			if (typeof callback === 'function') {
				callback(url,key,key2);
			}
		},
		error:function(e){
			alert(e.responseText);
		}
	});
}
function detail(url,key,key2){
	if (!isDetail)return;
	$.ajax({  
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'DETAIL'
		    , key : key
		    , key2 : key2
		    },
		success:function(json){
			var data = json;
			var detail = json.list;
			//adapterType
			$("input[name=bjobBzwkPrcssDstcd]").attr('readonly',true);
			$("input[name=bjobStgeDstcd]").attr('readonly',true);
			$("input,select").each(function(){
				if($(this).attr('name')!=undefined){
					var name = $(this).attr('name').toUpperCase();
					if ( name != null )
					$(this).val(detail[name]);
				}
			});
			
		},
		error:function(e){
			alert(e.responseText);
		}
	});

}
$(document).ready(function() {	
	var returnUrl = getReturnUrlForReturn();
	var url ='<c:url value="/bap/admin/message/flowStepMan.json" />';
	var key ="${param.key}";
	var key2 ="${param.key2}";
	
	if (key != "" && key !="null"){
		isDetail = true;
	}	

	if(devSvr) {
		svrType = "stg";
	} else if (stgSvr){
		svrType = "prod";
	} else if (!prdSvr){
		svrType = "dev";
	}
	
	init(url,key,key2,detail);

	$("#btn_modify").click(function(){
		if (!isValid())return;

		if (isDetail) {
			if ( confirm( "수정하시겠습니까?" ) != true ) return;
		} else {
			if ( confirm( "등록하시겠습니까?" ) != true ) return;
		}
		
		//공통부만 form으로 구성
		var postData = $('#ajaxForm').serializeArray();

		if (isDetail){
			postData.push({ name: "cmd" , value:"UPDATE"});
		}else{
			postData.push({ name: "cmd" , value:"INSERT"});
		}
		$.ajax({
			type : "POST",
			url:url,
			data:postData,
			success:function(args){
				alert("저장 되었습니다.");
				goNav(returnUrl);//LIST로 이동
			},
			error:function(e){
				alert(e.responseText);
			}
		});
	});
	$("#btn_delete").click(function(){
		if ( confirm( "삭제하시겠습니까?" ) != true ) return;
		
		var postData = $('#ajaxForm').serializeArray();
		postData.push({ name: "cmd" , value:"DELETE"});
		$.ajax({
			type : "POST",
			url:url,
			data:postData,
			success:function(args){
				alert("삭제 되었습니다.");
				goNav(returnUrl);//LIST로 이동

			},
			error:function(e){
				alert(e.responseText);
			}
		});
	});	
	$("#btn_previous").click(function(){
		goNav(returnUrl);//LIST로 이동
	});
	
	$("img[id=btn_clone_"+svrType+"]").click(function() {
		
		if ( confirm( "복사하시겠습니까?" ) != true ) return;
		
		var bjobBzwkPrcssDstcdList = [];
		var bjobStgeDstcdList = [];
		bjobBzwkPrcssDstcdList.push(key);
		bjobStgeDstcdList.push(key2);
		
		var postData = $('#ajaxForm').serializeArray();
		postData.push({
			name : "cmd",
			value : "LIST_DOWNLOAD_SQL"
		});
		postData.push({
			name : "searchBjobBzwkPrcssDstcdList",
			value : bjobBzwkPrcssDstcdList
		});
		postData.push({
			name : "searchBjobStgeDstcdList",
			value : bjobStgeDstcdList
		});			
		postData.push({
			name : "svrType",
			value : svrType
		});
		
		$.ajax({
			type : "POST",
			url : url,
			data : postData,
			success : function(args) {
 				if (args.result != "" && args.error == "") {
					alert("정상 반영되었습니다.");
				} else if (args.error != "") {
					if ("${fn:contains(args.error,'SqlIntegrityConstraintViolationException')}") {
						alert("이미 존재하여 정상 반영되지 않었습니다.");
					} else {
						alert("오류가 발생하여 정상 반영되지 않았습니다. 오류 : "+args.error);
					}
				}
 				goNav(returnUrl);
			},
			error : function(e) {
				alert(e.responseText);
			}
		});
		
	});
	
	buttonControl(isDetail);
	titleControl(isDetail);
		
	if(devSvr) {
		setBtnHide(roleString, " ", "btn_clone_dev");
		setBtnHide(roleString, " ", "btn_clone_prod");
		setBtnHide(roleString, isDetail ? "admin" : " ", "btn_clone_stg");
	} else if (stgSvr){
		setBtnHide(roleString, " ", "btn_clone_dev");
		setBtnHide(roleString, " ", "btn_clone_stg");
		setBtnHide(roleString, isDetail ? "admin" : " ", "btn_clone_prod");
	} else if (prdSvr){
		setBtnHide(roleString, " ", "btn_clone_stg");
		setBtnHide(roleString, " ", "btn_clone_prod");		
		setBtnHide(roleString, " ", "btn_clone_dev");
	} else {
		setBtnHide(roleString, " ", "btn_clone_stg");
		setBtnHide(roleString, " ", "btn_clone_prod");		
		setBtnHide(roleString, isDetail ? "admin" : " ", "btn_clone_dev");
	}
});
 
</script>
</head>
	<body>
		<div class="right_box">
			<div class="content_top">
				<ul class="path">
					<li><a href="#">${rmsMenuPath}</a></li>					
				</ul>					
			</div><!-- end content_top -->
			<div class="content_middle">
				<div class="search_wrap">
<%-- 				<img src="<c:url value="/img/btn_clone.png"/>" alt="" id="btn_clone_stg" level="W" status="DETAIL"/>
					<img src="<c:url value="/img/btn_clone_prod.png"/>" alt="" id="btn_clone_prod" level="W" status="DETAIL"/>
					<img src="<c:url value="/img/btn_clone_dev.png"/>" alt="" id="btn_clone_dev" level="W" status="DETAIL"/> --%>						
					<img src="<c:url value="/img/btn_modify.png"/>" alt="" id="btn_modify" level="W" status="DETAIL,NEW" />				
					<img src="<c:url value="/img/btn_delete.png"/>" alt="" id="btn_delete" level="W" status="DETAIL"/>
					<img src="<c:url value="/img/btn_previous.png"/>" alt="" id="btn_previous" level="R" status="DETAIL,NEW"/>
				</div>
				<div class="title">흐름단계</div>						
				<form id="ajaxForm">
					<table class="table_row" cellspacing="0">
						<!-- 공통-->
						<tr>
							<th style="width:200px;">BATCH작업업무처리구분코드</th><td><input type="text" name="bjobBzwkPrcssDstcd" /> </td>
						</tr>
						<tr>
							<th>BATCH작업단계구분코드</th><td><input type="text" name="bjobStgeDstcd"> </td>
						</tr>
						<tr>
							<th>BATCH작업단계유형명</th>
							<td>
								<div class="select-style"><select type="text" name="bjobStgePtrnName">
									<option value="SEND">SEND</option>
									<option value="RECV">RECV</option>
									<option value="END">END</option>
									<option value="EEND">EEND</option>
								</select></div>
							 </td>
						</tr>		
						<tr>
							<th>BATCH작업흐름컴포넌트명</th><td><input type="text" name="bjobFlowCmpoName"></td>
						</tr>	
						<tr>
							<th>전문클래스ID</th><td><div class="select-style"><select type="text" name="telgmClsID"></select></div> </td>
						</tr>	
						<tr>
							<th>단위전문길이</th><td><input type="text" name="unitTelgmLen"></td>
						</tr>	
						<tr>
							<th>BATCH작업노드설명</th><td><input type="text" name="bjobNodeDesc"></td>
						</tr>			
					</table>
				</form>

				<!-- grid -->
				<table id="grid" ></table>
				

	
				
			</div><!-- end content_middle -->
		</div><!-- end right_box -->	
	</body>
</html>

