<%@ 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");
%>

<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/fieldExtractInfoMan.json" />';
var url_view = '<c:url value="/onl/admin/service/fieldExtractInfoMan.view" />';

	var isDetail = false;
	function isValid() {

		var eaiSvcName = $('input[name=eaiSvcName]'); 
		if ( eaiSvcName.val() == "") {
			alert("<%= localeMessage.getString("fieldExtract.alert1")%>");
			eaiSvcName.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=etractDstcd]")).setData(json.exrClassTypeList).setFormat(codeName3OptionFormat).rendering();
				new makeOptions("CODE","NAME").setObj($("select[name=msgDstcd]")).setData(json.msgTypeList).setFormat(codeName3OptionFormat).rendering();
				
				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',
				eaiSvcName : key,
				etractDstcd : key2
				
			},
			success : function(json) {
				var data = json.detailList;
				var ref  = json.refList;
				
				$("#ajaxForm input[type!=radio],#ajaxForm select,#ajaxForm textarea").each(function(){
					var name = $(this).attr("name");
					var tag  = $(this).prop("tagName").toLowerCase();
					if ($(tag+"[name="+name+"]").length == 1){
						if (!$.isNull(data[0][name.toUpperCase()])){
							$(tag+"[name="+name+"]").val(data[0][name.toUpperCase()]);
						}
					}
				});
				$("#ajaxForm input[type=text]").each(function(){
					var name = $(this).attr("name");
					var tag  = $(this).prop("tagName").toLowerCase();
					
					if ($(tag+"[name="+name+"]").length > 1){
						$(tag+"[name="+name+"]").each(function(index){
							if (data.length > index ){
								$(tag+"[name="+name+"]").eq(index).val(data[index][name.toUpperCase()]);
							}
						});
					}
					
				});
				if (!$.isNull(ref[0]) && !$.isNull(ref[0]["REFMSGIDNAME"])){
					$("input[name=loutName]").val(ref[0]["REFMSGIDNAME"]);
					$("input[name=loutDesc]").val(ref[0]["LOUTDESC"]);
				}
				$("input[name=loutName]").change();

            if (Array.isArray(data)) {
               data.forEach(item => {
                  $("#sub4_3_2_detail_" + item['FLDPRCSSNO']).attr("checked", "checked");
               });
            }
				
				//셋팅후 disable해야됨
				$("select[name=etractDstcd] option").not(":selected").remove();
			},
			error : function(e) {
				alert(e.responseText);
			}
		});

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

		if (key != "" && key != "null") {
			$(".cls_btn_td, .cls_loutName_btn_td").remove();
			$(".cls_eaiSvcName_input_td, .cls_loutName_input_td").attr({"colspan":"2", "width":"95%"});
			$(".cls_eaiSvcName_table, .cls_loutName_table").attr("width", "100%");
			
			$("input[name=eaiSvcName],[name=loutName]").attr("readOnly", true);
			$("input[name=eaiSvcName],[name=loutName]").css({"background-color":"#E6E6E6", "width":"95%"});
			
			isDetail = true;
		}
		
		init(key,key2, detail);
		$('#tree').jqGrid({
		    datatype:'json',
		    loadui: "disable",
		    mtype: 'POST',
		    colNames: [
		    		"<%= localeMessage.getString("fieldExtract.num")%>",
		    		"<%= localeMessage.getString("fieldExtract.itemNameEng")%>",
		    		"<%= localeMessage.getString("fieldExtract.itemDesc")%>",
		    		"<%= localeMessage.getString("fieldExtract.maxOccCount")%>",
		    		"<%= localeMessage.getString("fieldExtract.refInfo")%>",
		    		"<%= localeMessage.getString("fieldExtract.dataType")%>",
		    		"<%= localeMessage.getString("fieldExtract.dataLength")%>",
		    		"<%= localeMessage.getString("fieldExtract.decpLength")%>",
		    		"<%= localeMessage.getString("fieldExtract.layoutName")%>",
		    		"path"
		    		],
		    colModel: [
		        {name: "LOUTITEMSERNO"          , width:"30"},
		        {name: "LOUTITEMNAME"           , width:"250"},
		        {name: "LOUTITEMDESC"                      },
		        {name: "LOUTITEMMAXOCCURNOITM"  },
		        {name: "LOUTITEMOCCREF"       },
		        
		        {name: "LOUTITEMDATATYPE" , width:"60"},
		        {name: "LOUTITEMLENCNT"         , width:"60"},
		        {name: "LOUTITEMDECIMAL"            , width:"60"},
		        
		        {name: "LOUTNAME"               , hidden:true},
		        {name: "LOUTITEMPATH"           , hidden:true}
		        
				],
		    treeGrid: true,
		    treeGridModel: "adjacency",
		    ExpandColumn: "LOUTITEMNAME",
		    height:"290",
		    rowNum: 10000,
		    autowidth : true,
		    treeIcons: {leaf:'ui-icon-document-b'},
		    jsonReader: {
		        repeatitems: false
		    },
		    loadComplete:function (d){
		    },
	        loadError: function (jqXHR, textStatus, errorThrown) {
	        	if(jqXHR.status == 401) {
	        		var location ='<%=request.getContextPath()%>/';
		        	comloadError(jqXHR, textStatus, errorThrown ,location);
		    	}
	    	},	    
			onSelectRow : function(rowId) {
				var rowData = $(this).getRowData(rowId); 
				$("input[name=selName]").each(function(index){
					if ($("input[name=selName]").eq(index).is(":checked")){
						//var value = rowData["LOUTITEMPATH"].replace(rowData["LOUTNAME"]+".","");
						var value = rowData["LOUTITEMPATH"];
						$("input[name=bzwkFldName]").eq(index).val(value);
					}
				});
				
			
			}
		});			

		$("#btn_modify").click(function() {
			if (!isValid()){
				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("<%= localeMessage.getString("common.saveMsg")%>");
					goNav(returnUrl);//LIST로 이동
				},
				error : function(e) {
					alert(e.responseText);
				}
			});
		});
		$("#btn_delete").click(function() {
			if(confirm("<%= localeMessage.getString("common.confirmMsg")%>")){ 
				var postData = $('#ajaxForm').serializeArray();
				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);
					}
				});
			}else{
				return false;
			}
		});
		$("#btn_previous").click(function() {
			goNav(returnUrl);//LIST로 이동
		});
		$("input[name=loutName]").change(function(){
			var postData = getSearchForJqgrid("cmd","DETAIL_LAYOUT"); //jqgrid에서는 object 로
			postData["loutName"] = $("input[name=loutName]").val();
			$("#tree").setGridParam({ url:url,postData: postData ,datatype:'json' }).trigger("reloadGrid");
		});
		
		$("input[name=selName]").click(function() {
			if ($(this).is(":checked")){
				$("input[name=selName]").removeAttr("checked");
				$(this).prop("checked",true);
			}else{
				$("input[name=selName]").removeAttr("checked");
			}
		});
		$("#btn_initialize").click(function(){
			$.ajax({  
				type : "POST",
				url:url,
				dataType:"json",
				data:{cmd: 'INITIALIZE',eaiSvcName : $("input[name=eaiSvcName]").val()},
				success:function(json){
					alert("<%= localeMessage.getString("common.successMsg")%>");
				},
				error:function(e){
					alert(e.responseText);
				}
			});		
		});
		
		$("#btn_search").click(function(){
			var key = "";
			var args = new Object();
	    	args['eaiSvcName'] = $('input[name=eaiSvcName]').val();
		    var interfaceUrl='<c:url value="/onl/transaction/extnl/interfaceMan.view"/>';
		    interfaceUrl = interfaceUrl + "?cmd=POPUP";
		    var ret = showModal(interfaceUrl,args,1020,678, function(arg){
		    	var args = null;
		        if(arg == null || arg == undefined ) {//chrome
		            args = this.dialogArguments;
		            args.returnValue = this.returnValue;
		        } else {//ie
		            args = arg;
		        }
		        
		        if( !args || !args.returnValue || !args.returnValue.key ) return;
		       	var ret = args.returnValue;
				
				key = ret['key'];
				
				$("input[name=eaiSvcName]").val(key);
				$("input[name=eaiSvcDesc]").val(ret['eaiSvcDesc']);
				
				searchLayout();
		    });
			
			if( ret == null) return;
			
			key = ret['key'];
			
			$("input[name=eaiSvcName]").val(key);
			$("input[name=eaiSvcDesc]").val(ret['eaiSvcDesc']);
			
			searchLayout();
		});
		$("#btn_search_loutName").click(function(){
			var key = "";
			var args = new Object();
	    	args['loutName'] = $('input[name=loutName]').val();
		    var layoutUrl='<c:url value="/onl/admin/rule/layoutMan.view"/>';
		    layoutUrl = layoutUrl + "?cmd=POPUP";
		    var ret = showModal(layoutUrl,args,1020,708,function(arg){
		    	var args = null;
		        if(arg == null || arg == undefined ) {//chrome
		            args = this.dialogArguments;
		            args.returnValue = this.returnValue;
		        } else {//ie
		            args = arg;
		        }
		        
		        if( !args || !args.returnValue || !args.returnValue.key ) return;
		       	var ret = args.returnValue;
				
				key = ret['key'];
				
				$("input[name=loutName]").val(key);
				$("input[name=loutDesc]").val(ret['loutDesc']);
				
				$("input[name=loutName]").change();
		    });
		});
		
		$("input[name=eaiSvcName],[name=loutName]").keydown(function(key){
			if (key.keyCode == 13){
				if($(this).attr("name") == "eaiSvcName") $("#btn_search").click();
				if($(this).attr("name") == "loutName") $("#btn_search_loutName").click();
			}
		});
		
		buttonControl(isDetail);
		titleControl(isDetail);
	});
	
	function searchLayout()
	{
		var postData = $('#ajaxForm').serializeArray();
		postData.push({
			name : "cmd",
			value : "LIST_REF"
		});
		$.ajax({
			type : "POST",
			url : url,
			data : postData,
			success : function(json) {
				var ref = json.refList;
				
				if (!$.isNull(ref[0]) && !$.isNull(ref[0]["REFMSGIDNAME"])){
					$("input[name=loutName]").val(ref[0]["REFMSGIDNAME"]);
					$("input[name=loutDesc]").val(ref[0]["LOUTDESC"]);
					$("input[name=loutName]").change();
				}else{
					$("input[name=loutName]").val("");
					$("input[name=loutDesc]").val("");
					$('#tree').jqGrid('clearGridData');
				}
				
				
			},
			error : function(e) {
				alert(e.responseText);
			}
		});
	}
