<%@ 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 selectName = "adptrBzwkGroupName";	// selectBox Name

var isDetail = false;
function buildJsonArray(){
	var jsonArr = [];
    var jsonArrIndex = 0;
	for(var i=0;i<9;i++){
		var rowData = {fldPrcssNo:"",bzwkFldName:"",clsName:"",msgFldStartSituVal:"",msgFldLen:"",nomalPrcssCtnt:""};
		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(){
	if($('select[name=adptrBzwkGroupName]').val() == ""){
		alert("<%= localeMessage.getString("messageKey.alert1")%>");
		return false;
	}else if($('select[name=msgDstcd]').val() == ""){
		alert("<%= localeMessage.getString("messageKey.alert2")%>");
		return false;
	}else if($('select[name=ioDstcd]').val() == ""){
		alert("<%= localeMessage.getString("messageKey.alert3")%>");
		return false;
	}

	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(key){
	$.ajax({  
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'LIST_INIT_COMBO'},
		success:function(json){
			//adptrBzwkGroupName combo
			new makeOptions("CODE","NAME").setObj($("select[name=adptrBzwkGroupName]")).setFormat(codeName3OptionFormat).setData(json.adapterRows).rendering();
			//msgDstcd combo
			new makeOptions("CODE","NAME").setObj($("select[name=msgDstcd]")).setFormat(codeName3OptionFormat).setData(json.msgTypeRows).rendering();
			//ioDstcd combo
			new makeOptions("CODE","NAME").setObj($("select[name=ioDstcd]")).setFormat(codeName3OptionFormat).setData(json.exrTypeRows).rendering();
			
			setSearchable(selectName);	// 콤보에 searchable 설정
		},
		error:function(e){
			alert(e.responseText);
		}
	});
}
$(document).ready(function() {	
	var returnUrl = getReturnUrlForReturn();	
	
	var key ="${param.adptrBzwkGroupName}";
	if (key != "" && key !="null"){
		isDetail = true;
	}	
	init(key);
	
	$("#btn_modify").click(function(){
		if (!isValid())return;
	
		//공통부만 form으로 구성
		var postData = $('#ajaxForm').serializeArray();
		//adptrBzwkGroupName 필드가 중복되어 array로 필드값이 셋팅 되어 제거
		// postData.push({ name: "adptrBzwkGroupName" , value:$("select[name=adptrBzwkGroupName]").val()});
		postData.push({ name: "gridData" , value:JSON.stringify(buildJsonArray())});
		
		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(){
		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);
			}
		});
	});	
	$("#btn_previous").click(function(){
		goNav(returnUrl);//LIST로 이동
	});

	buttonControl();
	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_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("messageKey.subtitle")%></div>	
				<!-- 공통-->	
				<form id="ajaxForm">
					<table class="table_row" cellspacing="0">		
						<tr>
							<th style="width:150px;"><%= localeMessage.getString("messageKey.adptrBzwkGroupName")%></th>
							<td colspan="3">
								<div class="select-style"><select type="text"  name="adptrBzwkGroupName"/></div>
								<!-- <input type="text" name="adptrBzwkGroupName"/>  -->
							</td>
						</tr>
						<tr>
							<th style="width:150px;"><%= localeMessage.getString("messageKey.msgDstCd")%></th>
							<td><div class="select-style"><select type="text"  name="msgDstcd"/></div></td>
							<th style="width:150px;"><%= localeMessage.getString("messageKey.etractionDstCd")%></th>
							<td><div class="select-style"><select type="text"  name="ioDstcd"/></div>
							<input type="hidden" name="groupInorNo" />
							</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="<c:out 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>

