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

var isDetail = false;
function buildJsonArray(){
	var jsonArr = [];
    var jsonArrIndex = 0;
	for(var i=0;i<9;i++){
		var rowData = {fldPrcssNo:"",bzwkFldName:"",msgFldStartSituVal:"",msgFldLen:"",nomalPrcssCtnt:"",clsName:""};
		for(var key in rowData){
			var value = $("input[name="+key+"]").eq(i).val();
			rowData[key] = value;
			if (!value) {
				continue;
            }
		}	
		if( rowData.bzwkFldName.trim()=="") break;  
		
		jsonArr[jsonArrIndex] = rowData;
		jsonArrIndex++;
		
	}   
	return jsonArr; 
}	

function isValid(){
	for(var i=0;i<9;i++){
		var value = $("input[name=bzwkFldName]").eq(i).val();
		if (value.length>0){
			if (isNaN(parseInt($("input[name=msgFldStartSituVal]").eq(i).val()))){
				alert("<%=localeMessage.getString("messageKey.msgFldStartSituVal")%>"+Number(i+1)+"<%=localeMessage.getString("messageKey.checkValue")%>");
				return false;
			}
			if (isNaN(parseInt($("input[name=msgFldLen]").eq(i).val()))){
				alert("<%=localeMessage.getString("messageKey.msgFldLen")%>"+Number(i+1)+"<%=localeMessage.getString("messageKey.checkValue")%>");
				return false;
			}
		}
	}

	
	return true;
}
function init(url,key,callback){
	if(typeof callback === 'function') {
		callback(url,key);
	}
}
function detail(url,key){
	if (!isDetail)return;
	$.ajax({  
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'DETAIL'
		    , adptrBzwkGroupName : key
		    , msgDstcd : "${param.msgDstcd}"
		    , ioDstcd : "${param.ioDstcd}"
		    , groupInorNo : "${param.groupInorNo}"
		    },
		success:function(json){
			var data = json.rows;
			//공통
			$("input[name=adptrBzwkGroupName]").val(data[0]['ADPTRBZWKGROUPNAME']);
			$("input[name=msgDstcd]").val(data[0]['MSGDSTCD']);
			$("input[name=ioDstcdTxt]").val(data[0]['IODSTCD']);
			$("input[name=groupInorNo]").val(data[0]['GROUPINORNO']);
			//hidden 셋팅
			$("input[name=ioDstcd]").val("${param.ioDstcd}");
			
			//개별 
			for (var i=0;i<data.length;i++){
				$("input[name=fldPrcssNo]").eq(i).val(data[i]['FLDPRCSSNO']);
				$("input[name=bzwkFldName]").eq(i).val(data[i]['BZWKFLDNAME']);
				$("input[name=clsName]").eq(i).val(data[i]['CLSNAME']);
				$("input[name=msgFldStartSituVal]").eq(i).val(data[i]['MSGFLDSTARTSITUVAL']);
				$("input[name=msgFldLen]").eq(i).val(data[i]['MSGFLDLEN']);
				$("input[name=nomalPrcssCtnt]").eq(i).val(data[i]['NOMALPRCSSCTNT']);
			} 

		},
		error:function(e){
			alert(e.responseText);
		}
	});

}
$(document).ready(function() {	
	var returnUrl = getReturnUrlForReturn();
	
	var key ="${param.adptrBzwkGroupName}";
	if (key !="null"){
		isDetail = true;
	}
	init(url,key,detail);
	$("#btn_modify").click(function(){
		if (!isValid())return;
	
		//공통부만 form으로 구성
		var postData = $('#ajaxForm').serializeArray();
		postData.push({ name: "gridData" , value:JSON.stringify(buildJsonArray())});
		
		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로 이동
	});
	$("#btn_initialize").click(function(){
		$.ajax({  
			type : "POST",
			url:url,
			dataType:"json",
			data:{cmd: 'INITIALIZE',adptrBzwkGroupName:$("input[name=adptrBzwkGroupName]").val(),ioDstcd:$("input[name=ioDstcd]").val()},
			success:function(json){
				alert(json.message);
			},
			error:function(e){
				alert(e.responseText);
			}
		});		
		
	});		

	buttonControl(isDetail);
	titleControl(isDetail);
});
 
