<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<script type="text/javaScript" defer="defer">
	<!--
	$(document).ready(function(){
		$.ajaxSetup({cache: false});
		
		getCode2List('${code.code2}' || 'AUTH', '${code.codenm}' || '권한등급');
		
		$('.code').click(function(){
			var code2 = $(this).parent().attr('code2');
			var codenm = $(this).parent().attr('codenm');
			getCode2List(code2, codenm);
		});
		
		$('.codeInfo .btn').click(function(){
			var table = $(this).closest('.inlinetable').find('table');
			
			if ($(this).prop('open') == true) {
				table.find('.addLine').hide();
				$(this).prop('open', false);
			} else {
				table.find('.addLine').show();
				$(this).prop('open', true);
			}
		});
		$('.inlinetable').on('click', '.addLine input[name=cancel]', function(){
			var tr = $(this).closest('tr');
			tr.hide();
			
			var codeInfo = $(this).closest('table').prev();
			codeInfo.find('.btn').prop('open', false);
		});
		
		$('.inlinetable').on('click', '.addLine input[name=save]', function(){
			var tr = $(this).closest('tr');
			var code1 = tr.find('input[name=code1]').val();
			var code2 = tr.find('input[name=code2]').val();
			var codenm = tr.find('input[name=codenm]').val();
			var codenmYak = tr.find('input[name=codenmYak]').val();
			var codeUseYN = tr.find('input[name=codeUseYN]').prop('checked');
			
			if(code1 && code2 && codenm) {
				$.ajax({
					url : '/admin/insertCode.json',
					type : 'get',
					data : { 'code1' : code1, 'code2' : code2, 'codenm' : codenm, 'codenmYak' : codenmYak, 'codeUseYN' : 'Y' },
					dataType : 'json',
					success : function(data) {
						if(data.result == '1'){
							alert('코드가 추가되었습니다.');
						} else {
							alert('코드가 추가에 실패하였습니다.');
						}
						fn_searchList();
					}
				});
			}
		});
		
		$('.inlinetable').on('click', 'input[name=codeUseYN]', function(){
			var flag = false;
			if($(this).prop('checked')){
				flag = confirm('사용여부를 설정하시겠습니까?');
			} else {
				flag = confirm('사용여부를 해제하시겠습니까?');
			}
			if(flag) {
				updateCode(this);
			} else {
				$(this).prop('checked', !$(this).prop('checked'));
			}
		});
	});
	
	function getCode2List(code1, codenm){
		$.ajax({
			url : '/admin/getCode2List.json',
			type : 'get',
			data : { 'code1' : code1 },
			dataType : 'json',
			success : function(data) {
				setCode2Info(code1, codenm);
				listingCode2(data.code2List);
			}
		});
	}

	var opened = false;
	var openedTarget = null;
	function updateCodeView(self){
		if (opened) {
			cancelUpdateCode(openedTarget);
		}
		openUpdateCode(self);
	}
	function openUpdateCode(self){
		var tr = $(self).closest('tr');
		tr.find('.edit').show();
		tr.find('.view').hide();
		opened = true;
		openedTarget = self;
	}
	function cancelUpdateCode(self){
		var tr = $(self).closest('tr');
		var codenm = tr.find('input[name=codenm_temp]').val();
					 tr.find('input[name=codenm]').val(codenm);
		
		tr.find('.edit').hide();
		tr.find('.view').show();
		opened = false;
	}
	function updateCode(self){
		var tr = $(self).closest('tr');
		var code1 = tr.find('input[name=code1]').val();
		var code2 = tr.find('input[name=code2]').val();
		var codenm = tr.find('input[name=codenm]').val();
		var codenmYak = tr.find('input[name=codenmYak]').val();
		var codeUseYN = tr.find('input[name=codeUseYN]').prop('checked');
		
		$.ajax({
			url : '/admin/updateCode.json',
			type : 'get',
			data : { 
				'code1' : code1, 
				'code2' : code2, 
				'codenm' : codenm, 
				'codenmYak' : codenmYak,
				'codeUseYN' : (codeUseYN) ? 'Y' : 'N' 
			},
			dataType : 'json',
			success : function(data) {
				if(data.result == '1'){
					alert('코드가 수정되었습니다.');
				} else {
					alert('코드가 수정에 실패하였습니다.');
				}
				fn_searchList();
			}
		});
	}
	function deleteCode(self){
		var flag = confirm('선택한 코드를 삭제하시겠습니까? 하위 코드 역시 삭제됩니다.');
		if(flag){
			var tr = $(self).closest('tr');
			var code1 = tr.find('input[name=code1]').val();
			var code2 = tr.find('input[name=code2]').val();
			var codenm = tr.find('input[name=codenm]').val();
			var codenmYak = tr.find('input[name=codenmYak]').val();
			var codeUseYN = tr.find('input[name=codeUseYN]').prop('checked');
			
			$.ajax({
				url : '/admin/deleteCode.json',
				type : 'get',
				data : { 
					'code1' : code1, 
					'code2' : code2
				},
				dataType : 'json',
				success : function(data) {
					if(data.result > 0){
						alert('코드가 삭제되었습니다.');
					} else {
						alert('코드가 삭제에 실패하였습니다.');
					}
					location.href = "/admin/codeManage.do";
				}
			});
		}
	}
	
	
	
    function setCode2Info(code1, codenm){
		$('#code2Info .type').html(code1 + ' ' + codenm);
		$('#code2Info input[name=code1]').val(code1);
	}
	
	function listingCode2(list){
		var tbody = $('#code2Table tbody').empty();
		var tr = '';
		
		tr += '<tr class="addLine">' +
					'<td class="listtd">NEW</td>' +
					'<td class="listtd">' +
						'<input name="code1" type="hidden" value="' + $('#code2Info input[name=code1]').val() + '">' +
						'<input name="code2" type="text">' +
					'</td>' +
					'<td class="listtd w-100"><input name="codenm" type="text"></td>' +
					'<td class="listtd" colspan="2"><input name="codenmYak" type="text"></td>' +
					'<td class="listtd"><input name="save" type="button" value="저장"></td>' +
					'<td class="listtd"><input name="cancel" type="button" value="취소"></td>' +
					<!--'<td class="listtd"></td>' +-->
				'</tr>';
				
		if (list.length > 0) {
			
			for(var i = 0; i < list.length; i++){
				var checked = (list[i].codeUseYN == 'Y') ? 'checked' : '';
				
				tr = tr + '<tr>' +
						'<td class="listtd" align="center">' + (i+1) + '</td>' +
						'<td class="listtd" align="center">' +
							'<span>' + list[i].code2 + '</span>' +
							'<input type="hidden" name="code1" value="' + list[i].code1 + '"/>' +
							'<input type="hidden" name="code2" value="' + list[i].code2 + '"/>' +
						'</td>' +
						'<td class="listtd codeNm">' +
							'<span class="view">' + list[i].codenm + '</span>' +
							'<input class="edit" type="text" name="codenm" value="' + list[i].codenm + '">' +
							'<input class="edit" type="hidden" name="codenm_temp" value="' + list[i].codenm + '">' +
						'</td>' +
						'<td class="listtd codeYak">' +
							'<span class="view">' + list[i].codenmYak + '</span>' +
							'<input class="edit" type="text" name="codenmYak" value="' + list[i].codenmYak + '">' +
							'<input class="edit" type="hidden" name="codenmYak_temp" value="' + list[i].codenmYak + '">' +
						'</td>' +
						'<td class="listtd codeEnable" align="center">' +
							'<input type="checkbox" name="codeUseYN" ' + checked + '/>' +
						'</td>' +
						'<td class="listtd codeModify">' +
							'<input class="view" type="button" value="수정" onclick="updateCodeView(this)">' +
							'<input class="edit" type="button" value="확인" onclick="updateCode(this)">' +
							'<input class="edit" type="button" value="취소" onclick="cancelUpdateCode(this)">' +
						'</td>' +
						'<td class="listtd code1Delete"><input type="button" value="삭제" onclick="deleteCode(this)"></td>' +
					'</tr>';
			}
		}
		tbody.html(tr);
	}
	
	
	/* 글 목록 화면 function */
    function fn_searchList() {
       	document.searchForm.action = "<c:url value='/admin/codeManage.do'/>";
       	document.searchForm.submit();
    }
	
	/* 검색초기화 function */
    function fn_selectList() {
       	location.href = "/admin/codeManage.do";
    }
	//-->
