<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ page import="com.eactive.eai.rms.common.login.SessionManager"%>
<%@ page import="com.eactive.eai.common.util.SystemUtil"%>
<%
	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="/bap/admin/work/systemIDMan.json" />';
var url_view ='<c:url value="/bap/admin/work/systemIDMan.view" />';

var isDetail = false;
var selectName = "bJobBzwkDstcd";	// selectBox Name
function init(key,callback){
	$.ajax({  
		type : "POST",
		url: '<c:url value="/bap/admin/work/systemIDMan.json" />',
		dataType:"json",
		data:{cmd: 'LIST_INIT_COMBO'},
		success:function(json){
			new makeOptions("CODE","NAME").setObj($("select[name=bJobBzwkDstcd]")).setNoValueInclude(true).setNoValue("", "").setFormat(codeName3OptionFormat).setData(json.bjobBzwkDstcdRows).rendering();
			setSearchable(selectName);
			if(typeof callback === 'function') {
				callback(key);
    		}
		},
		error:function(e){
			alert(e.responseText);
		}
	});
}

function detail(key){
	if (!isDetail)return;
	$.ajax({  
		type : "POST",
		url:url,
		dataType:"json",
		data:{ cmd: 'DETAIL',
		       systemId : key
		    },
		success:function(json){
			var data = json;
			
			$("input[name=systemId]").attr('readonly',true);
			$("input,select").each(function(){
				var name = $(this).attr('name');
				if (name != null)
					$(this).val(data[name.toUpperCase()]);
			});
			var bJobBzwkDstcdVal = $("select[name=bJobBzwkDstcd]").val();
			if(bJobBzwkDstcdVal!=""){
				getOgranFile(bJobBzwkDstcdVal);
			}
		},
		error:function(e){
			alert(e.responseText);
		}
	});
}
function isValid() {
	var systemId = $('input[name=systemId]').val(); 
	if (systemId == "") {
		alert("시스템ID를 입력해 주세요");
		$('input[name=systemId]').focus();
		return false;
	} else if (systemId.length > 9) {
		alert("시스템ID는 8자 이내로 입력해 주세요");
		$('input[name=systemId]').focus();
		return false;
	}
	
	return true;
}

function getOgranFile(key){
	$.ajax({  
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'LIST_FILTER_COMBO' , bJobBzwkDstcd : key },
		success:function(json){
		    $("select[name=osidInstiDstcd]").empty();
			new makeOptions("CODE","NAME").setObj($("select[name=osidInstiDstcd]")).setFormat(codeName3OptionFormat).setData(json.osidInstiRows).rendering();	
		},
		error:function(e){
			alert(e.responseText);
		}
	});
}
$(document).ready(function() {	
	var returnUrl = getReturnUrlForReturn();
	
	var key = "${param.systemId}";

	if (key != "" && key != "null") {
		isDetail = true;
	}

	init(key, detail);
	$("#btn_modify").click(function(){
		if (!isValid())return;
		
		if (isDetail) {
			if ( confirm( "수정하시겠습니까?" ) != true ) return;
		} else {
			if ( confirm( "등록하시겠습니까?" ) != true ) 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("저장 되었습니다.");
				goNav(returnUrl);//LIST로 이동
			},
			error:function(e){
				alert(e.responseText);
			}
		});
	});
	$("#btn_delete").click(function(){

		if ( confirm( "삭제하시겠습니까?" ) != true ) return;
		
		var postData = $('#ajaxForm').serializeArray();
		postData.push({ name: "systemId" , value:$("input[name=systemId]").val()});
		postData.push({ name: "cmd" , value:"DELETE"});
		$.ajax({
			type : "POST",
			url:url,
			data:postData,
			success:function(args){
				alert("삭제 되었습니다.");
				goNav(returnUrl);//LIST로 이동

			},
			error:function(e){
				alert(e.responseText);
			}
		});
	});	
	$("#btn_previous").click(function(){
		goNav(returnUrl);//LIST로 이동
	});
	
	$("select[name=bJobBzwkDstcd]").change(function(){
		var key = $(this).val();
		if(!key){
			$("select[name=osidInstiDstcd]").empty();
			return;
		}
	    getOgranFile(key);
	});
	
	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" id="title">
				<div class="search_wrap">
					<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_delete.png"/>" alt="" id="btn_delete" level="W" status="DETAIL"/>
					<img src="<c:url value="/img/btn_previous.png"/>" alt="" id="btn_previous" level="R" status="DETAIL,NEW"/>
				</div>
				<div class="title">시스템 ID 관리</div>						
				<form id="ajaxForm">
					<table class="table_row" cellspacing="0">
						<tr>
							<th style="width:180px;">시스템ID</th>
							<td><input type="text" name="systemId" /> </td>
						</tr>
						<tr>
							<th style="width:180px;">시스템명</th>
							<td><input type="text" name="systemName" /> </td>
						</tr>
						<tr>
							<th style="width:180px;">업무구분코드</th>
							<td>
								<div class="select-style">
									<select name="bJobBzwkDstcd"></select>
								</div>
							</td>
						</tr>
						<tr>
							<th style="width:180px;">대외기관구분코드</th>
							<td>
								<div class="select-style">
									<select name="osidInstiDstcd"></select>
								</div>
							</td>
						</tr>
						<tr>
							<th style="width:180px;">NDM노드명</th>
							<td><input type="text" name="rmtNodeName" /> </td>
						</tr>
					</table>
				</form>
			</div><!-- end content_middle -->
		</div><!-- end right_box -->	
	</body>
</html>

