<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : MenuForm.html
 * @desc    : 메뉴관리 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.10.08   gagamel     최초 작성
 *******************************************************************************
 -->
	<div id="main">
		<!-- 메인타이틀 영역 -->
		<div class="main-title">
		</div>
		<!-- //메인타이틀 영역 -->
		
		<!-- 메뉴 설명 -->
		<div class="infoBox menu-desc">
		</div>
		<!-- //메뉴 설명 -->
		
		<!-- 검색조건 영역 -->
		<div class="panelStyle">
			<!-- Search -->
			<form id="searchForm" name="searchForm" action="#" th:action="@{'/system/menu/list'}" onsubmit="$('#btnSearch').trigger('click'); return false;">
				<table class="frmStyle" aria-describedby="검색조건">
					<colgroup>
						<col style="width:10%;"/>
						<col style="width:90%;"/>
					</colgroup>
					<tr>
						<th>상위메뉴</th>
						<td>
							<select name="pmenuId" class="w150">
								<option value="">[전체]</option>
								<option th:if="${topMenuList}" th:each="oneData, status : ${topMenuList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
					</tr>
				</table>
				
				<ul class="panelBar">
					<li class="center">
						<button type="button" class="btn btn-success btn-lg" id="btnSearch">조회</button>
					</li>
				</ul>
			</form>
		</div>
		<!-- //검색조건 영역 -->
		
		<!-- TABS AREA -->
		<div class="tabs">
			<ul class="tabsNav">
				<li class="on"><a href="#tab-1">목록</a></li>
				<li><a href="#tab-2">신규</a></li>
				<li class="off"><a href="#tab-3">상세</a></li>
			</ul>
			
			<ul class="tabsCont">
				<li class="tab on" id="tab-1">
					<div class="panelStyle">
						<!-- 버튼 배치 영역 -->
						<ul class="panelBar">
							<li class="right">
								<button type="button" class="btn btn-danger btn-lg btnDelete">삭제</button>
							</li>
						</ul>
						<!-- //버튼 배치 영역 -->
						
						<div id="gridList" style="width: 100%; height: 570px;" class="ag-theme-balham"></div>
					</div>
				</li>
				
				<!-- 신규 -->
				<li class="tab" id="tab-2">
					<div class="panelStyle">
						<form id="registerForm" name="registerForm" action="#" th:action="@{'/system/menu/save'}">
							<table class="frmStyle" aria-describedby="신규">
								<colgroup>
									<col style="width:10%"/>
									<col style="width:40%;"/>
									<col style="width:10%;"/>
									<col/>
								</colgroup>
								<tr>
									<th>메뉴ID<i class="required" title="필수" aria-hidden="true"></i></th>
									<td>
										<input type="text" name="menuId" placeholder="" maxlength="20" required="required" data-valid-type="alphaNumeric" data-valid-name="메뉴ID" onkeyup="$(this).val($(this).val().toUpperCase());"/>
									</td>
									<th>메뉴명<i class="required" title="필수" aria-hidden="true"></i></th>
									<td>
										<input type="text" name="menuNm" placeholder="" maxlength="50" required="required" data-valid-name="메뉴명"/>
									</td>
								</tr>
								<tr>
									<th>메뉴구분<i class="required" title="필수" aria-hidden="true"></i></th>
									<td>
										<label class="rdoBtn"><input type="radio" name="menuGb" value="M"/>메뉴</label>
										<label class="rdoBtn"><input type="radio" name="menuGb" value="P"/>프로그램</label>
									</td>
									<th>상위메뉴</th>
									<td>
										<select name="pmenuId">
											<option value="">[선택]</option>
											<option th:if="${allMenuList}" th:each="oneData, status : ${allMenuList}" th:value="${oneData.cd}" th:text="${oneData.cdNm}"></option>
										</select>
									</td>
								</tr>
								<tr class="menu-role">
									<th>메뉴권한</th>
									<td colspan="3">
										<!-- <select class="chosen-select" name="roleCd" multiple="multiple" data-placeholder="[선택]" data-valid-name="메뉴권한">
											<option th:if="${roleList}" th:each="oneData, status : ${roleList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
										</select> -->
										<div class="mSelectWrap">
											<select name="roleCd" multiple="multiple" tabindex="-1" data-valid-name="메뉴권한">
												<option th:if="${roleList}" th:each="oneData, status : ${roleList}" th:attr="data-index=${status.index + 1}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
											</select>
											<div class="mSelectBox">
												<ul class="mSelected">
													<li class="off" th:if="${roleList}" th:each="oneData, status : ${roleList}" th:attr="data-index=${status.index + 1}" th:utext="${'[' + oneData.cd + '] ' + oneData.cdNm + '&lt;a&gt;닫기&lt;/a&gt;'}"></li>
													<li class="srchFld"><input type="text" autocomplete="off"/></li>
												</ul>
												<div class="mSelecting">
													<ul>
														<li th:if="${roleList}" th:each="oneData, status : ${roleList}" th:attr="data-index=${status.index + 1}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></li>
													</ul>
												</div>
											</div>
										</div>
										<input type="hidden" name="roleCds"/>
									</td>
								</tr>
								<tr>
									<th>메뉴URL</th>
									<td colspan="3">
										<input type="text" name="menuUrl" placeholder="" maxlength="100"/>
									</td>
								</tr>
								<tr>
									<th>메뉴설명</th>
									<td colspan="3">
										<textarea class="textareaR4" name="menuDesc"></textarea>
									</td>
								</tr>
								<tr>
									<th>표시순서<i class="required" title="필수" aria-hidden="true"></i></th>
									<td>
										<input type="text" class="w100 text-right" name="dispOrd" placeholder="" maxlength="5" required="required" data-valid-type="numeric" data-valid-name="표시순서" />
									</td>
									<th>사용여부</th>
									<td>
										<input type="hidden" name="useYn"/>
										<label class="chkBox"><input type="checkbox" name="chkUseYn" value="Y" checked="checked" disabled="disabled"/>사용</label>
									</td>
								</tr>
							</table>
						</form>
						
						<!-- 버튼 배치 영역 -->
						<ul class="panelBar">
							<li class="right">
								<input type="button" value="초기화" class="btn btn-gray btn-lg" onclick="$('#registerForm')[0].reset();"/>
								<input type="button" value="저장" class="btn btn-base btn-lg" onclick="fnSave('#registerForm');"/>
							</li>
						</ul>
						<!-- //버튼 배치 영역 -->
					</div>
				</li>
				<!-- //신규 -->
				
				<!-- 상세 -->
				<li class="tab" id="tab-3">
					<div class="panelStyle">
						<form id="detailForm" name="detailForm" action="#" th:action="@{'/system/menu/save'}">
							<table class="frmStyle" aria-describedby="상세">
								<colgroup>
									<col style="width:10%"/>
									<col style="width:40%;"/>
									<col style="width:10%;"/>
									<col/>
								</colgroup>
								<tr>
									<th>메뉴ID</th>
									<td>
										<input type="text" name="menuId" placeholder="" maxlength="20" readonly="readonly"/>
									</td>
									<th>메뉴명<i class="required" title="필수" aria-hidden="true"></i></th>
									<td>
										<input type="text" name="menuNm" placeholder="" maxlength="50" required="required" data-valid-name="메뉴명"/>
									</td>
								</tr>
								<tr>
									<th>메뉴구분<i class="required" title="필수" aria-hidden="true"></i></th>
									<td>
										<label class="rdoBtn"><input type="radio" name="menuGb" value="M"/>메뉴</label>
										<label class="rdoBtn"><input type="radio" name="menuGb" value="P"/>프로그램</label>
									</td>
									<th>상위메뉴</th>
									<td>
										<select name="pmenuId">
											<option value="">[선택]</option>
											<option th:if="${allMenuList}" th:each="oneData, status : ${allMenuList}" th:value="${oneData.cd}" th:text="${oneData.cdNm}"></option>
										</select>
									</td>
								</tr>
								<tr class="menu-role">
									<th>메뉴권한</th>
									<td colspan="3">
										<!-- <select class="chosen-select" name="roleCd" multiple="multiple" data-placeholder="[선택]" data-valid-name="메뉴권한">
											<option th:if="${roleList}" th:each="oneData, status : ${roleList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
										</select> -->
										<div class="mSelectWrap">
											<select name="roleCd" multiple="multiple" tabindex="-1" data-valid-name="메뉴권한">
												<option th:if="${roleList}" th:each="oneData, status : ${roleList}" th:attr="data-index=${status.index + 1}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
											</select>
											<div class="mSelectBox">
												<ul class="mSelected">
													<li class="off" th:if="${roleList}" th:each="oneData, status : ${roleList}" th:attr="data-index=${status.index + 1}" th:utext="${'[' + oneData.cd + '] ' + oneData.cdNm + '&lt;a&gt;닫기&lt;/a&gt;'}"></li>
													<li class="srchFld"><input type="text" autocomplete="off"/></li>
												</ul>
												<div class="mSelecting">
													<ul>
														<li th:if="${roleList}" th:each="oneData, status : ${roleList}" th:attr="data-index=${status.index + 1}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></li>
													</ul>
												</div>
											</div>
										</div>
										<input type="hidden" name="roleCds"/>
									</td>
								</tr>
								<tr>
									<th>메뉴URL</th>
									<td colspan="3">
										<input type="text" name="menuUrl" placeholder="" maxlength="100"/>
									</td>
								</tr>
								<tr>
									<th>메뉴설명</th>
									<td colspan="3">
										<textarea class="textareaR4" name="menuDesc"></textarea>
									</td>
								</tr>
								<tr>
									<th>표시순서<i class="required" title="필수" aria-hidden="true"></i></th>
									<td>
										<input type="text" class="w100 text-right" name="dispOrd" placeholder="" maxlength="5" required="required" data-valid-type="numeric" data-valid-name="표시순서" />
									</td>
									<th>사용여부<i class="required" title="필수" aria-hidden="true"></i></th>
									<td>
										<input type="hidden" name="useYn"/>
										<label class="chkBox"><input type="checkbox" name="chkUseYn" value="Y"/>사용</label>
									</td>
								</tr>
							</table>
						</form>
						
						<!-- 버튼 배치 영역 -->
						<ul class="panelBar">
							<li class="right">
								<input type="button" value="초기화" class="btn btn-gray btn-lg" onclick="$('#detailForm')[0].reset();"/>
								<input type="button" value="저장" class="btn btn-base btn-lg" onclick="fnSave('#detailForm');"/>
							</li>
						</ul>
						<!-- //버튼 배치 영역 -->
					</div>
				</li>
				<!-- //상세 -->
			</ul>
			<!-- //TAB CONTENT -->
		</div>
		<!-- //TABS AREA -->
	</div>
	
