<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : GoodsEpSkipPopupForm.html
 * @desc    : 네이버 EP 제외 상품 예약 화면
 *============================================================================
 * STYLE24
 * Copyright(C) 2019 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.11.03   eskim       최초 작성
 *******************************************************************************
 -->	
	<div class="modalPopup"  data-width="750" >
		<div class="panelStyle">
			<div class="panelTitle">
				<h2>상품비노출예약</h2>
				<button type="button" class="close" onclick="uifnPopupClose('popupGoodsEpSkip')"><i class="fa fa-times"></i></button>
			</div>
			<form id="goodsRsvtEpSkipForm" name="goodsRsvtEpSkipForm" >
			<input type="hidden" id="goodsCd" name="goodsCd" />
			<div class="panelContent">
				<table class="frmStyle">
					<colgroup>
						<col style="width:10%;"/>
						<col/>
					</colgroup>
					<tr>
						<th>비노출예약일<em class="required" title="필수"></em></th>
						<td>
							<input name="applyStYMD" id="applyStYMD" type="text" class="w80 schDate" maxlength="10" required="required" data-valid-name="예약시작일" th:value="${#calendars.format(#calendars.createNow(), 'yyyy-MM-dd')}"/>
							<input name="applyStdt" id="applyStdt" type="hidden" />
							~
							<input name="applyEdYMD" id="applyEdYMD" type="text" class="w80 schDate" maxlength="10" required="required" data-valid-name="예약 종료일" th:value="${#calendars.format(#calendars.createNow(), 'yyyy-MM-dd')}"/>
							<input name="applyEddt" id="applyEddt" type="hidden" />
						</td>
					</tr>
				</table>
			</div>
			<ul class="panelBar">
				<li class="left">
					<button type="button" class="btn btn-danger btn-lg" id="btnDeleteGoodsEpSkip">상품삭제</button>
				</li>
				<li class="right">
					<button type="button" class="btn btn-base btn-lg" id="btnSearchExcel">엑셀조회</button>
					<button type="button" class="btn btn-info btn-lg" id="btnSearchGoods">상품조회</button>
				</li>
			</ul>
			<div id="gridGoodsRsvtEpSkipList" style="width: 100%; height: 500px;" class="ag-theme-balham"></div>
			</form>
			<ul class="panelBar">
				<li class="right">
					<button type="button" class="btnRight btn btn-base btn-lg" id=btnSaveGoodsRsvtTnm>저장</button>
				</li>
			</ul>
		</div>
	</div>
