<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : MailTemplateForm.html
 * @desc    : 메일템플릿관리
 *============================================================================
 * SISUN
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2021.02.05   csh9191     최초 작성
 * 1.1  2021.05.11   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/mailtemplate/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:10%;"/>
						<col style="width:10%;"/>
						<col/>
						<col style="width:10%;"/>
						<col style="width:10%;"/>
					</colgroup>
					<tr>
						<th>메일템플릿구분</th>
						<td>
							<select name="mailtGb">
								<option value="">[전체]</option>
								<option value="A">[A] 자동메일</option>
								<option value="M">[M] 마케팅메일</option>
							</select>
						</td>
						<th>메일템플릿번호</th>
						<td>
							<input type="text" name="mailtSq" placeholder="" maxlength="10" data-valid-type="numeric"/>
						</td>
						<th>메일템플릿명</th>
						<td>
							<input type="text" name="mailtNm" placeholder="" maxlength="50"/>
						</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="fnOpenMailTemplateDetailPopup('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[*/
	//메일구분
	let mailtGb = {"M" :"마케팅메일", "A":"자동메일"};
	
	var columnDefs = [
		{width: 40, minWidth: 40, cellClass: 'text-center', headerCheckboxSelection: true, checkboxSelection: true, filter: false},
		{headerName: "메일템플릿번호", field: "mailtSq", width: 150, cellClass: 'text-center'},
		{
			headerName: "메일템플릿구분", field: "mailtGb", width: 150, cellClass: 'text-center',
			valueFormatter: function (params) { return gagaAgGrid.lookupValue(mailtGb, params.value); }
		},
		{
			headerName: "메일템플릿명", field: "mailtNm", width: 350,
			cellRenderer: function(params) { return '<a href="javascript:void(0);">' + params.value + '</a>'; }
		},
		{
			headerName: "미리보기", field:"preview", width: 100, cellClass: 'text-center',
			cellRenderer: function(params) { return '<button="javascript:void(0);" class="btn btn-info btn-sm">미리보기</button>'; }
		},
		{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
	var gridOptions = gagaAgGrid.getGridOptions(columnDefs);
	
	// 다중 선택
	gridOptions.rowSelection = 'multiple';
	
	// 셀 클릭 시
	gridOptions.onCellClicked = function(event) {
		if (event.colDef.field == 'mailtNm'){
			// 메일템플릿 상세 팝업
			fnOpenMailTemplateDetailPopup('U', event.data.mailtSq);
		} else if (event.colDef.field == 'preview') {
			// 메일템플릿 미리보기 팝업
			fnOpenMailPreviewPopup(event.data.mailtSq);
		}
	}
	
	// 조회
	$('#btnSearch').on('click', function() {
		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("삭제하시겠습니까?", {
			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/mailtemplate/list/delete', jsonData, function() {
					$('#btnSearch').trigger('click');
				});
			}
		});
	});
	
	// 등록/상세 팝업
	var fnOpenMailTemplateDetailPopup = function(mode, mailtSq) {
		var actionUrl = '/marketing/mailtemplate/detail/form' + '?mode=' + mode;
		if (!gagajf.isNull(mailtSq)) actionUrl += '&mailtSq=' + mailtSq;
		cfnOpenModalPopup(actionUrl, 'popupMailTemplateDetail');
	}
	
	// 미리보기
	var fnOpenMailPreviewPopup = function(mailtSq) {
		var actionUrl = '/marketing/mailtemplate/preview/form/' + mailtSq;
		cfOpenWindowPopup(actionUrl, 'popupMailTemplatePreview', 900, 900);
	}
	
	$(document).ready(function() {
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
	});
/*]]>*/

</script>
</html>