<script th:inline="javascript">
/*<![CDATA[*/
	var roleList = [[${roleList}]];
	
	// specify the columns
	var columnDefs = [
		{headerName: "메뉴명", field: "menuNm", width: 200,
			cellRenderer: function(params) {
				return '<a href="javascript:void(0);">' + params.value + '</a>';
			}
		},
		{headerName: "메뉴구분", field: "menuGb", width: 100, cellClass: 'text-center',
			cellRenderer: function(params) { return params.value == 'M' ? '메뉴' : '프로그램'; }
			/* valueFormatter: function(params) { return params.value == 'M' ? '메뉴' : '프로그램'; } */
		},
		{headerName: "메뉴권한코드", field: "roleCds", hide: true},
		{headerName: "메뉴권한", field: "roleNms", width: 400, cellClass: 'text-center'},
		{headerName: "메뉴URL", field: "menuUrl", width: 300},
		{headerName: "메뉴설명", field: "menuDesc", hide: true},
		{headerName: "표시순서", field: "dispOrd", width: 100, cellClass: 'text-right'},
		{headerName: "사용여부", field: "useYn", width: 100, cellClass: 'text-center'}
	];
	
	// Get GridOptions
	var gridOptions = gagaAgGrid.getGridOptions(columnDefs);
	
	// Add on options
	gridOptions.suppressRowClickSelection = true;
	gridOptions.rowSelection = 'multiple';
	//gridOptions.groupSelectsChildren = true; // Tree Data mode에서는 작동 안 함
	gridOptions.treeData = true; // enable Tree Data mode
	gridOptions.groupDefaultExpanded = -1; // expand all groups by default
	gridOptions.getDataPath = function(data) { // just return the hierarchy, no conversion required
		return data.treePath.split("/");
	};
	gridOptions.autoGroupColumnDef = {
		headerName: "메뉴ID", field: "menuId", width: 200,
		cellRendererParams: {
			suppressCount: true, // 하위의 항목 건수 표시 안 함
			checkbox: true
		}
	};
	
	// Cell Click
	gridOptions.onCellClicked = function(event) {
		if (event.colDef.field != 'menuNm')
			return;

		fnBindDetail(event.data);
	}
	
	// 조회
	$('#btnSearch').on('click', function() {
		var actionUrl = $('#searchForm').prop('action') + '?' + $('#searchForm').serialize();
		
		// Fetch data
		gagaAgGrid.fetch(actionUrl, gridOptions);
	});
	
	// 목록 > row 클릭 시
	var fnBindDetail = function(rowData) {
		$('#detailForm input[name=menuId]').val(rowData.menuId);
		$('#detailForm input[name=menuNm]').val(rowData.menuNm);
		
		$("#detailForm input:radio[name=menuGb]").parents('td').find('label').removeClass('checked');
		if (rowData.menuGb == 'M') {
			$("#detailForm input:radio[name=menuGb]").eq(0).prop('checked', true);
			$("#detailForm input:radio[name=menuGb]").eq(0).parent().addClass('checked');
			$('#detailForm').find('.menu-role').hide();
		} else {
			$("#detailForm input:radio[name=menuGb]").eq(1).prop('checked', true);
			$("#detailForm input:radio[name=menuGb]").eq(1).parent().addClass('checked');
			$('#detailForm').find('.menu-role').show();
		}
		
		$('#detailForm select[name=pmenuId]').val(rowData.pmenuId);
		
		// 다중Select에 권한 표시
		cfnShowMultiSelectedValue($('#detailForm select[name=roleCd]'), rowData.roleCds);
		
		$('#detailForm textarea[name=menuDesc]').val(rowData.menuDesc);
		$('#detailForm input[name=menuUrl]').val(rowData.menuUrl);
		$('#detailForm input[name=dispOrd]').val(rowData.dispOrd);
		
		$("#detailForm input[name=useYn]").val(rowData.useYn);
		if (rowData.useYn == 'Y') {
			$("#detailForm input:checkbox[name=chkUseYn]").prop('checked', true);
			$("#detailForm input:checkbox[name=chkUseYn]").parent().addClass('checked');
		} else {
			$("#detailForm input:checkbox[name=chkUseYn]").prop('checked', false);
			$("#detailForm input:checkbox[name=chkUseYn]").parent().removeClass('checked');
		}
		
		$('.tabsNav li').eq(2).removeClass('off');
		$('.tabsNav li').eq(2).trigger('click');
		
		$('#detailForm input[name=menuNm]').focus();
	}
	
	// 저장 처리
	var fnSave = function(formId) {
		// 입력 값 체크
		if (!gagajf.validation(formId))
			return false;
		
		$(formId + ' input[name=useYn]').val($(formId + ' input:checkbox[name=chkUseYn]').is(":checked") ? 'Y' : 'N');
		$(formId + ' input[name=roleCds]').val($(formId + ' select[name=roleCd]').val());
		
		gagajf.ajaxFormSubmit($(formId).prop('action'), formId, fnSaveCallback);
	}
	
	// 등록/수정 처리 후 콜백함수
	var fnSaveCallback = function(result) {
		// 초기화
		$('#registerForm')[0].reset();
		
		// 상세의 다중Select 초기화
		var mSelected = '';
		var mSelecting = '';
		$.each(roleList, function(idx, item) {
			mSelected += '<li class="off" data-index="' + (idx + 1) + '">[' + item.cd + '] ' + item.cdNm + '<a>닫기</a></li>\n';
			mSelecting += '<li data-index="' + (idx + 1) + '">[' + item.cd + '] ' + item.cdNm + '</li>\n';
		});
		mSelected += '<li class="srchFld"><input type="text" autocomplete="off"/></li>\n';
		$('#detailForm').find('.mSelected').html(mSelected);
		$('#detailForm').find('.mSelecting > ul').html(mSelecting);
		
		// 목록 재조회
		$('.tabsNav li').eq(0).trigger('click');
		
		$('#btnSearch').trigger('click');
	}
	
	// 삭제 버튼 클릭 시
	$('.btnDelete').on('click', function() {
		var removedData = gagaAgGrid.removeRowData(gridOptions);
		
		if (removedData.length == 0) {
			alert('선택된 행이 없습니다.');
			return;
		}
		
		mcxDialog.confirm("정말 삭제하시겠습니까?", {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function() {
				// delete 대신 update 처리해야 하므로 다음과 같이 useYn 값을 변환
				var updatedData = [];
				
				$.each(removedData, function(idx, item) {
					item.useYn = 'N';
					updatedData.push(item);
				});
				
				var jsonData = JSON.stringify(updatedData);
				gagajf.ajaxJsonSubmit('/system/menu/list/save', jsonData, fnSaveCallback);
			}
		});
	});
	
	$(document).ready(function() {
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
	});
/*]]>*/
</script>

</html>
