<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ include file="/jsp/common/include/localemessage.jsp" %>
<%
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setHeader("Expires", "0");
	String rmsMenuAuth = (String)request.getAttribute("rmsMenuAuth");
%>

<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 url      = '<c:url value="/onl/admin/service/b2bServiceMan.json" />';
var url_view = '<c:url value="/onl/admin/service/b2bServiceMan.view" />';
var selectName = "psvSysBzwkDstcd,outbndRoutName,psvSysAdptrBzwkGroupName";	// selectBox Name

	var isDetail = false; 
    var isClone = false;	
	function isValid() {

		var extnlInstiIdnfiName = $('input[name=extnlInstiIdnfiName]'); 
		
		if ( extnlInstiIdnfiName.val() == "") {
			alert("<%= localeMessage.getString("b2bService.alert")%>");
			extnlInstiIdnfiName.focus();
			return false;
		}
		var svcPrcssNo = $('select[name=svcPrcssNo]'); 
		if ( svcPrcssNo.val() == "") {
			alert("<%= localeMessage.getString("b2bService.alert2")%>");
			svcPrcssNo.focus();
			return false;
		}		

		return true;
	}
	function isValidClone() {
		var extnlInstiIdnfiName = $('input[name=newExtnlInstiIdnfiName]'); 

		if (extnlInstiIdnfiName.val().length>8){
			alert("기관코드를 8자리이하로 입력해주세요");
			extnlInstiIdnfiName.focus();
			return false;
		}
		if ( extnlInstiIdnfiName.val() == "") {
			alert("<%= localeMessage.getString("b2bService.alert")%>");
			extnlInstiIdnfiName.focus();
			return false;
		}
		var svcPrcssNo = $('select[name=newSvcPrcssNo]'); 
		if ( svcPrcssNo.val() == "") {
			alert("<%= localeMessage.getString("b2bService.alert2")%>");
			svcPrcssNo.focus();
			return false;
		}	

		return true;
	}	

	function init( key,key2, callback) {
		$.ajax({  
			type : "POST",
			url:url,
			dataType:"json",
			data:{cmd: 'LIST_DETAIL_COMBO'},
			success:function(json){
				new makeOptions("CODE","NAME").setObj($("select[name=psvIntfacDsticName]" )).setData(json.psvItfTypeRows        ).rendering();//수동 인터페이스 유형
				new makeOptions("CODE","NAME").setObj($("select[name=psvSysBzwkDstcd]"    )).setData(json.bizRows               ).setFormat(codeName3OptionFormat).rendering();//수동업무구분
				//new makeOptions("CODE","NAME").setObj($("select[name=psvSysIdName]"       )).setData(json.psvIdRows             ).rendering();//수동시스템ID
				new makeOptions("CODE","NAME").setObj($("select[name=flovrYn]"            )).setData(json.failOverClsRows       ).setFormat(codeName3OptionFormat).rendering();//FailOver여부
				new makeOptions("CODE","NAME").setObj($("select[name=chngEonot]"          )).setData(json.cnvExistNotRows       ).setFormat(codeName3OptionFormat).rendering();//변환유무
				new makeOptions("CODE","NAME").setObj($("select[name=bascRspnsChngEonot]" )).setData(json.bsRspCnvExistNotRows  ).setFormat(codeName3OptionFormat).rendering();//기본응답변환유무
				new makeOptions("CODE","NAME").setObj($("select[name=errRspnsChngEonot]"  )).setData(json.errRspCnvExistNotRows ).setFormat(codeName3OptionFormat).rendering();//오류응답변환유무
				new makeOptions("CODE","NAME").setObj($("select[name=outbndRoutName]"     )).setData(json.outBoundRoutingRows   ).rendering();//Outbound라우팅명
				new makeOptions("CODE","NAME").setObj($("select[name=supplDelYn]"         )).setData(json.addDelYnRows          ).setFormat(codeName3OptionFormat).rendering();//추가삭제여부
				new makeOptions("CODE","NAME").setObj($("select[name=hdrCtrlDstcd]"       )).setData(json.hdrCtlCdRows          ).setFormat(codeName3OptionFormat).rendering();//헤더제어구분코드
				new makeOptions("ADPTRBZWKGROUPNAME","ADPTRBZWKGROUPDESC").setObj($("select[name=psvSysAdptrBzwkGroupName]")).setNoValueInclude(true).setNoValue("", "선택안함").setData(json.ouAdapterRows).setFormat(codeName3OptionFormat).rendering();//수동시스템인터페이스유형
				
				new makeOptions("CODE","NAME").setObj($("select[name=newSvcPrcssNo]"      )).setNoValueInclude(true).setNoValue("", "서비스처리순서").setData(json.svcPrcssNo          	).rendering();//서비스 처리 순서
				new makeOptions("CODE","NAME").setObj($("select[name=svcPrcssNo]"         )).setData(json.svcPrcssNo          	).rendering();//서비스 처리 순서
				
				//EAI서버구분코드
				new makeOptions("CODE","NAME").setObj($("select[name=eaiSevrDstCd]")).setData(json.inoutTypeRows).setFormat(codeName3OptionFormat).rendering();
				new makeOptions("CODE","NAME").setObj($("select[name=useYn]")).setData(json.useYnRows).setFormat(codeName3OptionFormat).rendering();
				
				if(key == "") setSearchable(selectName);	// 콤보에 searchable 설정
				
				if(typeof callback === 'function') {
					callback(key,key2);
	    		}
			},
			error:function(e){
				alert(e.responseText);
			}
		});
	
	}
	function detail( key,key2) {

		if (!isDetail)
			return;
		$.ajax({
			type : "POST",
			url : url,
			dataType : "json",
			data : {
				cmd : 'DETAIL',
				extnlInstiIdnfiName : key,
				svcPrcssNo : key2
			},
			success : function(json) {
				//debugger;
				var data = json;
				$("input[name=extnlInstiIdnfiName]").attr('readonly', true);
				$("select[name=svcPrcssNo]").prop('disabled',true);
				
				$("#ajaxForm input[type!=radio],#ajaxForm select,#ajaxForm textarea").each(function(){
					var name = $(this).attr("name");
					var tag  = $(this).prop("tagName").toLowerCase();
					$(tag+"[name="+name+"]").val(data[name.toUpperCase()]);
				});
				//$("select[name=svcPrcssNo] option").not(":selected").remove();

				setSearchable(selectName);	// 콤보에 searchable 설정
			},
			error : function(e) {
				alert(e.responseText);
			}
		});

	}
	$(document).ready(function() {
		var returnUrl = getReturnUrlForReturn();
		var key = "${param.extnlInstiIdnfiName}";
		var key2 = "${param.svcPrcssNo}";

		if (key != "" && key != "null") {
			isDetail = true;
		}
		init(key,key2, detail);
		
		$("input[name=adptrNickName]").attr('readonly', true);

		$("#btn_modify").click(function() {
			if (!isValid()){
				return;
			}
		
			var disabled = $('#ajaxForm').find(':input:disabled').removeAttr('disabled');
			var postData = $('#ajaxForm').serializeArray();
			disabled.attr('disabled','disabled');
			
			//EAI서버구분코드
			if($("select[name=eaiSevrDstCd]").val() == null){
				postData.push({ name: "eaiSevrDstCd" , value:""});
			} 
			
			if (isDetail && isClone){
				postData.push({ name: "cmd" , value:"INSERT"});
			} else 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) {
					if (isClone) {
						alert('복제 되었습니다.');
					} else {
						alert("<%= localeMessage.getString("common.saveMsg") %>");
					}
					goNav(returnUrl);//LIST로 이동
				},
				error : function(e) {
					alert(e.responseText);
				}
			});
		});
		$("#btn_delete").click(function() {
			if (confirm("<%= localeMessage.getString("common.confirmMsg")%>") != true)
				return;
			
			var disabled = $('#ajaxForm').find(':input:disabled').removeAttr('disabled');
			var postData = $('#ajaxForm').serializeArray();
			disabled.attr('disabled','disabled');
			
			postData.push({
				name : "cmd",
				value : "DELETE"
			});
			$.ajax({
				type : "POST",
				url : url,
				data : postData,
				success : function(args) {
					alert("<%= localeMessage.getString("common.deleteMsg")%>");
					goNav(returnUrl);//LIST로 이동

				},
				error : function(e) {
					alert(e.responseText);
				}
			});
		});
		$("#btn_previous").click(function() {
			goNav(returnUrl);//LIST로 이동
		});
		$("#btn_initialize").click(function(){
			$.ajax({  
				type : "POST",
				url:url,
				dataType:"json",
				data:{cmd: 'INITIALIZE',extnlInstiIdnfiName:$("input[name=extnlInstiIdnfiName]").val()},
				success:function(json){
					alert("<%= localeMessage.getString("common.successMsg")%>");
				},
				error:function(e){
					alert(e.responseText);
				}
			});		
		});		
		$("#btn_clone").click(function() {
			if (!isValidClone()){
				return;
			}
			alert("복제한 내용을 저장하시려면 수정버튼을 눌러주세요.");
			$("input[name=extnlInstiIdnfiName]").val($("input[name=newExtnlInstiIdnfiName]").val());
			$("input[name=extnlInstiIdnfiName]").attr("readonly",false);
			$("select[name=svcPrcssNo]").val($("select[name=newSvcPrcssNo]").val()).prop("selected",true);
			isClone = true;
		});

		buttonControl(key);
		titleControl(key);
	});