</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_initialize" level="W" status="DETAIL,NEW"><i class="material-icons">restart_alt</i> <%= localeMessage.getString("button.reset") %></button>
					<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_initialize.png" />" id="btn_initialize" 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_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("messageKey.subtitle")%></div>	
				<!-- 공통-->	
				<form id="ajaxForm">
					<table class="table_row" cellspacing="0">		
						<tr>
							<th style="width:150px;"><%= localeMessage.getString("messageKey.adptrBzwkGroupName")%></th>
							<td><input type="text" name="adptrBzwkGroupName" readonly="readonly" /> </td>
							<th style="width:150px;"><%= localeMessage.getString("messageKey.groupInorNo")%></th>
							<td><input type="text" name="groupInorNo" readonly="readonly" /></td>
						</tr>
						<tr>	
							<th style="width:150px;"><%= localeMessage.getString("messageKey.msgDstCd")%></th>
							<td><input type="text"  name="msgDstcd" readonly="readonly" /></td>
							<th style="width:150px;"><%= localeMessage.getString("messageKey.etractionDstCd")%></th>
							<td><input type="text"  name="ioDstcdTxt" value="${param.ioDstcd}" readonly="readonly" />
							<input type="hidden" name="ioDstcd"/>
							</td>
						</tr>		
					</table>
				</form>
						<!-- 개별 -->
				<table class="table_row" cellspacing="0">
					<tr height="30px">
						<th style = "text-align:center"><%= localeMessage.getString("messageKey.dstCd")%></th>
						<th style = "text-align:center"><%= localeMessage.getString("messageKey.bzwkFldName")%></th>
						<th style = "text-align:center"><%= localeMessage.getString("messageKey.msgFldStartSituVal")%></th>
						<th style = "text-align:center"><%= localeMessage.getString("messageKey.msgFldLen")%></th>
						<th style = "text-align:center"><%= localeMessage.getString("messageKey.nomalPrcssCtnt")%></th>
						<th style = "text-align:center"><%= localeMessage.getString("messageKey.clsName")%></th>
					</tr>
				<c:forEach var="i" begin="1" end="9">
					<tr>
						<td><input type="hidden" name="fldPrcssNo" value="${i}" /><c:out value="${i}"/></td>
						<td><input type="text" name="bzwkFldName"/></td></td>
						<td><input type="text"  name="msgFldStartSituVal"/></td>
						<td><input type="text"  name="msgFldLen"/></td>
						<td><input type="text" name="nomalPrcssCtnt"/></td>
						<td><input type="text" name="clsName"/></td>
					</tr>
				<%-- 	<tr>
						<th style="width:150px;background-color:#c5c5c5"><%= localeMessage.getString("messageKey.bzwkFldName")%><c:out value="${i}"/></th>
						<td><input type="hidden" name="fldPrcssNo" value="${i}" />
						<input type="text" name="bzwkFldName"/></td>
						<th><%= localeMessage.getString("messageKey.msgFldStartSituVal")%><c:out value="${i}"/></th>
						<td style="width:50px;"><input type="text"  name="msgFldStartSituVal"/></td>
						<th><%= localeMessage.getString("messageKey.msgFldLen")%><c:out value="${i}"/></th>
						<td style="width:50px;"><input type="text"  name="msgFldLen"/></td>
					</tr>
					<tr>
						<th><%= localeMessage.getString("messageKey.nomalPrcssCtnt")%><c:out value="${i}"/></th>
						<td><input type="text" name="nomalPrcssCtnt"/></td>
						<th><%= localeMessage.getString("messageKey.clsName")%><c:out value="${i}"/></th>
						<td  colspan="3"><input type="text" name="clsName"/></td>
					</tr>	 --%>
					</c:forEach>			
				</table>
			</div><!-- end content_middle -->
		</div><!-- end right_box -->	
	</body>
</html>

