<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	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 popData = window.dialogArguments['data'];
var isPopup = window.dialogArguments['ispop'];

function isValid() {

	// 헤더검증 체크
	if($('#hdrVeriYn').val() == "1") {
		if($('#hdrVeriOffset').val().trim() == "") {
			$('#hdrVeriOffset').focus();
			alert("헤더검증 시작위치를 입력하여 주십시요.");
			return false;
		} else if($('#hdrVeriLen').val().trim() == "") {
			$('#hdrVeriLen').focus();
			alert("헤더검증 길이를 입력하여 주십시요.");
			return false;
		} else if($('#hdrVeriVal').val().trim() == "") {
			$('#hdrVeriVal').focus();
			alert("헤더검증 검증값을 입력하여 주십시요.");
			return false;
		} 
	}

	// 데이터검증 체크
	const dataVeriCnt = $("select[name=dataVeriCnt]").val();
	if($('#dataVeriYn').val() == "1") {
		if($('#dataVeriOffset').val().trim() == "") {
			$('#dataVeriOffset').focus();
			alert("데이터검증 시작위치를 입력하여 주십시요.");
			return false;
		} else if($('#dataVeriLen').val().trim() == "") {
			$('#dataVeriLen').focus();
			alert("데이터검증 길이를 입력하여 주십시요.");
			return false;
		} else if(dataVeriCnt == 0) {
			$('#dataVeriCnt').focus();
			alert("데이터검증 갯수를 선택하십시요.");
			return false;
		} else if(dataVeriCnt > 0) {
			for(let i = 1; i <= dataVeriCnt; i++) {
				if($("#dataVeriVal" + i).val().trim() == "") {
					$("#dataVeriVal" + i).focus();
					alert("데이터검증을 위한 검증값 [ " + i + " ] 를 입력하여 주십시요.");
					return false;
				}
			}
		} 
	}

	// 트레일러검증 체크
	if($('#trailVeriYn').val() == "1") {
		if($('#trailVeriOffset').val().trim() == "") {
			$("#trailVeriOffset").focus();
			alert("트레일러검증 시작위치를 입력하여 주십시요.");
			return false;
		} else if($('#trailVeriLen').val().trim() == "") {
			$("#trailVeriLen").focus();
			alert("트레일러검증 길이를 입력하여 주십시요.");
			return false;
		} else if($('#trailVeriVal').val().trim() == "") {
			$("#trailVeriVal").focus();
			alert("트레일러검증 검증값을 입력하여 주십시요.");
			return false;
		} 
	}

/* 	$('#thisMsgUseYn').val($('#thisMsgUseCheck').is(":checked") ? '1' : '0');
	$('#hdrVeriYn').val($('#hdrVeriCheck').is(":checked") ? '1' : '0');
	$('#dataVeriYn').val($('#dataVeriCheck').is(":checked") ? '1' : '0');
	$('#trailVeriYn').val($('#trailVeriCheck').is(":checked") ? '1' : '0');
 */
	if (dataVeriCnt > 0) {
		let dataVeriVal = '';
		for(let i = 1; i <= dataVeriCnt; i++) {
			dataVeriVal += $("#dataVeriVal" + i).val() + ',';
		}
		$('#dataVeriVal').val(dataVeriVal.substring(0, dataVeriVal.length-1));
	} else {
		$('#dataVeriVal').val('');
	}
	
	// default 값 셋팅
	if ($('#hdrVeriOffset').val().trim() == "") $('#hdrVeriOffset').val("0");
	if ($('#dataVeriOffset').val().trim() == "") $('#dataVeriOffset').val("0");
	if ($('#trailVeriOffset').val().trim() == "") $('#trailVeriOffset').val("0");
	
	if ($('#hdrVeriLen').val().trim() == "") $('#hdrVeriLen').val("0");
	if ($('#dataVeriLen').val().trim() == "") $('#dataVeriLen').val("0");
	if ($('#trailVeriLen').val().trim() == "") $('#trailVeriLen').val("0");

	if ($('#hdrVeriVal').val().trim() == "") $('#hdrVeriVal').val(" ");
	if ($('#dataVeriVal').val().trim() == "") $('#dataVeriVal').val(" ");
	if ($('#trailVeriVal').val().trim() == "") $('#trailVeriVal').val(" ");

	return true;
}

