<%@page import="kcg.imis.sec.LoginUserVO"%>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<link rel="stylesheet" type="text/css" href="/css/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="/css/easyui/themes/icon.css">
<script type="text/javascript" src="/js/lib/jquery.easyui.min.js"></script>
<script type="text/javaScript" defer="defer">
	if('${isEmpty}' == 'true'){
		location.href = '/';
	}
</script>

<div class="memberAdd" id="table">
	<div class="easyui-panel" style="padding:5px">
		<ul id="moniter">
			
		</ul>
	</div>
</div>
<br/>

	<table class="tbl_add" width="100%" border="0" cellpadding="0" cellspacing="0"">
		<colgroup>
   			<col width="140"/>
   			<col width="">
   			<col width="140"/>
   			<col width="">
   			<col width="140"/>
   			<col width="">
	    </colgroup>	   		
		<tbody>
			<tr>				
				<th>이름</th>
				<td colspan="3">
					<input type="hidden" id="id">
					<input type="hidden" id="ord">
					<input type="hidden" id="parendId">
					<input type="text" id="name" >
				</td>
			</tr>
			<tr>
				<th>링크</th>
				<td colspan="3">
					<input type="text" id="href" >
				</td>
			</tr>
		</tbody>
	</table>
	
<form name="menuForm" method="post">
		<div class="boardviewinfo">
		<ul class="right btn-style">
			<li>
 				<a href="javascript:Moniter.add();">추가</a>
            </li>
 			<li>
 				<a href="javascript:Moniter.save();">저장</a>
            </li>
            <li>
 				<a href="javascript:Moniter.remove();">삭제</a>
            </li>
		</ul>	
	</div>
</form>
<script type="text/javaScript" defer="defer">
$(document).ready(function(){
	$("#moniter").tree({
		url : "/admin/moniterList.json",
		loadFilter: function(data){
			console.log(data);
	      	return data;
	    },
	    onClick :function(node){
	    	console.log(node);
	    	$("#id").val(node.id);
	    	$("#parendId").val(node.parentId);
	    	$("#name").val(node.text);
	    	$("#ord").val(node.ord);
	    	$("#href").val(node.href);
	    	
	    }
	});
});

var Moniter = {
		add : function(){
			var selected = $('#moniter').tree('getSelected');
			
			if(!selected){
				alert("선택된 노드가 없습니다.");
				return;				
			}
			
			if(selected.id == ''){
				alert("저장 후 추가하십시오.");
				return;
			}
			
			if(selected.depth == '6') {
				alert("더이상 하위로 추가하실수 없습니다.");
				return;
			}
			
			$('#moniter').tree('append', { 
				parent: selected.target,
				data: [{
					id: '',
					text: '새노드',
					ord: (selected.children.length + 1),
					parentId : selected.id,
					depth : parseInt(selected.depth) +1,
					children : []
				}]
			});
			
			$('#moniter').tree('toggle', selected.target);
			
			var node = $('#moniter').tree('find', '');
			$('#moniter').tree('select', node.target);
			
			
		},
		
		save : function(){
			var selected = $('#moniter').tree('getSelected');
			if(!selected){
				alert("선택된 노드가 없습니다.");
				return;				
			}
			
			var param = {
				jmIdx : $("#id").val()
		    	, parentId : $("#parendId").val()
		    	, jmName : $("#name").val()
		    	, jmOrder : $("#ord").val()
		    	, jmHref : $("#href").val()
			} 
			
			$.ajax({
				url: '/admin/saveMoniter.json',
				type: 'post',
				cache: false,			
				data: param,
				dataType: 'json',
				success: function(data){
						
					var node = $('#moniter').tree('getSelected');
					if (node){
						$('#moniter').tree('update', {
							target: node.target,
							text: data.jmName,
							id: data.jmIdx,
							depth: data.jmDepth,
							href: data.jmHref
						});
					}
					
					alert("저장 되었습니다.");
				}
			});
	    	
			
		},
		remove : function(){
			var selected = $('#moniter').tree('getSelected');
			
			if(!selected){
				alert("선택된 노드가 없습니다.");
				return;				
			}
			
			if(confirm("("+selected.text +")의 하위 노드까지 모두 삭제됩니다. 삭제하시겠습니까?")){
				
				$.ajax({
					url: '/admin/deleteMoniter.json',
					type: 'post',
					cache: false,			
					data: {
						  idx : selected.id
						, depth : selected.depth
					},
					dataType: 'json',
					success: function(data){
							
					if(data > 0){
						$('#moniter').tree('remove', selected.target);						
						alert("저장 되었습니다.");
					}
						
					}
				});
					
				
			}
		}
}
</script>