</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("fieldExtract.detailTitle")%></div>	
				
				<form id="ajaxForm">
					<table class="table_row" cellspacing="0">
						<tr>
							<th style="width:150px;"><%= localeMessage.getString("fieldExtract.eaiSvcName")%></th>
							<td>
								<input type="text" name="eaiSvcName" style="width:95%;"> 
<%-- 								<img id="btn_search" src="<c:url value="/img/btn_pop_search.png" />" level="W" status="DETAIL,NEW" class="btn_img" /> --%>
								<button type="button" class="cssbtn smallBtn2" id="btn_search" style="vertical-align:middle; font-weight:bold;" level="W" status="DETAIL,NEW"><i class="material-icons">search</i><%= localeMessage.getString("button.search") %></button>
							</td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("fieldExtract.eaiSvcDesc")%></th>
							<td>
								<input type="text" name="eaiSvcDesc" readOnly>
							</td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("fieldExtract.etractDstcd")%></th>
							<td>
								<div class="select-style">
								<select name="etractDstcd" ></select>
								</div>
							</td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("fieldExtract.msgDsctcd")%></th>
							<td>
								<div class="select-style">
								<select name="msgDstcd" ></select>
								</div>
							</td>
						</tr>
						<tr>
							<th><label for="sub4_3_2_detail_1"><%= localeMessage.getString("fieldExtract.bzwkFldName")%>1</label> <input type="checkbox" name="selName" id="sub4_3_2_detail_1"/> </th>
							<td>
								<input type="text" name="bzwkFldName" />
							</td>
						</tr>
						<tr>
							<th><label for="sub4_3_2_detail_2"><%= localeMessage.getString("fieldExtract.bzwkFldName")%>2</label> <input type="checkbox" name="selName" id="sub4_3_2_detail_2" /></th>
							<td>
								<input type="text" name="bzwkFldName" />
							</td>
						</tr>
						<tr>
							<th><label for="sub4_3_2_detail_3"><%= localeMessage.getString("fieldExtract.bzwkFldName")%>3</label> <input type="checkbox" name="selName" id="sub4_3_2_detail_3" /></th>
							<td>
								<input type="text" name="bzwkFldName" />
							</td>
						</tr>
						<tr>
							<th><label for="sub4_3_2_detail_4"><%= localeMessage.getString("fieldExtract.bzwkFldName")%>4</label> <input type="checkbox" name="selName" id="sub4_3_2_detail_4" /></th>
							<td>
								<input type="text" name="bzwkFldName" />
							</td>
						</tr>
					</table>
				</form>
				
				<div class="table_row_title"><%= localeMessage.getString("fieldExtract.rowTitle")%></div>
				<table class="table_row" cellspacing="0">
					<tr>
						<th style="width:150px;"><%= localeMessage.getString("fieldExtract.layoutName")%></th>
						<td>
							<input type="text" name="loutName" style="width:95%;"> 
<%-- 							<img id="btn_search_loutName" src="<c:url value="/img/btn_pop_search.png" />" level="W" status="DETAIL,NEW" class="btn_img" />	 --%>
							<button type="button" class="cssbtn smallBtn2" id="btn_search_loutName" style="vertical-align:middle; font-weight:bold;" level="W" status="DETAIL,NEW"><i class="material-icons">search</i><%= localeMessage.getString("button.search") %></button>
						</td>
					</tr>
					<tr>
						<th><%= localeMessage.getString("fieldExtract.layoutDesc")%></th>
						<td colspan="5">
							<input type="text" name="loutDesc" readOnly>
						</td>
					</tr>
				</table>
				<!-- grid -->
				<table id="tree" ></table>				
				
			</div><!-- end content_middle -->
		</div><!-- end right_box -->	
	</body>
</html>