function init(url, key) {
	$("input[name=bjobMsgDstcd]").val(key);
	$.ajax({
		type: "POST",
		url: url,
		dataType: "json",
		data:{cmd: 'DETAIL_VERIFY_INFO',
			bjobMsgDstcd: key},
		success: function(json){
			const detail = json.detail;
			$("input[name=bjobMsgDstcd]").attr('readonly', true);
			$("input[name=bjobBzwkDstcd]").attr('readonly', true);
			
			$("input,select").each(function(){
				var name = $(this).attr('name').toUpperCase();
				if ( name != null ) $(this).val(detail[name]);
			});

/* 			// 사용여부 체크
			$('#thisMsgUseCheck').attr('checked', $('#thisMsgUseYn').val() == 1);
			// 검증여부 체크
			$('#hdrVeriCheck').attr('checked', $('#hdrVeriYn').val() == 1);
			$('#dataVeriCheck').attr('checked', $('#dataVeriYn').val() == 1);
			$('#trailVeriCheck').attr('checked', $('#trailVeriYn').val() == 1);
 */			
			// 검증갯수셋팅
			let dataVeriCnt = ($("#dataVeriVal").val().match(/,/g) || []).length;
			if($("input[name=dataVeriVal]").val() != '') {
				dataVeriCnt += 1;
				const arrDataVeriVal = $("input[name=dataVeriVal]").val().split(',');
				arrDataVeriVal.forEach((veriVal, idx) => {
					$("#dataVeriVal" + (idx+1)).val(veriVal);
				 });
			}
			
			clearInputbox(dataVeriCnt);
			
			// default 값 셋팅
			if ($('#hdrVeriOffset').val().trim() == "") $('#hdrVeriOffset').val("0");
			if ($('#dataVeriOffset').val().trim() == "") $('#dataVeriOffset').val("0");
			if ($('#trailVeriOffset').val().trim() == "") $('#trailVeriOffset').val("0");
			
			if ($('#hdrVeriLen').val().trim() == "") $('#hdrVeriLen').val("0");
			if ($('#dataVeriLen').val().trim() == "") $('#dataVeriLen').val("0");
			if ($('#trailVeriLen').val().trim() == "") $('#trailVeriLen').val("0");

			$("input[name=hdrVeriCnt]").val('1').attr('readonly', true); // 헤더검증
			$("select[name=dataVeriCnt]").val(dataVeriCnt); // 데이터 검증
			$("input[name=trailVeriCnt]").val('1').attr('readonly', true); // 트레일러 검증
		},
		error: function(e){
			alert(e.responseText);
		}
	});
}

function clearInputbox(cnt) {
	const num = parseInt(cnt);
	for(let i = 1; i <= 60; i++) {
		$("#dataVeriVal" + i).attr('readonly', true);
	}
	for(let j = 0; j <= num; j++) {
		$("#dataVeriVal" + j).attr('readonly', false);
	}
	for(let k = (num + 1); k <= 60; k++) {
		$("#dataVeriVal" + k).val('');
	}
}

