<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : GoodsSettleForm.html
 * @desc    : 상품정산 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.10.22   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/goods/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:15%;"/>
						<col style="width:10%;"/>
						<col style="width:15%;"/>
					</colgroup>
					<tr>
						<th>매출기간<i class="required" title="필수" aria-hidden="true"></i></th>
						<td colspan="3" id="terms">
						</td>
						<th>유통구분</th>
						<td>
							<select name="distributionGb">
								<option value="">[전체]</option>
								<option th:if="${distributionGbList}" th:each="oneData, status : ${distributionGbList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
					</tr>
					<tr>
						<th>몰구분</th>
						<td>
							<select name="mallGb">
								<option value="">[전체]</option>
								<option th:if="${mallGbList}" th:each="oneData, status : ${mallGbList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
							<span id="extmallVendor">
								<select name="vendorId">
									<option value="">[전체]</option>
									<option th:if="${vendorList}" th:each="oneData, status : ${vendorList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
								</select>
								<select name="extmallId">
									<option value="">[전체]</option>
								</select>
							</span>
						</td>
						<th>공급업체</th>
						<td>
<!-- 							<select name="supplyCompCd"> -->
<!-- 								<option value="">[전체]</option> -->
<!-- 								<option th:if="${supplyCompList}" th:each="oneData, status : ${supplyCompList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option> -->
<!-- 							</select> -->
							<input type="text" class="w100" name="supplyCompSearchTxt" id="supplyCompSearchTxt" maxlength="20"/>
							<button type="button" class="btn icn" onclick="cfnOpenCompanyListPopup('fnSetSupplyCompInfo', 'M');"><i class="fa fa-search"></i></button>
							<span id="supplyCompTxt"></span>
							<input type="hidden" name="supplyCompList"/>
						</td>
						<th>브랜드</th>
						<td>
<!-- 							<select name="brandCd"> -->
<!-- 								<option value="">[전체]</option> -->
<!-- 							</select> -->
							<input type="text" class="w100" name="brandSearchTxt" id="brandSearchTxt" maxlength="20" />
							<button type="button" class="btn icn" onclick="cfnOpenBrandListPopup('fnSetBrandInfo', 'M');"><i class="fa fa-search"></i></button>
							<!-- <input type="text" class="w100" name="brandCd" readonly="readonly"/> -->
							<span id="brandTxt"></span>
							<input type="hidden" name="brandList"/>
						</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="left">
					<span class="infoTxt cBlue">
						<i class="fa fa-info-circle" aria-hidden="true"></i><strong>1.상품총액</strong>: 실판매가 * 판매수량<br/>
						<i class="fa fa-info-circle" aria-hidden="true"></i><strong>2.수수료</strong>: 상품총액(쿠폰할인금액과 다다익선할인금액만 차감. 포인트와 상품권은 포함) * 판매수수료율(%)<br/>
						<i class="fa fa-info-circle" aria-hidden="true"></i><strong>3.정산대상액</strong>: 상품총액 - 수수료
					</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: 470px" class="ag-theme-balham"></div>
		</div>
		<!-- //리스트 영역 -->
	</div>

