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

var isDetail = false;
function isValid(){
	if($('input[name=scopeName]').val() == ""){
		alert("스코프명을 입력하여 주십시요.");
		$('input[name=scopeName]').focus();
		return false;
	}
	if($('input[name=scopeId]').val() == ""){
		alert("스코프ID를 입력하여 주십시요.");
		$('input[name=scopeId]').focus();
		return false;
	}
	
	return true;
}

function detail(key){
	if (!isDetail)return;
	$.ajax({
		type : "POST",
		url:url,
		dataType:"json",
		data:{cmd: 'DETAIL', scopeId : key},
		success:function(json){
			var data = json;
			$('input[name=scopeId]').attr('readonly', true);

			$("#ajaxForm input[type!=checkbox],#ajaxForm select,#ajaxForm textarea").each(function(){
				var name = $(this).attr("name");
				var tag  = $(this).prop("tagName").toLowerCase();
				$(tag+"[name="+name+"]").val(data[name.toUpperCase()]);
			});
		},
		error:function(e){
			alert(e.responseText);
		}
	});
}

function init(key,callback){
	detail(key);
}

$(document).ready(function() {
	var returnUrl = getReturnUrlForReturn();
	var key ="${param.scopeId}";
	if (key != "" && key !="null"){
		isDetail = true;
	}
	init(key,detail);
	
	$("#btn_modify").click(function(){
		if (!isValid()){
			return;
		}
		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("삭제하시겠습니까?")){
			var postData = $('#ajaxForm').serializeArray();
			postData.push({ name: "cmd" , value:"DELETE"});
			$.ajax({
				type : "POST",
				url:url,
				data:postData,
				success:function(args){
					if(args.status=="error"){
						alert("동기화중 오류입니다.\n확인하여 주십시요.\n" + args.message);
					}else{
						alert("삭제 되었습니다.");
						goNav(returnUrl);//LIST로 이동
					}
				},
				error:function(e){
					alert(e.responseText);
				}
			});
		}
	});
	
	$("#btn_previous").click(function(){
		goNav(returnUrl);//LIST로 이동
	});

	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("scpManDt.title") %></div>	
				
				<form id="ajaxForm">
					<table class="table_row" cellspacing="0">
						<tr>
							<th style="width:180px;"><%= localeMessage.getString("scpMan.scpNm") %> <small>(*)</small></th><td><input type="text" name="scopeName"/></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("scpMan.scpId") %> <small>(*)</small></th><td><input type="text" name="scopeId"/></td>
						</tr>
						<tr>
							<th><%= localeMessage.getString("scpMan.desc") %></th>
							<td>
								<textarea name="description" rows="3" cols="50"></textarea>
							</td>
						</tr>
					</table>
				</form>
				
			</div><!-- end content_middle -->
		</div><!-- end right_box -->
	</body>
</html>