$(document).ready(function() {
 	var url ='<c:url value="/bap/admin/work/messageProcMan.json" />';
 	var key = popData;

 	init(url, key);
 	
	$("select[name=dataVeriCnt]").change(function() {
		clearInputbox($(this).val());
	});
	
	$("#btn_close").click(function() {
		window.close();
	});

	$("#btn_modify").click(function() {

		if(!isValid()) return true;

		var postData = $('#ajaxForm').serializeArray();
		postData.push({ name: "cmd", value:"UPDATE_VERIFY_INFO"});

		$.ajax({
			type: "POST",
			url: url,
			data: postData,
			success: function(args) {
				alert("저장 되었습니다.");
				window.close();
			},
			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" id="content_middle">
			<div class="search_wrap">
			<img src="<c:url value="/img/btn_modify.png"/>" alt="" id="btn_modify" level="W" />
			<img src="<c:url value="/img/btn_close.png"/>" alt="" id="btn_close" level="R" />
			</div>
			<div class="title">데이터 검증정보 관리</div>
			<form id="ajaxForm">
				<table class="search_condition" cellspacing="0">
					<tr>
						<th style="width:17%">인터페이스ID</th>
						<td style="width:25%"><input type="text" id="bjobMsgDstcd" name="bjobMsgDstcd"></td>
						<th style="width:17%">대외기관코드</th>
						<td style="width:25%"><input type="text" id="bjobBzwkDstcd" name="bjobBzwkDstcd"></td>
						<td style="width:16%">
<!-- 						<input type="hidden" id="thisMsgUseYn" name="thisMsgUseYn" />
							<label style="cursor:pointer;margin-right:2px;margin-bottom:4px;"><input type="checkbox" id="thisMsgUseCheck" name="thisMsgUseCheck" style="cursor:pointer;margin-right:5px;margin-bottom:3px;" />사용</label> -->
							<div class="select-style">
								<select id="thisMsgUseYn" name="thisMsgUseYn">
								<option value="1">사용</option>
								<option value="0">사용안함</option>
								</select>
							</div>
						</td>
					</tr>
				</table>
				<fieldset style="border: 1px solid #c5c5c5; padding: 10px; margin-top:20px;">
					<legend>헤더검증</legend>
					<!-- <input type="hidden" id="hdrVeriYn" name="hdrVeriYn" />
					<label style="cursor:pointer;margin-right:2px;margin-bottom:5px;"><input type="checkbox" id="hdrVeriCheck" name="hdrVeriCheck" style="cursor:pointer;margin-right:5px;margin-bottom:5px;" />검증</label> -->
					<table class="table_row" cellspacing="0">
						<colgroup>
							<col width="14%">
							<col width="20%">
							<col width="13%">
							<col width="20%">
							<col width="13%">
							<col width="20%">
						</colgroup>
						<tr>
							<th>검증여부</th>
							<td style="border-right: 0px;">
							<div class="select-style">
								<select id="hdrVeriYn" name="hdrVeriYn">
								<option value="1">사용</option>
								<option value="0">사용안함</option>
								</select>
							</div>
							</td>
							<td colspan="4" />
						</tr>
						<tr>
							<th>시작위치</th>
							<td><input type="text" maxlength="3" id="hdrVeriOffset" name="hdrVeriOffset" onKeyup="this.value=this.value.replace(/[^0-9]/g,'');" style="text-align:center;" /></td>
							<th>길이</th>
							<td><input type="text" maxlength="3" id="hdrVeriLen" name="hdrVeriLen" onKeyup="this.value=this.value.replace(/[^0-9]/g,'');" style="text-align:center;" /></td>
							<th>검증갯수</th>
							<td><input type="text" maxlength="1" id="hdrVeriCnt" name="hdrVeriCnt" value="1" onKeyup="this.value=this.value.replace(/[^0-9]/g,'');" style="text-align:center;" /></td>
						</tr>
						<tr>
							<th>검증값</th>
							<td colspan="5"><input type="text" maxlength="10" id="hdrVeriVal" name="hdrVeriVal" style="width:150px;" /></td>
						</tr>
					</table>
				</fieldset>
				<fieldset style="border: 1px solid #c5c5c5; padding: 10px; margin-top:20px;">
					<legend>데이터 검증</legend>
					<!-- <input type="hidden" id="dataVeriYn" name="dataVeriYn" />
					<label style="cursor:pointer;margin-right:2px;margin-bottom:5px;"><input type="checkbox" id="dataVeriCheck" name="dataVeriCheck" style="cursor:pointer;margin-right:5px;margin-bottom:5px;" />검증</label> -->
					<table class="table_row" cellspacing="0">
						<colgroup>
							<col width="14%">
							<col width="20%">
							<col width="13%">
							<col width="20%">
							<col width="13%">
							<col width="20%">
						</colgroup>
						<tr>
							<th>검증여부</th>
							<td style="border-right: 0px;">
							<div class="select-style">
								<select id="dataVeriYn" name="dataVeriYn">
								<option value="1">사용</option>
								<option value="0">사용안함</option>
								</select>
							</div>
							</td>
							<td colspan="4" />
						</tr>
						<tr>
							<th>시작위치</th>
							<td><input type="text" maxlength="3" id="dataVeriOffset" name="dataVeriOffset" onKeyup="this.value=this.value.replace(/[^0-9]/g,'');" style="text-align:center;" /></td>
							<th>길이</th>
							<td><input type="text" maxlength="3" id="dataVeriLen" name="dataVeriLen" onKeyup="this.value=this.value.replace(/[^0-9]/g,'');" style="text-align:center;" /></td>
							<th>검증갯수</th>
							<td><div class="select-style">
								<select id="dataVeriCnt" name="dataVeriCnt" style="text-align:center;">
								<c:forEach var="i" begin="0" end="60">
								<option value="${i}">${i}</option>
								</c:forEach>
								</select>
								</div>
							</td>
						</tr>
						<tr>
							<th>검증값</th>
							<td colspan="5">
								<input type="hidden" id="dataVeriVal" name="dataVeriVal" />
								<div style="margin-top:3px;"></div>
								<c:forEach var="i" begin="1" end="60">
									<c:if test="${i != 1 and i % 10 == 1}">
									<br><div style="margin-top:3px;"></div>
									</c:if>
									<input type="text" maxlength="1" id="dataVeriVal${i}" name="dataVeriVal${i}" style="width:40px;margin-right:4.6px;text-align:center;" />
								</c:forEach>
								<div style="margin-top:3px;"></div>
							</td>
						</tr>
					</table>

				</fieldset>
				<fieldset style="border: 1px solid #c5c5c5; padding: 10px; margin-top:20px;">
					<legend>트레일러 검증</legend>
					<!-- <input type="hidden" id="trailVeriYn" name="trailVeriYn" />
					<label style="cursor:pointer;margin-right:2px;margin-bottom:5px;"><input type="checkbox" id="trailVeriCheck" name="trailVeriCheck"  style="cursor:pointer;margin-right:5px;margin-bottom:5px;" />검증</label> -->
					<table class="table_row" cellspacing="0">
						<colgroup>
							<col width="14%">
							<col width="20%">
							<col width="13%">
							<col width="20%">
							<col width="13%">
							<col width="20%">
						</colgroup>
						<tr>
							<th>검증여부</th>
							<td style="border-right: 0px;">
							<div class="select-style">
								<select id="trailVeriYn" name="trailVeriYn">
								<option value="1">사용</option>
								<option value="0">사용안함</option>
								</select>
							</div>
							</td>
							<td colspan="4" />
						</tr>
						<tr>
							<th>시작위치</th>
							<td><input type="text" maxlength="3" id="trailVeriOffset" name="trailVeriOffset" onKeyup="this.value=this.value.replace(/[^0-9]/g,'');" style="text-align:center;" /></td>
							<th>길이</th>
							<td><input type="text" maxlength="3" id="trailVeriLen"name="trailVeriLen" onKeyup="this.value=this.value.replace(/[^0-9]/g,'');" style="text-align:center;" /></td>
							<th>검증갯수</th>
							<td><input type="text" maxlength="1" id="trailVeriCnt" name="trailVeriCnt" value="1" onKeyup="this.value=this.value.replace(/[^0-9]/g,'');" style="text-align:center;" /></td>
						</tr>
						<tr>
							<th>검증값</th>
							<td colspan="5"><input type="text" maxlength="10" id="trailVeriVal" name="trailVeriVal" style="width:150px;" /></td>
						</tr>
					</table>
				</fieldset>
			</form>
		</div><!-- end content_middle -->
	</div><!-- end right_box -->
</body>
</html>