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

function init(url,key,key2,key3,callback) {
	$.ajax({  
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'LIST_INIT_COMBO'},
		success:function(json){
			//EAI서버구분코드
			new makeOptions("CODE","NAME").setObj($("select[name=eaiSevrDstCd]")).setData(json.inoutTypeRows).setFormat(codeName3OptionFormat).rendering();
			
			if(key3 != ''){
				$("select[name=eaiSevrDstCd]").val(key3);
			}
			
			
			if(typeof callback === 'function') {
				callback(key,key2);
			}
						
		},
		error:function(e){
			alert(e.responseText);
		}
	});
	
}


var isDetail = false;

function isValid(){
	// 수신 어댑터명, 업무명 필수 추가
// 	if($('input[name=recvAdptrBzwkName]').val() == ""){
<%-- 		alert('<%=localeMessage.getString("socketPortMan.msg0")%>'); --%>
// 		$('input[name=recvAdptrBzwkName]').focus();
// 		return false;
// 	}
	
	if($('input[name=recvAdptrDesc]').val() == ""){
		alert('<%=localeMessage.getString("socketPortMan.msg1")%>');
		$('input[name=recvAdptrDesc]').focus();
		return false;
	}	
	
	//수신 포트는 필수등록
	if($('input[name=recvTestPort]').val() == ""){
		alert('<%=localeMessage.getString("socketPortMan.msg2")%>');
		$('input[name=recvTestPort]').focus();
		return false;
	}
	
	if($('input[name=recvRealPort]').val() == ""){
		alert('<%=localeMessage.getString("socketPortMan.msg3")%>');
		$('input[name=recvRealPort]').focus();
		return false;
	}
	
	return true;		
}

//어댑터수정여부
function isModify(type){
	
	var orgName;
	var curName;
	
	if(type == 'R'){
		orgName = $("input[name=org_recvAdptrBzwkName]").val();
		curName = $("input[name=recvAdptrBzwkName]").val();
	}else{
		orgName = $("input[name=org_sendAdptrBzwkName]").val();
		curName = $("input[name=sendAdptrBzwkName]").val();
	}
	
	if(orgName == curName){
		return false;
	}
	
	return true;
	
}

//포트번호 할당	 신규등록시 사용하지 않는 어댑터중에 젤 작은 포트번호 가져오기
function portAssign(key3){ 
	var eaiSevrDstCd = $("select[name=eaiSevrDstCd]").val();
	
	var postData = [];
	postData.push({ name: "cmd" 		 , value: "DETAIL_INIT_PORT"});
	postData.push({ name: "eaiSevrDstCd" , value:(key3==''?eaiSevrDstCd:key3)});
	
	
	console.log(postData);
	$.ajax({  
		type : "POST",
		url:url,
		dataType:"json",
		data:postData,
		success:function(json){
			
			var isUnusedPort = null;
			$("input").each(function(){
				var name = $(this).attr('name').toUpperCase();
				
				if(name == 'RECVLASTADMUSERID'){
					isUnusedPort = json[name];
				}
				
				$(this).val(json[name]);
				
			});
			
			
			if(isUnusedPort != null){  //사용하지 않은 어댑터 불러옴
				isDetail = true;
				
				$("input[name=recvTestPort]").attr('readonly',true);
				$("input[name=recvRealPort]").attr('readonly',true);
				
				$("input[name=recvportlastymd]").inputmask("9999-99-99 99:99:99",{'autoUnmask':true});
				$("input[name=sendportlastymd]").inputmask("9999-99-99 99:99:99",{'autoUnmask':true});
			}else{//신규 등록시 송신포트는 default 없앰
				$("input[name=sendTestPort]").val("");
				$("input[name=sendRealPort]").val("");
			}
			
						
		},
		error:function(e){
			alert(e.responseText);
		}
	});
	
}

//송신포트할당
function btnPortAssign(){
	
	var recvTestPort = $("input[name=recvTestPort]").val();
	var recvRealPort = $("input[name=recvRealPort]").val();
	
	if(recvRealPort == null || recvRealPort == "" || Number(recvRealPort) < 1000
	|| recvTestPort	== null || recvTestPort == "" || Number(recvTestPort) < 1000
	){
		alert("수신테스트포트 or 수신온라인포트를 확인해 주세요.");
		return false;
	}
	
	 $("input[name=sendTestPort]").val(Number(recvTestPort-1000));
	 $("input[name=sendRealPort]").val(Number(recvRealPort-1000));
	
}


