<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : UserForm.html
 * @desc    : 사용자관리 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.10.07   gagamel     최초 작성
 *******************************************************************************
 -->
	<div id="main">
		<!-- 메인타이틀 영역 -->
		<div class="main-title">
		</div>
		<!-- //메인타이틀 영역 -->
		
		<!-- 메뉴 설명 -->
		<div class="infoBox menu-desc">
		</div>
		<!-- //메뉴 설명 -->
		
		<!-- 검색조건 영역 -->
		<div class="panelStyle">
			<form id="searchForm" name="searchForm" action="#" th:action="@{'/system/user/list'}" onsubmit="$('#btnSearch').trigger('click'); return false;">
				<table class="frmStyle" aria-describedby="검색조건">
					<colgroup>
						<col style="width:10%;"/>
						<col style="width:15%;"/>
						<col style="width:10%;"/>
						<col style="width:20%;"/>
						<col style="width:10%;"/>
						<col style="width:15%;"/>
						<col style="width:10%;"/>
						<col style="width:10%;"/>
					</colgroup>
					<tr>
						<th>업체</th>
						<td>
							<select name="supplyCompCd">
								<option th:if="${sessionInfo.supplyCompCd == null}" value="">[전체]</option>
								<option th:if="${supplyCompList}" th:each="oneData, status : ${supplyCompList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
						<th>권한</th>
						<td>
							<select name="roleCd">
								<option value="">[전체]</option>
								<option th:if="${roleList}" th:each="oneData, status : ${roleList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
						<th>구분</th>
						<td>
							<select name="searchGb">
								<option value="">[선택]</option>
								<option value="userId">사용자ID</option>
								<option value="userNm">사용자명</option>
							</select>
							<input type="text" class="w100" name="searchTxt" placeholder="" maxlength="20"/>
						</td>
						<th>사용여부</th>
						<td>
							<select name="useYn">
								<option value="">[전체]</option>
								<option value="Y" selected="selected">[Y] Yes</option>
								<option value="N">[N] No</option>
							</select>
						</td>
					</tr>
				</table>
				
				<ul class="panelBar">
					<li class="center">
						<button type="button" class="btn btn-base btn-lg" id="btnSearch">조회</button>
						<button type="button" class="btn btn-gray btn-lg" onclick="$('#searchForm')[0].reset();">초기화</button>
					</li>
				</ul>
			</form>
		</div>
		<!-- //검색조건 영역 -->
		
		<!-- 리스트 영역 -->
		<div class="panelStyle">
			<!-- 버튼 배치 영역 -->
			<ul class="panelBar">
				<li class="right">
					<button type="button" class="btn btn-info btn-lg" onclick="fnOpenUserDetailPopup('N');">등록</button>
					<button type="button" class="btn btn-danger btn-lg" id="btnDelete">삭제</button>
				</li>
			</ul>
			<!-- //버튼 배치 영역 -->
			
			<div id="gridList" style="width: 100%; height: 570px" class="ag-theme-balham"></div>
		</div>
		<!-- //리스트 영역 -->
	</div>

<script th:inline="javascript">
/*<![CDATA[*/
	var roleList = gagajf.convertToArray([[${roleList}]]);
	
	// specify the columns
	var columnDefs = [
		{width: 40, minWidth: 40, cellClass: 'text-center', headerCheckboxSelection: true, checkboxSelection: true, filter: false},
		{headerName: 'No', width: 60, cellClass: 'text-center', valueGetter: function(params) { return params.node.rowIndex + 1 }},
		{headerName: "사용자번호", field: "userNo", width: 100, cellClass: 'text-center', hide: true},
		{
			headerName: "메뉴", field: "btnMenu", width: 100, cellClass: 'text-center',
			cellRenderer: function(params) {
				return '<button type="button" class="btn btn-success btn-sm" onclick="fnOpenUserMenuPopup(\'' + params.data.userNo + '\');">메뉴관리</button>';
			}
		},
		{
			headerName: "사용자ID", field: "userId", width: 100, cellClass: 'text-center',
			cellRenderer: function(params) {
				return '<a href="javascript:void(0);" onclick="fnOpenUserDetailPopup(\'U\', \'' + params.data.userNo + '\');">' + params.value + '</a>';
			}
		},
		{headerName: "사용자명", field: "userNm", width: 150, cellClass: 'text-center'},
		{headerName: "권한코드", field: "roleCd", width: 100, hide: true},
		{
			headerName: "권한", field: "roleNm", width: 120, cellClass: 'text-center',
			cellRenderer: function (params) { return gagaAgGrid.lookupValue(roleList, params.data.roleCd); }
		},
		{headerName: "권한관련값", field: "roleRefVal", width: 150, cellClass: 'text-center', hide: true},
		{headerName: "업체", field: "supplyCompNm", width: 150, cellClass: 'text-center'},
		{headerName: "외부몰벤더", field: "vendorNm", width: 150, cellClass: 'text-center'},
		{
			headerName: "제휴채널", field: "afLinkNm", width: 120, cellClass: 'text-center',
			cellRenderer: function(params) {
				return params.data.roleCd.indexOf('G001_D') > -1 ? params.data.afChannelNm + '-' + params.value : '';
			}
		},
		{headerName: "이메일", field: "email", width: 200, cellClass: 'text-center'},
		{headerName: "휴대전화번호", field: "cellPhnno", width: 150, cellClass: 'text-center'},
		{headerName: "IP주소", field: "ipAddr", width: 100, cellClass: 'text-center'},
		{headerName: "IP체크여부", field: "ipChkYn", width: 100, cellClass: 'text-center'},
		{headerName: "마스킹여부", field: "maskingYn", width: 100, cellClass: 'text-center'},
		{headerName: "사용여부", field: "useYn", width: 100, cellClass: 'text-center'},
		{
			headerName: '최근로그인일시', field: 'loginLdt', width: 150, cellClass: 'text-center',
			cellRenderer: function(params) {
				return !gagajf.isNull(params.value) ? params.value.toDate("YYYYMMDDHHmmss").format("YYYY-MM-DD HH:mm:ss") : '';
			}
		}
	];
	
	// Get GridOptions
	var gridOptions = gagaAgGrid.getGridOptions(columnDefs);
	
	gridOptions.rowSelection = 'multiple';
	
	// 조회
	$('#btnSearch').on('click', function() {
		// Fetch data
		gagaAgGrid.fetch($('#searchForm').prop('action'), gridOptions, '#searchForm');
	});
	
	// 삭제
	$('#btnDelete').on('click', function() {
		var removedData = gagaAgGrid.removeRowData(gridOptions);
		
		if (removedData.length == 0) {
			mcxDialog.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/user/list/delete'
						, jsonData
						, function() {
							$('#btnSearch').trigger('click');
						});
			}
		});
	});
	
	// 사용자 상세 팝업
	var fnOpenUserDetailPopup = function(mode, userNo) {
		var actionUrl = "/system/user/info/form?mode=" + mode;
		if (typeof(userNo) != 'undefined') actionUrl += "&userNo=" + userNo;
		cfnOpenModalPopup(actionUrl, "popupUser");
	}
	
	// 사용자 메뉴 팝업
	var fnOpenUserMenuPopup = function(userNo) {
		var actionUrl = "/system/user/menu/" + userNo;
		cfnOpenModalPopup(actionUrl, "popupUserMenu");
	}
	
	$(document).ready(function() {
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
	});
/*]]>*/
</script>

</html>
