<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : MailHeaderFooterForm.html
 * @desc    : 메일헤더푸터관리 화면
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2021.02.01   csh9191     최초 작성
 * 1.1  2021.05.13   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="@{'/marketing/mail/headerfooter/list'}" onsubmit="$('#btnSearch').trigger('click'); return false;">
				<table class="frmStyle" aria-describedby="검색조건">
					<colgroup>
						<col style="width:10%;"/>
						<col style="width:20%;"/>
						<col style="width:10%;"/>
						<col/>
					</colgroup>
					<tr>
						<th>구분</th>
						<td>
							<select name="mailhfGb">
								<option value="">[전체]</option>
								<option value="H">헤더</option>
								<option value="F">푸터</option>
							</select>
						</td>
						<th>헤더푸터명</th>
						<td>
							<input type="text" name="mailhfNm" class="w200" maxlength="50"/>
						</td>
					</tr>
				</table>
				
				<ul class="panelBar">
					<li class="center">
						<button type="button" class="btn btn-gray btn-lg" onclick="$('#searchForm')[0].reset();">초기화</button>
						<button type="button" class="btn btn-info btn-lg" id="btnSearch">조회</button>
					</li>
				</ul>
			</form>
		</div>
		<!-- 검색조건 영역 -->
			
		<!-- 리스트 영역 -->
		<div class="panelStyle">
			<!-- 버튼 배치 영역 -->
			<ul class="panelBar">
				<li class="left">
					<button type="button" class="btn btn-danger btn-lg" id="btnDeleteRow">선택삭제</button>	
				</li>
				<li class="right">
					<button type="button" class="btn btn-info btn-lg" onclick="fnOpenMailHeaderFooterPopup('N');">메일헤더푸터 등록</button>
				</li>
			</ul>
			<!-- //버튼 배치 영역 -->
			
			<div id="gridList" style="width: 100%; height: 570px" class="ag-theme-balham"></div>
		</div>
		<!-- //리스트 영역 -->
	</div>

<script th:inline="javascript">
/*<![CDATA[*/
	//헤더 푸터 구분 표시
	let mailhfGb = { "H":"헤더", "F":"푸터" };

	// specify the columns
	let columnDefs = [
		{width: 40, minWidth: 40, cellClass: 'text-center', headerCheckboxSelection: true, checkboxSelection: true, filter: false},
		{headerName: "헤더푸터번호", field: "mailhfSq", width: 150, cellClass: 'text-center'},
		{
		 	headerName: "헤더푸터구분", field: "mailhfGb", width: 150, cellClass: 'text-center',
		 	valueFormatter: function (params) { return gagaAgGrid.lookupValue(mailhfGb, params.value); }
		},
		{
			headerName: "헤더푸터명", field: "mailhfNm", width: 350,
			cellRenderer: function(params) { return '<a href="javascript:void(0);">' + params.value + '</a>'; }
		},
		{headerName: "사용여부", field: "useYn", width: 100, cellClass: 'text-center'},
		{headerName: "등록자", field: "regNm", width: 100, cellClass: 'text-center'},
		{
			headerName: "등록일시", field: "regDt", width: 150, cellClass: 'text-center',
			cellRenderer: function(params) { return gagaAgGrid.toDateTimeFormat(params.value); }
		},
		{headerName: "수정자", field: "updNm", width: 100, cellClass: 'text-center'},
		{
			headerName: "수정일시", field: "updDt", width: 150, cellClass: 'text-center', 
			cellRenderer: function(params) { return gagaAgGrid.toDateTimeFormat(params.value); }
		}
	];

	// Get GridOptions
	let gridOptions = gagaAgGrid.getGridOptions(columnDefs); 

	// 다중 선택
	gridOptions.rowSelection = 'multiple';
	
	// 셀 클릭 이벤트
	gridOptions.onCellClicked = function(event) {
		if (event.colDef.field != 'mailhfNm')
			return;
		
		fnOpenMailHeaderFooterPopup('U', event.data.mailhfSq);
	}
	
	// 조회
	$('#btnSearch').on('click', function() {
		gagaAgGrid.fetch($('#searchForm').prop('action'), gridOptions, '#searchForm');
	});
	
	// 선택삭제
	$('#btnDeleteRow').on('click', function() {
		var removedData = gagaAgGrid.removeRowData(gridOptions);
		
		if (removedData.length == 0) {
			mcxDialog.alert('선택된 행이 없습니다.');
			return;
		}
		
		mcxDialog.confirm("삭제하시겠습니까?", {
			sureBtnText: "확인",
			cancelBtnText: "취소",
			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('/marketing/mail/headerfooter/list/delete', jsonData, function() {
					$('#btnSearch').trigger('click');
				});
			}
		});
	});
	
	// 등록/상세 팝업
	var fnOpenMailHeaderFooterPopup = function(mode, mailhfSq) {
		var actionUrl = '/marketing/mail/headerfooter/detail/form?mode=' + mode;
		if (!gagajf.isNull(mailhfSq)) actionUrl += '&mailhfSq=' + mailhfSq;
		cfnOpenModalPopup(actionUrl, 'popupMailHeaderFooter');
	}
	
	$(document).ready(function() {
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
	});
/*]]>*/
</script>

</html>