<script th:inline="javascript">
/*<![CDATA[*/
	let supplyCompList = gagajf.convertToArray([[${supplyCompList}]]);
	let distributionGbList = gagajf.convertToArray([[${distributionGbList}]]);

	let columnDefs = [
		{ headerName: "주문번호", field: "ordNo", width: 100, cellClass: 'text-center' },
		{ headerName: "주문상세번호", field: "ordDtlNo", width: 150, cellClass: 'text-center' },
		{ headerName: "몰구분", field: "mallGbNm", width: 100, cellClass: 'text-center' },
		{ headerName: "제휴몰주문번호", field: "extmallOrderId", width: 150, cellClass: 'text-center' },
		{ headerName: "제휴몰명", field: "extmallNm", width: 150, cellClass: 'text-center' },
// 		{
// 			headerName: "공급업체", field: "supplyCompCd", width: 100, cellClass: 'text-center',
// 			valueGetter: function (params) {
// 				return gagaAgGrid.lookupValue(supplyCompList, params.data.supplyCompCd);
// 			}
// 		},
		{ headerName: "공급업체", field: "supplyCompNm", width: 150, cellClass: 'text-center' },
// 		{
// 			headerName: "유통구분", field: "distributionGb", width: 100, cellClass: 'text-center',
// 			valueGetter: function (params) {
// 				return gagaAgGrid.lookupValue(distributionGbList, params.data.distributionGb);
// 			}
// 		},
		{ headerName: "유통구분", field: "distributionGbNm", width: 100, cellClass: 'text-center'},
		{ headerName: "정산주기", field: "settleDayNm", width: 100, cellClass: 'text-center' },
		{ headerName: "주문일시", field: "ordDt", width: 150, cellClass: 'text-center' },
		{ headerName: "정산구분", field: "settleGbNm", width: 100, cellClass: 'text-center' },
		{ headerName: "발생(출고/회수)일시", field: "occurDt", width: 150, cellClass: 'text-center' },
		{ headerName: "상품코드", field: "goodsCd", width: 120, cellClass: 'text-center' },
		{ headerName: "상품명", field: "goodsNm", width: 300 },
		{ headerName: "브랜드", field: "brandEnm", width: 100, cellClass: 'text-center' },
		{ headerName: "담당MD", field: "mdNm", width: 100, cellClass: 'text-center' },
		{ headerName: "단품코드", field: "itemCd", width: 120, cellClass: 'text-center' },
		{ headerName: "옵션", field: "optCd1", width: 100, cellClass: 'text-center' },
		{
			headerName: "판매가", field: "sellPrice", width: 100, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: "판매수량", field: "sellQty", width: 100, cellClass: 'text-center',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "판매총액", field: "sellAmt", width: 100, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "즉시사용쿠폰금액", field: "cpn1DcAmt", width: 120, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: "상품쿠폰사용금액", field: "goodsCpnDcAmt", width: 120, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: "주문서쿠폰사용금액", field: "cartCpnDcAmt", width: 130, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: "포인트사용금액", field: "pntDcAmt", width: 120, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: "다다익선할인금액", field: "tmtbDcAmt", width: 120, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: "상품권사용금액", field: "gfcdUseAmt", width: 120, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},	
		{
			headerName: "자사쿠폰분담액", field: "selfCpnDcAmt", width: 120, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: "입점쿠폰분담액", field: "supplyCompCpnDcAmt", width: 120, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: "실판매가", field: "realSellPrice", width: 100, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); }
		},
		{
			headerName: "상품총액", field: "realSellAmt", width: 100, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "판매수수료율(%)", field: "sellFeeRate", width: 120, cellClass: 'text-center',
			cellRenderer: function (params) { return params.value + '%'; }
		},
		{
			headerName: "수수료", field: "sellFeeAmt", width: 100, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		},
		{
			headerName: "정산대상액", field: "settleAmt", width: 100, cellClass: 'text-right',
			cellRenderer: function (params) { return gagaAgGrid.toAddComma(params.value); },
			cellStyle : function(params) { if (Number(params.value) < 0) return { 'color' : 'red' } }
		}
	];

	let gridOptions = gagaAgGrid.getGridOptions(columnDefs);

	// 몰구분 콤보박스 변경 시
	$('#searchForm select[name=mallGb]').on('change', function() {
		if ($(this).val() == 'G011_10') { // 자사몰
			$('#extmallVendor').hide();
			$('#searchForm select[name=vendorId]').val('');
		} else if ($(this).val() == 'G011_20') { // 외부몰
			$('#extmallVendor').show();
		} else {
			$('#extmallVendor').hide();
			$('#searchForm select[name=vendorId]').val('');
		}
	});
	
	// 벤더 콤보박스 변경 시
	$('#searchForm select[name=vendorId]').on('change', function() {
		$.getJSON('/renderer/vendor/extmall/list/' + $(this).val()
				, function(result, status) {
					if (status == 'success') {
						$('#searchForm select[name=extmallId]').empty().append('<option value="">[전체]</option>');
						$.each(result, function(idx, data) {
							var optionHtml = '<option value="' + data.cd + '">[' + data.cd + '] '+ data.cdNm + '</option>\n';
							$('#searchForm select[name=extmallId]').append(optionHtml);
						});
					}
				});
	});
	
	// 업체 조회 팝업에서 호출
	var fnSetSupplyCompInfo = function(result) {
		var arrSupplyComp = [];
		var supplyCompTxt = "";
		var sIndex = 0;
		$('#supplyCompTxt').html('');
		$('#searchForm input[name=supplyCompSearchTxt]').val('');
		
		result.forEach(function(supplyComp) {
			sIndex++; 
			arrSupplyComp.push(supplyComp.supplyCompCd);
		});

		// 조회 값이 하나일 경우 화면에 코드 노출. 그 외는 갯수 처리 
		if (sIndex == 1) {
			$('#searchForm input[name=supplyCompSearchTxt]').val(arrSupplyComp[0]);
		} else {
			supplyCompTxt = sIndex + " 개";
			$('#supplyCompTxt').html(supplyCompTxt);
		}
		
		var jsonData = JSON.stringify(arrSupplyComp);
		$("#searchForm input[name=supplyCompList]").val(jsonData);
	}
	
	// 브랜드 조회 팝업에서 호출
	var fnSetBrandInfo = function(result) {
		var arrbrandCd = [];
		var brandTxt = "";
		var bIndex = 0;
		$('#brandTxt').html('');
		$('#searchForm input[name=brandSearchTxt]').val('');
		
		result.forEach(function(brand){
			bIndex++; 
			arrbrandCd.push(brand.brandCd);
		});

		// 조회 값이 하나일 경우 화면에 코드 노출. 그 외는 갯수 처리 
		if (bIndex == 1) {
			$('#searchForm input[name=brandSearchTxt]').val(arrbrandCd[0]);
		} else {
			brandTxt = bIndex + " 개";
			$('#searchForm').find('#brandTxt').html(brandTxt);
		}
		
		var jsonData = JSON.stringify(arrbrandCd);
		$("#searchForm input[name=brandList]").val(jsonData);
	}
	
	// 검색
	$('#btnSearch').on('click', function() {
		// 입력 값 체크
		if (!gagajf.validation($('#searchForm')))
			return false;
		
		gagaAgGrid.fetch($('#searchForm').prop('action'), gridOptions, '#searchForm');
	});
	
	// 초기화 클릭시
	$('#btnInit').on('click', function() {
		$('#searchForm')[0].reset();
		$('#searchForm input[name=brandList]').val('');
		$('#searchForm input[name=supplyCompList]').val('');
		$('#brandTxt').html('');
		$('#supplyCompTxt').html('');
	});
	
	// 엑셀다운로드
	$('#btnExcel').on('click', function() {
		gagaAgGrid.exportToExcel('상품정산 목록', gridOptions);
	});
	
	$(document).ready(function() {
		cfnCreateCalendar('#terms', 'salesStdt', 'salesEddt', true, '매출');
		$('.btnToday').trigger('click');
		
		$('#extmallVendor').hide();
		
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
	});
/*]]>*/
</script>

</html>
