<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html lang="ko">
<head>
	<title>메뉴권한관리</title>
	<%@ include file="/WEB-INF/jsp/adm/include/topLinkTag.jsp" %>
</head>
<body>
<h1>ICOMSYS ADMIN</h1>

<div class="skip_navigation">
	<ul>
		<li>
			<a href="#Content" class="go_content">본문 바로가기</a>
		</li>
		<li>
			<a href="#Gnb">메뉴 바로가기</a>
		</li>
	</ul>
</div>

<header id="header">
	<%@ include file="/WEB-INF/jsp/adm/include/header.jsp" %>
</header>

<c:import url="/adm/menu/getLnbMenu.do" />

<script>
$(document).ready( function () {

	// 권한 조회
	$('#btnSearchCust').on('click',function() {
		fncSearchCust();
    });

	// 신규 권한 저장
	$('#btnSaveCust').on('click',function() {
		fncCreateCustInfo();
    });

	// 권한 수정
	$('#btnEditCust').on('click',function() {
		fncUpdateCustInfo();
    });

	// 권한 삭제
	$('#btnDelCust').on('click',function() {
		// scripts.js : swal(alert) 공통 알림
		swalAlert('warning', "삭제", fncDeleteService);
    });

	// 추가 modal
	$('#btnRegModal').on('click',function() {
		// 초기화
		$('#serviceInfo').reset();
    });

	// 권한 리스트 그리기
	fncSearchCust();
});

function fncKeyPresshandler(e) {
	if (event.keyCode == 13) {
		fncSearchCust();
		return false;
	}
}

// 권한 리스트 그리기
function fncSearchCust() {

	$('#listTable tbody').empty();

	var param = "searchKeyword=" + $('#keyword').val();
	var resultTr = '';

	$.ajax({
		type : "post",
		async : false,
		datatype : "text",
		url : "<c:url value='/admin/system/retrieveAuthList.do'/>",
		data : param,
		success : function(data){

			$.each($.parseJSON(data.nMap), function(i, val){
				i += 1;

				resultTr += '<tr class="modalLoad" rel="reg_edit_modal" data-auth-code="' + val.authCode
							+ '" data-auth-name="' + val.authName + '" data-auth-desc="' + val.authDesc
							+ '" data-regist-date="' + val.registDate + '">';
				resultTr += '	<td>';
				resultTr += '		<div class="chk_box">';
				resultTr += '			<input type="checkbox" name="checkbox" id="chk'+ i +'" value="off">';
				resultTr += '			<label for="chk'+ i +'"></label>';
				resultTr += '		</div>';
				resultTr += '	</td>';
				resultTr += '	<td class="tl">' + val.authCode + '</td>';
				resultTr += '	<td class="tl">' + val.authName + '</td>';
				resultTr += '	<td class="tl">' + val.authDesc + '</td>';
				resultTr += '	<td>' + val.registDate + '</td>';
				resultTr += '</tr>';
			});

			$('#listTable tbody').append(resultTr);

			// 권한 수정 modal open event
			$('#listTable tbody tr').on('dblclick', function() {
				fncSearchDetail($(this));
			});

			// scripts.js : 권한 list modal 연결
			modalUi();

			// scripts.js : 체크 박스 체크에 따른 '모두 체크' 변경
			checkAllChecked();
		}
	});
}

// 수정 modal
function fncSearchDetail(tr){
	// 값 가져오기
	var authCode = $(tr).data('authCode');
	var authName = $(tr).data('authName');
	var authDesc = $(tr).data('authDesc');

	// 값 채우기
	$('#authCode').val(authCode);
	$('#authName').val(authName);
	$('#authDesc').text(authDesc);
}

// 신규 권한 저장
function fncCreateCustInfo() {
	if($('input[name="authCode"]').val() == '') {
		swalAlert('info', '권한코드는 필수 항목입니다.');
	}
	else if($('input[name="authName"]').val() == '') {
		swalAlert('info', '권한명은 필수 항목입니다.');
	}
	else {
		swalAlert('warning', '저장', insertCustInfo);
	}
}

// 권한 insert
function insertCustInfo() {
	var param = $('#serviceInfo').serialize();
	param += "&jobCudType=insert";

	$.ajax({
		type : "post",
		async : false,
		datatype : "text",
		url : "<c:url value='/admin/system/authWrite.do'/>",
		data : param,
		success : function(data) {
			swalAlert('success', '저장');
		}
	});

	// edit_modal close
	$('#reg_modal a.close').trigger('click');

	// 권한 리스트 그리기
	fncSearchCust();
}