</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">
					<button type="button" class="cssbtn" id="btn_delete" level="W" status="DETAIL"><i class="material-icons">delete</i> <%= localeMessage.getString("button.delete") %></button>
					<button type="button" class="cssbtn" id="btn_modify" level="W" status="DETAIL,NEW"><i class="material-icons">save</i> <%= localeMessage.getString("button.modify") %></button>
					<button type="button" class="cssbtn" id="btn_previous" level="R" status="DETAIL,NEW"><i class="material-icons">arrow_back</i> <%= localeMessage.getString("button.previous") %></button>
<%-- 					<img src="<c:url value="/img/btn_delete.png"/>" alt="" id="btn_delete" 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_previous.png"/>" alt="" id="btn_previous" level="R" status="DETAIL,NEW"/> --%>
				</div>
				<div class="title"><%= localeMessage.getString("b2bService.title2")%></div>	
				<form id="ajaxForm">
					<table class="table_row" cellspacing="0">
						
						<%
						if("R".equals(rmsMenuAuth)) {
						%>
						<tr>
							<th style="width:20%;"><%= localeMessage.getString("b2bService.kikwanCd")%></th>
							<td colspan="3"><input type="text" name="extnlInstiIdnfiName"/> </td>
						</tr>
						<%
						}
						else {
						%>
						<tr>
							<th style="width:20%;"><%= localeMessage.getString("b2bService.kikwanCd")%></th>
							<td style="width:30%;"><input type="text"  name="extnlInstiIdnfiName"/></td>
							<th style="width:20%;"><%= localeMessage.getString("b2bService.cloneCdSvcPrcssNo")%></th>
							<td style="width:30%;">
								<input type="text" name="newExtnlInstiIdnfiName" style="width : 50%" placeholder="기관코드"/>&nbsp;,&nbsp;
								<div class="select-style" style="width : 30%; display: inline-block;">
									<select name="newSvcPrcssNo"></select>
								</div>
<%-- 								<img id="btn_clone" src="<c:url value="/img/btn_clone1.png"/>" level="W" status="DETAIL" style=" display: inline-block;"/> --%>
								<button type="button" class="cssbtn smallBtn2" id="btn_clone" style="vertical-align:middle; font-weight:bold; display: inline-block;" level="W" status="DETAIL"><i class="material-icons">content_copy</i><%= localeMessage.getString("button.clone2") %></button>
							</td>
						</tr>
						<%
						}
						%>
						<tr>
							<th style="width:20%;"><%= localeMessage.getString("b2bService.svcPrcssNo")%></th>
							<td style="width:30%;">
								<div class="select-style">
								<select name="svcPrcssNo" />
								</div>
							</td>
							<th style="width:20%;"><%= localeMessage.getString("b2bService.psvIntfaCdSticName")%></th>
							<td style="width:30%;">
								<div class="select-style">
								<select name="psvIntfacDsticName"/> 
								</div>
							</td>
						</tr>
						<tr>
							<th> <%= localeMessage.getString("b2bService.psvSysBzwkDstcd")%></th>
							<td>
								<div class="select-style">
								<select name="psvSysBzwkDstcd"/> 
								</div>
							</td>
							<%-- <th><%= localeMessage.getString("b2bService.psvSysIdName")%></th>
							<td>
								<div class="select-style">
								<select name="psvSysIdName"/> 
								</div>
							</td> --%>
							<th><%= localeMessage.getString("b2bService.psvSysSvcDsticName")%></th>
							<td><input type="text" name="psvSysSvcDsticName"/> </td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("b2bService.psvSysAdptBzwkGroupName")%></th>
							<td>
								<div class="select-style">
								<select name="psvSysAdptrBzwkGroupName"/> 
								</div>
							</td>
							<th><%= localeMessage.getString("b2bService.flOvrYn")%></th>
							<td>
								<div class="select-style">
								<select name="flovrYn"/> 
								</div>
							</td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("b2bService.chngEonot")%></th>
							<td>
								<div class="select-style">
								<select name="chngEonot"/> 
								</div>
							</td>
							<th><%= localeMessage.getString("b2bService.chngMsgIdName")%></th>
							<td><input type="text" name="chngMsgIdName"/> </td>
						</tr>
						<tr>
							
							<th><%= localeMessage.getString("b2bService.bascRspnsChngEonot")%></th>
							<td>
								<div class="select-style">
								<select name="bascRspnsChngEonot"/> 
								</div>
							</td>								
							<th><%= localeMessage.getString("b2bService.bascRspnsChngMsgIdName")%></th>
							<td ><input type="text" name="bascRspnsChngMsgIdName"/> </td>				
						</tr>
						<tr>
							<th><%= localeMessage.getString("b2bService.errRspnsChngEonot")%></th>
							<td>
								<div class="select-style">
								<select name="errRspnsChngEonot"/> 
								</div>
							</td>
							<th><%= localeMessage.getString("b2bService.errRspnsChngMsgIdName")%></th>
							<td><input type="text" name="errRspnsChngMsgIdName"/> </td>			
						</tr>
						<tr>
							<th><%= localeMessage.getString("b2bService.bascRspnsMsgCmprCtnt")%></th>
							<td><input type="text" name="bascRspnsMsgCmprCtnt"/> </td>
							<th><%= localeMessage.getString("b2bService.errRspnsMsgCmprCtnt")%></th><td><input type="text" name="errRspnsMsgCmprCtnt"/> </td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("b2bService.nextSvcPrcessNo")%></th>
							<td><input type="text" name="nextSvcPrcssNo" value="0"/> </td>
							<th><%= localeMessage.getString("b2bService.outBndRoutName")%></th>
							<td>
								<div class="select-style">
								<select name="outbndRoutName"/> 
								</div>
							</td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("b2bService.tOutVal")%></th>
							<td><input type="text" name="toutVal" value="0"/> </td>
							<th><%= localeMessage.getString("b2bService.cmpenSvcPrcssDsticName")%></th>
							<td><input type="text" name="cmpenSvcPrcssDsticName"/> </td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("b2bService.supplDelYn")%></th>
							<td>
								<div class="select-style">
								<select name="supplDelYn"/> 
								</div>
							</td>
							<th><%= localeMessage.getString("b2bService.hdrCtrlDstcd")%></th>
							<td>
								<div class="select-style">
								<select name="hdrCtrlDstcd"/> 
								</div>
							</td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("b2bService.hdrRefClsName")%></th>
							<td><input type="text" name="hdrRefClsName"/> </td>
							<th><%=localeMessage.getString("b2bService.eaisevrdstcd")%></th>
							<td >
								<div class="select-style">
									<select name="eaiSevrDstCd"></select>
								</div>
							</td>
						</tr>
						<tr>
							<th><%=localeMessage.getString("b2bService.useYn")%></th>
							<td >
								<div class="select-style">
									<select name="useYn"></select>
								</div>
							</td>
							<th><%=localeMessage.getString("b2bService.adptrNickName")%></th>
							<td><input type="text" name="adptrNickName"/> </td>
						</tr>
					</table>
				</form>
				
			</div><!-- end content_middle -->
		</div><!-- end right_box -->	
	</body>
</html>