<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : GiftcardSettleForm.html
 * @desc    : 상품권정산 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.10.26   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="@{'/settle/giftcard/list'}" onsubmit="$('#btnSearch').trigger('click'); return false;">
				<table class="frmStyle" aria-describedby="검색조건">
					<colgroup>
						<col style="width:10%;"/>
						<col/>
						<col style="width:10%;"/>
						<col style="width:10%;"/>
					</colgroup>
					<tr>
						<th>구분<i class="required" title="필수" aria-hidden="true"></i></th>
						<td>
							<select name="termGb">
								<option value="SETTLE_YM">정산월</option>
								<option value="REG_DT">등록일</option>
								<option value="EXPIRE_DT">사용만료일</option>
							</select>
							<span id="spanSettle">
								<input type="text" class="schMonth w60" name="settleYm" id="settleYm" maxlength="7"/>
								<button type="button" class="btn icn schBtn" data-id="useYm"><i class="fa fa-calendar" aria-hidden="true"></i></button>
							</span>
							<span id="terms" style="display: none;">
								<span class="nowrap">
									<input name="startDt" id="startDt" type="text" class="w80 schDate" maxlength="8"/>
									~
									<input name="endDt" id="endDt" type="text" class="w80 schDate" maxlength="8"/>
								</span>
								<button type="button" class="btn btn-default btn-sm btnToday" onclick="gagajf.setDate('#terms', 'startDt', 'endDt', 't');">오늘</button>
								<button type="button" class="btn btn-default btn-sm btnYesterday" onclick="gagajf.setDate('#terms', 'startDt', 'endDt', 'y');">어제</button>
								<button type="button" class="btn btn-default btn-sm" onclick="gagajf.setDate('#terms', 'startDt', 'endDt', '7d');">최근한주</button>
								<button type="button" class="btn btn-default btn-sm" onclick="gagajf.setDate('#terms', 'startDt', 'endDt', 'tw');">이번주</button>
								<button type="button" class="btn btn-default btn-sm" onclick="gagajf.setDate('#terms', 'startDt', 'endDt', 'pw');">지난주</button>
								<button type="button" class="btn btn-default btn-sm" onclick="gagajf.setDate('#terms', 'startDt', 'endDt', '1m');">최근한달</button>
								<button type="button" class="btn btn-default btn-sm" onclick="gagajf.setDate('#terms', 'startDt', 'endDt', 'tm');">이번달</button>
								<button type="button" class="btn btn-default btn-sm btnYesterMonth" onclick="gagajf.setDate('#terms', 'startDt', 'endDt', 'pm');">지난달</button>
								<button type="button" class="btn btn-default btn-sm" onclick="gagajf.setDate('#terms', 'startDt', 'endDt', '3m');">최근3개월</button>
							</span>
						</td>
						<th>유효상품권</th>
						<td>
							<select name="availYn">
								<option value="">[전체]</option>
								<option value="Y">Yes</option>
								<option value="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" id="btnInit">초기화</button>
					</li>
				</ul>
			</form>
		</div>
		<!-- 검색조건 영역 -->

		<!-- 리스트 영역 -->
		<div class="panelStyle">
			<!-- 버튼 배치 영역 -->
			<ul class="panelBar">
				<li class="right">
					<button type="button" class="btn btn-default btn-lg" id="btnExcel">엑셀다운로드</button>
				</li>
			</ul>
			<!-- //버튼 배치 영역 -->
			
			<div id="gridList" style="width: 100%; height: 470px" class="ag-theme-balham"></div>
		</div>
		<!-- //리스트 영역 -->
	</div>