// 기존 권한 수정
function fncUpdateCustInfo() {
	if($('#authCode').val() == '') {
		swalAlert('info', '권한명은 필수 항목입니다.');
	}
	else {
		swalAlert('warning', '수정', updateCustInfo);
	}
}

//권한 update
function updateCustInfo() {
	var param = $('#editServiceInfo').serialize();
		param += "&jobCudType=update";

	$.ajax({
		type : "post",
		async : false,
		datatype : "text",
		url : "<c:url value='/admin/system/authWrite.do'/>",
		data : param,
		success : function(data) {
			swalAlert('success', '수정');
		}
	});

	// edit_modal close
	$('#reg_edit_modal a.close').trigger('click');

	// 권한 리스트 그리기
	fncSearchCust();
}

// 권한 삭제
function fncDeleteService() {
	var checkbox = $("input[name=checkbox]:checked");

	var param = '';
	$.each(checkbox, function(i, val){
		if(i == 0) {
			param += 'authCodes=' + $(val).parents('tr').data('authCode');
		}
		else {
			param += ';' + $(val).parents('tr').data('authCode');
		}
	});

	$.ajax({
		type : "post",
		async : false,
		datatype : "text",
		url : "<c:url value='/admin/system/authListDelete.do'/>",
		data : param,
		success : function(data) {
			if(data.result == 'success') {
				swalAlert('success', '삭제');

				// 권한 리스트 그리기
				fncSearchCust();
			}
			else {
				swalAlert('info', '삭제하시는 권한의 하위권한를 먼저 삭제해주세요.');
			}
		}
	});

}
</script>
<section id="Content" class="system_management">
    <div class="location">
        <ul>
            <li class="home"><a href="#">HOME</a></li>
            <li><a href="#">시스템관리</a></li>
            <li><a href="#">메뉴권한관리</a></li>
        </ul>
    </div>
    <div class="sub_cont menu06_01_01">
        <div class="cont_box">
            <div class="box_title">
                <div class="fl">
                    <h2 class="mt12">권한 조회</h2>
                </div>
                <div class="btn_wrap fr">
                	<a href="#" id="btnSearchCust" class="btn">조회</a>
                </div>
            </div>
            <div class="tbl">
                <table class="search">
                    <colgroup>
                        <col width="130px">
                        <col width="auto">
                    </colgroup>
                    <tbody>
                    <tr>
                        <th class="tl">권한명</th>
                        <td class="tl">
                            <div class="ipt_box">
                                <input type="text" placeholder="권한명을 입력해주세요." id="keyword" class="" onkeypress="return fncKeyPresshandler(event);">
                            </div>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="cont_box">
            <div class="box_title">
                <div class="fl">
                    <h2 class="mt12">권한 리스트</h2>
                </div>
                <div class="btn_wrap fr">
                    <a href="#" class="btn grey" id="btnDelCust">삭제</a>
                    <a href="#" rel="reg_modal" class="btn modalLoad" id="btnRegModal">추가</a>
                </div>
            </div>
            <div class="tbl">
            	<form id="custListForm">
	                <table id="listTable" class="">
	                    <colgroup>
	                        <col width="60px">
	                        <col width="23%">
	                        <col width="23%">
	                        <col width="auto">
	                        <col width="170px">
	                    </colgroup>
	                    <thead>
	                    <tr>
	                        <th>
	                            <div class="chk_box">
	                                <input type="checkbox" name="all" id="chk00"  value="">
	                                <label for="chk00"></label>
	                            </div>
	                        </th>
	                        <th>권한코드</th>
	                        <th>권한명</th>
	                        <th>설명</th>
	                        <th>등록일자</th>
	                    </tr>
	                    </thead>
	                    <tbody>