function isNumber(v){
	var reg=/^[-+]?\d+$/;
	return reg.test(v);
}

function detail(key,key2){
	if (!isDetail)return;
	$.ajax({  
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'DETAIL'
		    , recvTestPort : key
		    , eaiSevrDstCd : key2
		    },
		success:function(json){
			var data = json;
			
			$("#ajaxForm input[type!=radio],#ajaxForm select").each(function(){
				var name = $(this).attr("name");
				var tag  = $(this).prop("tagName").toLowerCase();
				var dataVal = data[name.toUpperCase()];
				if(dataVal != null){
					dataVal = dataVal.trim();
				}
				$(tag+"[name="+name+"]").val(dataVal);
				
				//송신 수신 어댑터명 수정여부 판단
				if(name == 'recvAdptrBzwkName'){
					$("input[name=org_recvAdptrBzwkName]").val(dataVal);
				}else if(name == 'sendAdptrBzwkName'){
					$("input[name=org_sendAdptrBzwkName]").val(dataVal);
				}
				
			});
			
			$("input[name=recvTestPort]").attr('readonly',true);
			$("input[name=recvRealPort]").attr('readonly',true);
			
			$("input[name=recvportlastymd]").inputmask("9999-99-99 99:99:99",{'autoUnmask':true});
			$("input[name=sendportlastymd]").inputmask("9999-99-99 99:99:99",{'autoUnmask':true});
			
		},
		error:function(e){
			alert(e.responseText);
		}
	});

}
$(document).ready(function() {	
	var returnUrl = getReturnUrlForReturn();
	
	var key ="${param.recvTestPort}";
	var key2 ="${param.eaiSevrDstCd}";
	var key3 ="${param.searchEaiSevrDstCd}";
	
	if (key !="" && key2 !=""){
		isDetail = true;
	}
	
	init(url,key,key2,key3,detail);
	
	if(!isDetail){ 
		portAssign(key3);
	}
	
	
	$("#btn_modify").click(function(){	
		
		if (!isValid()) return;		
	
		//공통부만 form으로 구성
		var postData = $('#ajaxForm').serializeArray();
		postData.push({ name: "gridData" , value:JSON.stringify(postData)});
		
		if (isDetail){
			//송수신 어댑터명 수정여부
			if(isModify('R')){
				postData.push({ name: "resvModYN" , value:"Y"});	
			}
			if(isModify('S')){
				postData.push({ name: "sendModYN" , value:"Y"});	
			}
			postData.push({ name: "cmd" , value:"UPDATE"});
			
		}else{
			postData.push({ name: "cmd" , value:"INSERT"});
		}
		$.ajax({
			type : "POST",
			url:url,
			data:postData,
			success : function(json) {
				if(json.message == "success"){
					alert("<%= localeMessage.getString("common.saveMsg") %>");
					goNav(returnUrl);//LIST로 이동
				}else{
					alert(json.message);
				}
			},
			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로 이동
	});
	
	$("select[name=eaiSevrDstCd]").change(function(){
		portAssign('');
	});
	
	$("#btn_assign").click(function(){
		btnPortAssign();
	});
	
	$("input[name=recvTestPort]").on("keyup", function(){
		$(this).val($(this).val().replace(/[^0-9]/g,""));
		$("input[name=recvRealPort]").val($(this).val());
		if($("input[name=recvRealPort]").val().length>5 || $("input[name=recvRealPort]").val() > 20000){
			$("input[name=recvRealPort]").val($(this).val()-10000);
		}
	});	
	
	$("input[name=recvRealPort]").on("keyup", function(){
		$(this).val($(this).val().replace(/[^0-9]/g,""));
	});	
	
	$("input[name=sendTestPort]").on("keyup", function(){
		$(this).val($(this).val().replace(/[^0-9]/g,""));
	});
	
	$("input[name=sendRealPort]").on("keyup", function(){
		$(this).val($(this).val().replace(/[^0-9]/g,""));
	});

	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_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("socketPortMan.title")%></div>	
				<!-- 공통-->	
				
				<form id="ajaxForm">
					<table class="table_row" cellspacing="0">
						<tr>
							<th style="width:150px;"><%=localeMessage.getString("socketPortMan.eaisevrdstcd")%></th>
							<td>
								<div class="select-style">
									<select name="eaiSevrDstCd">
									</select>
								</div>	
							</td>
						</tr>
					</table>
				<div class="table_row_title">[<%=localeMessage.getString("socketPortMan.title2")%>]</div>
					<table class="table_row" cellspacing="0">
					<input type="hidden" name="org_recvAdptrBzwkName"/>
						<tr>
							<th style="width:150px;"><%=localeMessage.getString("socketPortMan.recvadptrbzwkname")%></th>
							<td><input type="text" name="recvAdptrBzwkName"/></td>
							<th style="width:180px;"><%=localeMessage.getString("socketPortMan.recvadptrdesc")%>*</th>
							<td><input type="text" name="recvAdptrDesc"/></td>
						</tr>
						<tr>
							<th style="width:180px;"><%=localeMessage.getString("socketPortMan.recvtestport")%>*</th>
							<td><input type="text" name="recvTestPort" maxlength="5"/></td>
							<th style="width:180px;"><%=localeMessage.getString("socketPortMan.recvrealport")%>*</th>
							<td><input type="text" name="recvRealPort" maxlength="5"/></td>
						</tr>
						<tr>
							<th style="width:180px;"><%=localeMessage.getString("socketPortMan.recvportlastymd")%></th>
							<td><input type="text" name="recvportlastymd" readonly/></td>
							<th style="width:180px;"><%=localeMessage.getString("socketPortMan.recvlastadmuserid")%></th>
							<td><input type="text" name="recvlastAdmUserid" readonly/></td>
						</tr>
						
					</table>
					
					<div class="table_row_title">
						<table><tr>
							<td class="table_row_title">
								[<%=localeMessage.getString("socketPortMan.title3")%>]
							</td>
							<td>&nbsp;</td>
							<td>
<%-- 								<img src="<c:url value="/img/btn_assign.png" />" id="btn_assign"  level="W" status="DETAIL,NEW" class="btn_img"/> --%>
								<button type="button" class="cssbtn smallBtn2" id="btn_assign" style="vertical-align:middle; font-weight:bold;" level="W" status="DETAIL,NEW"><i class="material-icons">assignment</i><%= localeMessage.getString("button.assign") %></button>
							</td>
							<td>&nbsp;<span style="color:blue;"><%=localeMessage.getString("socketPortMan.msg")%></span></td><td>&nbsp;</td>
						</tr></table>		
					</div>
					
					
					<table class="table_row" cellspacing="0">
					<input type="hidden" name="org_sendAdptrBzwkName"/>
						<tr>
							<th style="width:150px;"><%=localeMessage.getString("socketPortMan.sendadptrbzwkname")%></th>
							<td><input type="text" name="sendAdptrBzwkName" /></td>
							<th style="width:180px;"><%=localeMessage.getString("socketPortMan.sendadptrdesc")%></th>
							<td> <input type="text" name="sendAdptrDesc" /></td>
						</tr>
						<tr>
							<th style="width:180px;"><%=localeMessage.getString("socketPortMan.sendtestport")%></th>
							<td>
							<input type="text" name="sendTestPort" maxlength="5" /></td>
							<th style="width:180px;"><%=localeMessage.getString("socketPortMan.sendrealport")%></th>
							<td>
							<input type="text" name="sendRealPort" maxlength="5" /></td>
						</tr>
						<tr>
							<th style="width:180px;"><%=localeMessage.getString("socketPortMan.sendportlastymd")%></th>
							<td><input type="text" name="sendportlastymd" readonly/></td>
							<th style="width:180px;"><%=localeMessage.getString("socketPortMan.sendlastadmuserid")%></th>
							<td><input type="text" name="sendlastAdmUserid" readonly/></td>
						</tr>
					</table>
				</form>
			</div><!-- end content_middle -->
		</div><!-- end right_box -->	
	</body>
</html>