<script th:inline="javascript">
/*<![CDATA[*/
	var rsvtGoodsTnmColumnDefs = [
		{width: 40, minWidth: 40, cellClass: 'text-center', headerCheckboxSelection: true, checkboxSelection: true, filter: false},
		{headerName: 'No', width: 60, cellClass: 'text-center',valueGetter: function(params) { return params.node.rowIndex+1}},
		{headerName: "브랜드명", field: "brandEnm", width: 140, cellClass: 'text-center'},
		{headerName: "상품코드", field: "goodsCd", width: 140, cellClass: 'text-center'},
		{headerName: "상품명", field: "goodsNm", width: 250, cellClass: 'text-left'}
	];
	
	// Get GridOptions
	var rsvtGoodsEpSkipGridOptions = gagaAgGrid.getGridOptions(rsvtGoodsTnmColumnDefs);

	// 중복 선택 가능
	rsvtGoodsEpSkipGridOptions.rowSelection = 'multiple';
	rsvtGoodsEpSkipGridOptions.suppressRowClickSelection = true;

	// 상품 조회 클릭 시
	$('#btnSearchGoods').on('click', function() {
		cfnOpenGoodsPopup('fnSearchGoods');
	});

	// 상품 조회 콜백함수
	var fnSearchGoods = function(result) {
		if (result.length < 1) return;
		var oldData = gagaAgGrid.getAllRowData(rsvtGoodsEpSkipGridOptions);
		$.each(result, function(idx, item) {
			var isInvalid = false;
			if (oldData != null && oldData.length != 0){
				oldData.forEach(function(oneData){
					if(oneData.goodsCd == item.goodsCd){
						isInvalid = true;
						return true;
					}
				});
				if(isInvalid){
					return isInvalid;
				}
			}
			gagaAgGrid.addRowData(rsvtGoodsEpSkipGridOptions, {"goodsCd" : item.goodsCd, "goodsNm" : item.goodsNm, "brandEnm" : item.brandEnm});
		});
		uifnPopupClose('popupGoods');
		return;
	};

	// 저장 클릭 시
	$('#btnSaveGoodsRsvtTnm').on('click', function() {
		
		var fromDate = $('#goodsRsvtEpSkipForm input[name=applyStYMD]').val();
		var toDate = $('#goodsRsvtEpSkipForm input[name=applyEdYMD]').val();

		if (gagajf.isNull(fromDate)) {
			mcxDialog.alertC("예약  시작일자를 입력하세요.", {
				sureBtnText: "확인",
				sureBtnClick: function() {
					$('#goodsRsvtEpSkipForm input[name=applyStYMD]').focus();
				}
			});
			return false;
		}
		
		if (gagajf.isNull(toDate)) {
			mcxDialog.alertC("예약  종료일자를 입력하세요.",  {
				sureBtnText: "확인",
				sureBtnClick: function() {
					$('#goodsRsvtEpSkipForm input[name=applyEdYMD]').focus();
				}
			});
			return false;
		}

		if (fromDate > toDate) {
			mcxDialog.alertC("예약 시작일자는 종료일자 보다 클 수 없습니다.", {
				sureBtnText: "확인",
				sureBtnClick: function() {
					$('#goodsRsvtEpSkipForm input[name=applyEdYMD]').focus();
				}
			});
			return false;
		} 
		
		var applyStdt = $('#goodsRsvtEpSkipForm input[name=applyStYMD]').val().replaceAll("-","");

		var applyEddt = $('#goodsRsvtEpSkipForm input[name=applyEdYMD]').val().replaceAll("-","");
		
		$('#goodsRsvtEpSkipForm input[name=applyStdt]').val(applyStdt);
		$('#goodsRsvtEpSkipForm input[name=applyEddt]').val(applyEddt);
		
		if ($('#goodsRsvtEpSkipForm input[name=applyStdt]').val() > $('#goodsRsvtEpSkipForm input[name=applyEddt]').val()) {
			mcxDialog.alertC("예약 시작일자는 종료일자 보다 클 수 없습니다.", {
				sureBtnText: "확인",
				sureBtnClick: function() {
					$('#goodsRsvtEpSkipForm input[name=applyEdYMD]').focus();
				}
			});
			return false;
		}
		
		var toDateStr = new Date().format("YYYYMMDD");

		if (toDateStr > applyStdt){
			mcxDialog.alertC("예약 시작일시는 현재일시 보다 작을 수 없습니다.", {
				sureBtnText: "확인",
				sureBtnClick: function() {
					$('#goodsRsvtEpSkipForm input[name=applyStYMD]').focus();
				}
			});
			return false;
		}

		if (toDateStr >= applyEddt){
			mcxDialog.alertC("예약 종료일시는 현재일시 보다 작거나 같을 수 없습니다.", {
				sureBtnText: "확인",
				sureBtnClick: function() {
					$('#goodsRsvtEpSkipForm input[name=applyEdYMD]').focus();
				}
			});
			return false;
		}
		
		mcxDialog.confirm('저장하시겠습니까?', {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function(){
				var arrGoodsCd = [];
				var allRowData = gagaAgGrid.getAllRowData(rsvtGoodsEpSkipGridOptions);
				allRowData.forEach(function(item, index) {
					arrGoodsCd.push(item.goodsCd);
				});

				var data = {applyStdt : $('#goodsRsvtEpSkipForm input[name=applyStdt]').val()
						, applyEddt : $('#goodsRsvtEpSkipForm input[name=applyEddt]').val()
						, arrGoodsCd : arrGoodsCd
				};
				
				var jsonData = JSON.stringify(data);
				gagajf.ajaxJsonSubmit('/goods/rsvt/ep/skip/save', jsonData, fnGoodsRsvtEpSkipListFormClose);
			}
		});
	});

	//엑셀 상품 조회
	$('#btnSearchExcel').on('click', function() {
		cfnExcelUploadPopup('goodsRsvtEpSkipExcelUpload', 'goodsRsvtEpSkipExcelUpload');
	});

	var goodsRsvtEpSkipExcelUpload = function(result){
		var data = {procJob : result.procJob
			,excelFileNm : result.excelFileNm
		};
		var jsonData = JSON.stringify(data);
		gagajf.ajaxJsonSubmit('/goods/search/excelupload/save', jsonData, fnRsvtGoodsEpSkipExcelUploadCallBack);
	}

	var fnRsvtGoodsEpSkipExcelUploadCallBack = function(result){
		gagajf.ajaxJsonSubmit('/goods/excel/upload/goods/list', '', fnExcelSearchCallBack);
	}

	var fnExcelSearchCallBack = function(result){
		if (result.goodsExcelList.length < 1) return;
		var oldData = gagaAgGrid.getAllRowData(rsvtGoodsEpSkipGridOptions);
		$.each(result.goodsExcelList, function(idx, item) {
			var isInvalid = false;
			if (oldData != null && oldData.length != 0){
				oldData.forEach(function(oneData){
					if(oneData.goodsCd == item.goodsCd){
						isInvalid = true;
						return true;
					}
				});
				if(isInvalid){
					return isInvalid;
				}
			}
			gagaAgGrid.addRowData(rsvtGoodsEpSkipGridOptions, {"goodsCd" : item.goodsCd, "goodsNm" : item.goodsNm, "brandEnm" : item.brandEnm});
		});
		return;
	}
	
	// 조회상품 삭제
	$('#btnDeleteGoodsEpSkip').on('click', function() {
		var selectedData = rsvtGoodsEpSkipGridOptions.api.getSelectedRows();
		if (selectedData.length == 0) {
			mcxDialog.alert('선택된 행이 없습니다.');
			return;
		}

		//화면에서 삭제
		var removedData = gagaAgGrid.removeRowData(rsvtGoodsEpSkipGridOptions, false);
	});
	
	//창종료
	var fnGoodsRsvtEpSkipListFormClose = function(){
		uifnPopupClose('popupGoodsEpSkip');
		fnGoodsEpSkipResDeleteCallBack();
	}

	$(document).ready(function() {
		// Create a agGrid
		gagaAgGrid.createGrid('gridGoodsRsvtEpSkipList', rsvtGoodsEpSkipGridOptions);
	});

/*]]>*/
</script>
</html>