<%--
	                    <tr class="modalLoad" rel="reg_edit_modal">
	                        <td>
	                            <div class="chk_box">
	                                <input type="checkbox" name="checkbox" id="chk01"  value="">
	                                <label for="chk01"></label>
	                            </div>
	                        </td>
	                        <td class="tl">IS_AUTHENTICATED_ANONYMOUSLY</td>
	                        <td class="tl">[Egov정의]인명사용자</td>
	                        <td class="tl">[스프링기본권한]익명사용자(롤부여금지)</td>
	                        <td>2019-04-09 21:11:33</td>
	                    </tr>
	                    <tr class="modalLoad" rel="reg_edit_modal">
	                        <td>
	                            <div class="chk_box">
	                                <input type="checkbox" name="checkbox" id="chk02"  value="">
	                                <label for="chk02"></label>
	                            </div>
	                        </td>
	                        <td class="tl">IS_AUTHENTICATED_ANONYMOUSLY</td>
	                        <td class="tl">[Egov정의]인명사용자</td>
	                        <td class="tl">[스프링기본권한]익명사용자(롤부여금지)</td>
	                        <td>2019-04-09 21:11:33</td>
	                    </tr>
	                    <tr class="modalLoad" rel="reg_edit_modal">
	                        <td>
	                            <div class="chk_box">
	                                <input type="checkbox" name="checkbox" id="chk03"  value="">
	                                <label for="chk03"></label>
	                            </div>
	                        </td>
	                        <td class="tl">IS_AUTHENTICATED_ANONYMOUSLY</td>
	                        <td class="tl">[Egov정의]인명사용자</td>
	                        <td class="tl">[스프링기본권한]익명사용자(롤부여금지)</td>
	                        <td>2019-04-09 21:11:33</td>
	                    </tr>
	                    <tr class="modalLoad" rel="reg_edit_modal">
	                        <td>
	                            <div class="chk_box">
	                                <input type="checkbox" name="checkbox" id="chk04"  value="">
	                                <label for="chk04"></label>
	                            </div>
	                        </td>
	                        <td class="tl">IS_AUTHENTICATED_ANONYMOUSLY</td>
	                        <td class="tl">[Egov정의]인명사용자</td>
	                        <td class="tl">[스프링기본권한]익명사용자(롤부여금지)</td>
	                        <td>2019-04-09 21:11:33</td>
	                    </tr>
	                    <tr class="modalLoad" rel="reg_edit_modal">
	                        <td>
	                            <div class="chk_box">
	                                <input type="checkbox" name="checkbox" id="chk05"  value="">
	                                <label for="chk05"></label>
	                            </div>
	                        </td>
	                        <td class="tl">IS_AUTHENTICATED_ANONYMOUSLY</td>
	                        <td class="tl">[Egov정의]인명사용자</td>
	                        <td class="tl">[스프링기본권한]익명사용자(롤부여금지)</td>
	                        <td>2019-04-09 21:11:33</td>
	                    </tr>
	                    <tr class="modalLoad" rel="reg_edit_modal">
	                        <td>
	                            <div class="chk_box">
	                                <input type="checkbox" name="checkbox" id="chk06"  value="">
	                                <label for="chk06"></label>
	                            </div>
	                        </td>
	                        <td class="tl">IS_AUTHENTICATED_ANONYMOUSLY</td>
	                        <td class="tl">[Egov정의]인명사용자</td>
	                        <td class="tl">[스프링기본권한]익명사용자(롤부여금지)</td>
	                        <td>2019-04-09 21:11:33</td>
	                    </tr>
	                    <tr class="modalLoad" rel="reg_edit_modal">
	                        <td>
	                            <div class="chk_box">
	                                <input type="checkbox" name="checkbox" id="chk07"  value="">
	                                <label for="chk07"></label>
	                            </div>
	                        </td>
	                        <td class="tl">IS_AUTHENTICATED_ANONYMOUSLY</td>
	                        <td class="tl">[Egov정의]인명사용자</td>
	                        <td class="tl">[스프링기본권한]익명사용자(롤부여금지)</td>
	                        <td>2019-04-09 21:11:33</td>
	                    </tr>
	                    <tr class="modalLoad" rel="reg_edit_modal">
	                        <td>
	                            <div class="chk_box">
	                                <input type="checkbox" name="checkbox" id="chk08"  value="">
	                                <label for="chk08"></label>
	                            </div>
	                        </td>
	                        <td class="tl">IS_AUTHENTICATED_ANONYMOUSLY</td>
	                        <td class="tl">[Egov정의]인명사용자</td>
	                        <td class="tl">[스프링기본권한]익명사용자(롤부여금지)</td>
	                        <td>2019-04-09 21:11:33</td>
	                    </tr>
	                    <tr class="modalLoad" rel="reg_edit_modal">
	                        <td>
	                            <div class="chk_box">
	                                <input type="checkbox" name="checkbox" id="chk09"  value="">
	                                <label for="chk09"></label>
	                            </div>
	                        </td>
	                        <td class="tl">IS_AUTHENTICATED_ANONYMOUSLY</td>
	                        <td class="tl">[Egov정의]인명사용자</td>
	                        <td class="tl">[스프링기본권한]익명사용자(롤부여금지)</td>
	                        <td>2019-04-09 21:11:33</td>
	                    </tr>
	                    <tr class="modalLoad" rel="reg_edit_modal">
	                        <td>
	                            <div class="chk_box">
	                                <input type="checkbox" name="checkbox" id="chk10"  value="">
	                                <label for="chk10"></label>
	                            </div>
	                        </td>
	                        <td class="tl">IS_AUTHENTICATED_ANONYMOUSLY</td>
	                        <td class="tl">[Egov정의]인명사용자</td>
	                        <td class="tl">[스프링기본권한]익명사용자(롤부여금지)</td>
	                        <td>2019-04-09 21:11:33</td>
	                    </tr>
	<!--                    <tr>-->
	<!--                        <td colspan="5">변경된 전처리 리스트가 없습니다.</td>-->
	<!--                    </tr>-->