</script>

<!-- List -->
<div id="table" class="clear">	
	<!-- 대분류 코드 구분 -->
	<div class="inlinetable">
		<div class="codeInfo">		
			<img src="/images/cmmn/title.png" alt="icon">
			<span>대분류 코드 구분</span>	
			<input class="value" type="hidden" value=""/>
			<input class="code_btn btn" type="button" value="대분류 코드 추가"/>
		</div>
		<table class="code_list">
			<colgroup>
	   			<col width="57">
	   			<col width="60">
	   			<col width="195">
	   			<col width="68">
	   			<col width="50">
	   			<col width="50">
	   		</colgroup>
			<thead>
				<tr>
					<th align="center"><spring:message code="code.view.rnum" /></th>
					<th align="center">
						<span class="order" value="<spring:message code="member.value.order.place1"/>">
							<spring:message code="code.view.code" />
						</span>
					</th>
					<th align="center">
						<span class="order" value="<spring:message code="member.value.order.position"/>">
							<spring:message code="code.view.codenm" />
						</span>
					</th>
					<th align="center">
						<span class="order" value="<spring:message code="member.value.order.userid"/>">
							<spring:message code="code.view.enabled" />
						</span>
					</th>
					<th align="center">
						<span class="order" value="<spring:message code="member.value.order.name"/>">
							<spring:message code="code.view.modify" />
						</span>
					</th>
					<th align="center">
						<span class="order" value="<spring:message code="member.value.order.name"/>">
							<spring:message code="code.view.delete" />
						</span>
					</th>
				</tr>
			</thead>
			<tbody>
				<tr class="addLine">
					<td>NEW</td>
					<td class="input_text">
						<input name="code1" type="hidden" value="${code1List[0].code1}">
						<input name="code2" type="text">
					</td>
					<td class="input_text" colspan="2"><input name="codenm" type="text"></td>
					<td align="center"><input name="save" type="button" value="저장"></td>
					<td align="center"><input name="cancel" type="button" value="취소"></td>
				</tr>
				<c:forEach items="${code1List}" var="item" varStatus="status">					
					<tr codenm="${item.codenm}" code2="${item.code2}">
						<td align="center">${status.count}</td>
						<td align="center" class="code">
							<span>${item.code2}</span>
							<input type="hidden" name="code1" value="${item.code1}"/>
							<input type="hidden" name="code2" value="${item.code2}"/>
						</td>
						<td align="left" class="codeNm">
							<span class="view">${item.codenm}</span>
							<input class="edit" type="text" name="codenm" value="${item.codenm}">
							<input class="edit" type="hidden" name="codenm_temp" value="${item.codenm}">
						</td>
						<td align="center" class="codeEnable">
							<input type="checkbox" name="codeUseYN" ${item.codeUseYN == 'Y' ? 'checked': ''} />
						</td>
						<td align="center"  class="codeModify">
							<input class="view" type="button" value="수정" onclick="updateCodeView(this)">
							<input class="edit" type="button" value="확인" onclick="updateCode(this)">
							<input class="edit" type="button" value="취소" onclick="cancelUpdateCode(this)">
						</td>
						<td align="center" class="codeDelete"><input type="button" value="삭제" onclick="deleteCode(this)"></td>
					</tr>
				</c:forEach>
			</tbody>
		</table>			
	</div>
	<!-- 대분류 코드 구분 끝 -->
	
	<!-- AUTH 권한등급 -->
	<div class="inlinetable">
		<form name="searchForm" method="get">
		<div id="code2Info" class="codeInfo">	
			<img src="/images/cmmn/title.png" alt="icon">
			<span class="type"></span>
			<input name="code1" type="hidden" value="${code.code2}"/>
			<input class="code_btn btn" type="button" value="코드 추가"/>
		</div>
		</form>
		<table class="code_list" id="code2Table" border="0" cellpadding="0" cellspacing="0">
			<colgroup>
	   			<col width="57">
	   			<col width="60">
	   			<col width="">	   			
	   			<col width="">
	   			<col width="68">
	   			<col width="50">
	   			<col width="50">
	   		</colgroup>
			<thead>
				<tr>
					<th align="center"><spring:message code="code.view.rnum" /></th>
					<th align="center">
						<span class="order" value="<spring:message code="member.value.order.place1"/>">
							<spring:message code="code.view.code" />
						</span>
					</th>
					<th align="center">
						<span class="order" value="<spring:message code="member.value.order.position"/>">
							<spring:message code="code.view.codenm" />
							<img src="/images/cmmn/blank_1x1.png" />
						</span>
					</th>
					<th align="center">
						<span class="order" value="<spring:message code="member.value.order.position"/>">
							<spring:message code="code.view.codenmYak" />
						</span>
					</th>
					<th align="center">
						<span class="order" value="<spring:message code="member.value.order.userid"/>">
							<spring:message code="code.view.enabled" />
						</span>
					</th>
					<th align="center">
						<span class="order" value="<spring:message code="member.value.order.name"/>">
							<spring:message code="code.view.modify" />
						</span>
					</th>
					<th align="center">
						<span class="order" value="<spring:message code="member.value.order.name"/>">
							<spring:message code="code.view.delete" />
						</span>
					</th>
				</tr>
			</thead>
			<tbody>
			</tbody>
		</table>		
	</div>
</div>
<!-- /List -->