<script th:inline="javascript">
/*<![CDATA[*/
	let columnDefs = [
		{ headerName: "상품권번호", field: "gfcdNo", width: 200, cellClass: 'text-center' },
		{ headerName: "등록일", field: "regDt", width: 120, cellClass: 'text-center' },
		{ headerName: "사용만료일", field: "useExpDate", width: 120, cellClass: 'text-center' },
		{ headerName: "유효여부", field: "availYn", width: 100, cellClass: 'text-center' },
		{ headerName: "회원ID", field: "custId", width: 100, cellClass: 'text-center' },
		{ 
			headerName: "최초등록금액", field: "chgGfcdAmt", width: 150, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: "누적사용금액", field: "usGfcdAmt", width: 150, cellClass: 'text-right', hide: true,
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: "정산월 사용금액", field: "useGfcdAmt", width: 150, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: "정산월 취소금액", field: "cnclGfcdAmt", width: 150, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: "잔액", field: "rmGfcdAmt", width: 150, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		}
	];

	let gridOptions = gagaAgGrid.getGridOptions(columnDefs);

	let gb = 'SETTLE_YM';
	
	// 검색
	$('#btnSearch').on('click', function() {
		if (gb == 'SETTLE_YM') { // 정산월
			if (gagajf.isNull($('#searchForm input[name=settleYm]').val())) {
				mcxDialog.alertC('정산월을 입력해 주세요.', {
					sureBtnText: "확인",
					sureBtnClick: function() {
						$('#searchForm input[name=settleYm]').focus();
					}
				});
				return false;
			}
		} else {
			if (gagajf.isNull($('#searchForm input[name=startDt]').val())) {
				mcxDialog.alertC('시작일자를 입력해 주세요.', {
					sureBtnText: "확인",
					sureBtnClick: function() {
						$('#searchForm input[name=startDt]').focus();
					}
				});
				return false;
			}
			
			if (gagajf.isNull($('#searchForm input[name=endDt]').val())) {
				mcxDialog.alertC('종료일자를 입력해 주세요.', {
					sureBtnText: "확인",
					sureBtnClick: function() {
						$('#searchForm input[name=endDt]').focus();
					}
				});
				return false;
			}
			
			var fromDate = $('#searchForm input[name=startDt]').val().toDate('YYYY-MM-DD');
			var toDate = $('#searchForm input[name=endDt]').val().toDate('YYYY-MM-DD');
			
			if (fromDate > toDate) {
				mcxDialog.alert("시작일자는 종료일자 보다 클 수 없습니다.");
				$('#searchForm input[name=endDt]').focus();
				return false;
			}
		}
		
		gagaAgGrid.fetch($('#searchForm').prop('action'), gridOptions, '#searchForm', function() {
			fnShowOrHideColumn();
		});
	});
	
	// 초기화 클릭시
	$('#btnInit').on('click', function() {
		$('#searchForm')[0].reset();
		$('#settleYm').val((new Date()).before(0, 1, 0).format("YYYY-MM"));
		$('.btnYesterMonth').trigger('click');
		$('#searchForm select[name=termGb]').trigger('change');
	});
	
	// 엑셀다운로드
	$('#btnExcel').on('click', function() {
		gagaAgGrid.exportToExcel('상품권 목록', gridOptions);
	});
	
	// 구분 선택 시
	$('#searchForm select[name=termGb]').on('change', function() {
		gb = $('#searchForm select[name=termGb]').val();
		
		if (gb == 'SETTLE_YM') { // 정산월
			$('#spanSettle').show();
			$('#terms').hide();
		} else {
			$('#spanSettle').hide();
			$('#terms').show();
		}
		
		fnShowOrHideColumn();
		gridOptions.api.setRowData();
	});
	
	var fnShowOrHideColumn = function() {
		if (gb == 'SETTLE_YM') { // 정산월
			gagaAgGrid.showOrHideColumn(gridOptions, 'usGfcdAmt', false);
			gagaAgGrid.showOrHideColumn(gridOptions, 'useGfcdAmt', true);
			gagaAgGrid.showOrHideColumn(gridOptions, 'cnclGfcdAmt', true);
		} else {
			gagaAgGrid.showOrHideColumn(gridOptions, 'usGfcdAmt', true);
			gagaAgGrid.showOrHideColumn(gridOptions, 'useGfcdAmt', false);
			gagaAgGrid.showOrHideColumn(gridOptions, 'cnclGfcdAmt', false);
		}
	}
	
	$(document).ready(function() {
		$('#settleYm').val((new Date()).before(0, 1, 0).format("YYYY-MM"));
		$('.btnYesterMonth').trigger('click');
		
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
	});
/*]]>*/
</script>

</html>