--%>
	                    </tbody>
	                </table>
                </form>
            </div>
        </div>
    </div>
</section>

<!--  modal :: 권한 등록 팝업  -->
<div class="modal" id="reg_modal">
    <div class="modal_header">
        <h3>권한 신규등록</h3>
    </div>
    <div class="modal_body">
        <div class="modal_cont">
	        <form id="serviceInfo">
	            <dl>
	                <dt><h4><em class="important">권한코드</em></h4></dt>
	                <dd>
	                    <div class="ipt_box">
	                        <input type="text" name="authCode" placeholder="권한코드를 입력하세요.">
	                    </div>
	                </dd>
	            </dl>
	            <dl class="">
	                <dt><h4><em class="important">권한명</em></h4></dt>
	                <dd>
	                    <div class="ipt_box">
	                        <input type="text" name="authName" placeholder="권한명 입력하세요.">
	                    </div>
	                </dd>
	            </dl>
	            <dl class="">
	                <dt><h4><em class="">권한 설명</em></h4></dt>
	                <dd>
	                    <textarea name="authDesc"></textarea>
	                </dd>
	            </dl>
            </form>
        </div>
    </div>
    <div class="modal_footer">
        <div class="btn_wrap fr">
            <a href="#" class="btn" id="btnSaveCust">저장</a>
        </div>
    </div>
    <div class="modal_controller">
        <a href="#" class="close">close</a>
    </div>
</div>
<!--  modal :: 권한 등록 팝업 end -->

<!--  modal :: 권한 수정 팝업  -->
<div class="modal" id="reg_edit_modal">
    <div class="modal_header">
        <h3>권한 수정</h3>
    </div>
    <div class="modal_body">
        <div class="modal_cont">
	        <form id="editServiceInfo">
	            <dl>
	                <dt><h4><em class="">권한코드</em></h4></dt>
	                <dd>
	                    <div class="ipt_box">
	                        <input type="text" id="authCode" name="authCode" value="IS_AUTHENTICATED_ANONYMOUSLY" readonly>
	                    </div>
	                </dd>
	            </dl>
	            <dl class="">
	                <dt><h4><em class="important">권한명</em></h4></dt>
	                <dd>
	                    <div class="ipt_box">
	                        <input type="text" id="authName" name="authName" value="[Egov정의]인명사용자">
	                    </div>
	                </dd>
	            </dl>
	            <dl class="">
	                <dt><h4><em class="">권한 설명</em></h4></dt>
	                <dd>
	                    <textarea id="authDesc" name="authDesc">[스프링기본권한]익명사용자(롤부여금지)</textarea>
	                </dd>
	            </dl>
            </form>
        </div>
    </div>
    <div class="modal_footer">
        <div class="btn_wrap fr">
            <a href="#" id="btnEditCust" class="btn">저장</a>
        </div>
    </div>
    <div class="modal_controller">
        <a href="#" class="close">close</a>
    </div>
</div>
<!--  modal :: 권한 수정 팝업 end -->

</body>
</html>