<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : AfLinkSettleForm.html
 * @desc    : 제휴채널정산 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2021.01.20   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/aflink/fee/list'}" onsubmit="$('#btnSearch').trigger('click'); return false;">
				<table class="frmStyle" aria-describedby="검색조건">
					<colgroup>
						<col style="width:10%;"/>
						<col style="width:60%;"/>
						<col style="width:10%;"/>
						<col/>
					</colgroup>
					<tr>
						<th>발생(정산)기간<i class="required" title="필수" aria-hidden="true"></i></th>
						<td id="terms">
						</td>
						<th>제휴채널</th>
						<td>
							<select name="afChannel">
								<option value="">[전체]</option>
								<option th:if="${afChannelList}" th:each="oneData, status : ${afChannelList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
							<select name="afLinkCd">
								<option value="">[전체]</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="left">
					<span class="infoTxt cBlue">
						<i class="fa fa-info-circle" aria-hidden="true"></i><strong>발생(정산)일시</strong> : 결제/취소/반품이 실제 발생한 일시. <i class="fa fa-info-circle marL10" aria-hidden="true"></i><strong>판매금액</strong> : 정상 판매가로 배송비를 제외한 상품금액 합계<br/>
						<i class="fa fa-info-circle" aria-hidden="true"></i><strong>쿠폰할인금액</strong> : 즉시할인 + 다다익선 + 상품쿠폰 + 장바구니쿠폰 + PG결제쿠폰 등의 합계. <i class="fa fa-info-circle marL10" aria-hidden="true"></i><strong>포인트사용금액</strong> : 포인트사용 + 선포인트사용 합계<br/>
						<i class="fa fa-info-circle" aria-hidden="true"></i><strong>결제액</strong> : 판매금액 - 쿠폰할인금액 - 포인트사용금액 - 상품권사용금액. <i class="fa fa-info-circle marL10" aria-hidden="true"></i><strong>매출액</strong> : 결제액 / 1.1
					</span>
				</li>
				<li class="right">
					<button type="button" class="btn btn-default btn-lg" id="btnExcel">엑셀다운로드</button>
				</li>
			</ul>
			<!-- //버튼 배치 영역 -->
			
			<div id="gridList" style="width: 100%; height: 670px" class="ag-theme-balham"></div>
		</div>
		<!-- //리스트 영역 -->
	</div>

<script th:inline="javascript">
/*<![CDATA[*/
	let columnDefs = [
		{headerName: "상위제휴채널", field: "afChannelNm", width: 100, rowGroup: true, hide: true},
		{headerName: "제휴채널", field: "afLinkNm", width: 100},
		{headerName: "발생(정산)일시", field: "occurDt", width: 150, cellClass: 'text-center'},
		{headerName: "주문번호", field: "ordNo", width: 80, cellClass: 'text-center'},
		{headerName: "주문상태", field: "ordDtlStat", width: 100, cellClass: 'text-center'},
		{
			headerName: '판매금액', field: 'sellAmt', width: 120, cellClass: 'text-right',
			aggFunc: 'sum', enableValue: true, cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: '쿠폰할인금액', field: 'cpnDcAmt', width: 120, cellClass: 'text-right',
			aggFunc: 'sum', enableValue: true, cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: '포인트사용금액', field: 'pntDcAmt', width: 120, cellClass: 'text-right',
			aggFunc: 'sum', enableValue: true, cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: '상품권사용금액', field: 'gfcdUseAmt', width: 120, cellClass: 'text-right',
			aggFunc: 'sum', enableValue: true, cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: '결제액(VAT포함)', field: 'payAmt', width: 120, cellClass: 'text-right',
			aggFunc: 'sum', enableValue: true, cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: '매출액(VAT제외)', field: 'salesAmt', width: 120, cellClass: 'text-right',
			aggFunc: 'sum', enableValue: true, cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		}
	];

	let gridOptions = gagaAgGrid.getGridOptions(columnDefs);

	gridOptions.autoGroupColumnDef = {
			headerName: "상위제휴채널",
			field: "grouping",
			width: 200,
			cellRendererParams: {
				suppressCount: true
			}
		};
	
	// for expand everything
	gridOptions.groupDefaultExpanded = -1;
	
	// 제휴채널 콤보박스 변경시
	$('#searchForm select[name=afChannel]').on('change', function() {
		if (gagajf.isNull($(this).val())) {
			$("#searchForm select[name=afLinkCd] option:gt(0)").remove();
		} else {
			var actionUrl = '/renderer/aflink/list/' + $(this).val();
			cfnCreateCombo(actionUrl, $('#searchForm select[name=afLinkCd]'), "[전체]");
		}
	});
	
	// 검색
	$('#btnSearch').on('click', function() {
		// 입력 값 체크
		if (!gagajf.validation($('#searchForm')))
			return false;
		
		gagaAgGrid.fetch($('#searchForm').prop('action'), gridOptions, '#searchForm', fnCreateTotal);
	});
	
	// 합계 생성
	let fnCreateTotal = function() {
		let sellAmt = 0;
		let cpnDcAmt = 0;
		let pntDcAmt = 0;
		let gfcdUseAmt = 0;
		let payAmt = 0;
		let salesAmt = 0;

		gridOptions.api.forEachNode(function(rowNode, index) {
			if (!rowNode.group) {
				if (typeof rowNode.data.sellAmt == 'number') { sellAmt += rowNode.data.sellAmt; }
				if (typeof rowNode.data.cpnDcAmt == 'number') { cpnDcAmt += rowNode.data.cpnDcAmt; }
				if (typeof rowNode.data.pntDcAmt == 'number') { pntDcAmt += rowNode.data.pntDcAmt; }
				if (typeof rowNode.data.gfcdUseAmt == 'number') { gfcdUseAmt += rowNode.data.gfcdUseAmt; }
				if (typeof rowNode.data.payAmt == 'number') { payAmt += rowNode.data.payAmt; }
				if (typeof rowNode.data.salesAmt == 'number') { salesAmt += rowNode.data.salesAmt; }
			}
		});

		let data = {
			grouping: '합계', afChannelNm: null, afLinkNm: null,
			occurDt: null, ordNo: null, ordDtlStat: null,
			sellAmt: sellAmt, cpnDcAmt: cpnDcAmt,
			pntDcAmt: pntDcAmt, gfcdUseAmt: gfcdUseAmt,
			payAmt: payAmt, salesAmt: salesAmt
		};

		gagaAgGrid.setPinnedRowData(gridOptions, data, 'top');
	}
	
	// 엑셀다운로드
	$('#btnExcel').on('click', function() {
		gagaAgGrid.exportToExcel('제휴채널정산 목록', gridOptions);
	});
	
	$(document).ready(function() {
		cfnCreateCalendar('#terms', 'startDt', 'endDt', true, '발생(정산)');
		$('.btnToday').trigger('click');
		
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
	});
/*]]>*/
</script>

</html>
