<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : GoodsReserveSellPopupForm.html
 * @desc    : 상품예약판매 등록 화면
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.11.12   eskim       최초 작성
 *******************************************************************************
 -->	
	<div class="modalPopup"   data-width="700" >
		<div class="panelStyle">
			<div class="panelTitle">
				<h2>예약판매등록</h2>
				<button type="button" class="close" onclick="uifnPopupClose('popupGoodsRsvtList')"><i class="fa fa-times"></i></button>
			</div>
			<form id="goodsResSellRegisterForm" name="goodsResSellRegisterForm" >
			<div class="panelContent">
				<table class="frmStyle">
					<colgroup>
						<col style="width:10%;"/>
						<col/>
					</colgroup>
					<tr>
						<th>출고예정일<em class="required" title="필수"></em></th>
						<td>
							<input name="delvResDt" id="delvResDt" type="text" class="w80 schDate" maxlength="10" required="required" data-valid-name="출고예정일" th:value="${#calendars.format(#calendars.createNow(), 'yyyy-MM-dd')}"/>
						</td>
					</tr>
				</table>
			</div>	
				<ul class="panelBar">
					<li class="left">
						<button type="button" class="btn btn-danger btn-lg" id="btnDeleteGoodsResSell">상품삭제</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="gridGoodsResSellRegisterList" style="width: 100%; height: 500px;" class="ag-theme-balham"></div>
				<ul class="panelBar">
					<li class="right">
						<button type="button" class="btnRight btn btn-base btn-lg" id=btnSaveGoodsResSell>저장</button>
					</li>
				</ul>
			</form>
		</div>
	</div>
<script th:inline="javascript">
/*<![CDATA[*/
	var goodsResSellRegisterColumnDefs = [
		{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 goodsResSellRegisterGridOptions = gagaAgGrid.getGridOptions(goodsResSellRegisterColumnDefs);

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

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

	// 상품 조회 콜백함수
	var fnSearchGoods = function(result) {
		if (result.length < 1) return;
		var oldData = gagaAgGrid.getAllRowData(goodsResSellRegisterGridOptions);
		$.each(result, function(idx, item) {
			//if(item.goodsType != 'G056_N'){
			//	return true;
			//}
			if("Y" != item.selfGoodsYn){
				return true;
			}
			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(goodsResSellRegisterGridOptions, {"goodsCd" : item.goodsCd, "goodsNm" : item.goodsNm, "brandEnm" : item.brandEnm});
		});
		uifnPopupClose('popupGoods');
		return;
	};

	// 저장 클릭 시
	$('#btnSaveGoodsResSell').on('click', function() {
		var delvResDt = $('#goodsResSellRegisterForm input[name=delvResDt]').val();

		if (gagajf.isNull(delvResDt)) {
			mcxDialog.alertC("출고예정일을 입력하세요.", {
				sureBtnText: "확인",
				sureBtnClick: function() {
					$('#goodsResSellRegisterForm input[name=delvResDt]').focus();
				}
			});
			return false;
		}

		delvResDt = $('#goodsResSellRegisterForm input[name=delvResDt]').val().replaceAll("-","");

		var toDateStr = new Date().format("YYYYMMDD");

		if (toDateStr >= delvResDt){
			mcxDialog.alertC("출고예정일은 현재일시 보다 작거나 같을 수 없습니다.", {
				sureBtnText: "확인",
				sureBtnClick: function() {
					$('#goodsResSellRegisterForm input[name=delvResDt]').focus();
				}
			});
			return false;
		}

		mcxDialog.confirm('저장하시겠습니까?', {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function(){
				var arrGoodsCd = [];
				var allRowData = gagaAgGrid.getAllRowData(goodsResSellRegisterGridOptions);
				allRowData.forEach(function(item, index) {
					arrGoodsCd.push(item.goodsCd);
				});

				var data = {delvResDt : delvResDt
						, arrGoodsCd : arrGoodsCd
				};
				
				var jsonData = JSON.stringify(data);
				gagajf.ajaxJsonSubmit('/goods/reserve/sell/regist/save', jsonData, fnGoodsResSellRegisterFormClose);
			}
		});
	});

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

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

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

	var fnExcelSearchCallBack = function(result){
		if (result.goodsExcelList.length < 1) return;
		var oldData = gagaAgGrid.getAllRowData(goodsResSellRegisterGridOptions);
		$.each(result.goodsExcelList, function(idx, item) {
			if(item.goodsType != 'G056_N'){
				return true;
			}
			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(goodsResSellRegisterGridOptions, {"goodsCd" : item.goodsCd, "goodsNm" : item.goodsNm, "brandEnm" : item.brandEnm});
		});

		return;
	}
	
	// 조회상품 삭제
	$('#btnDeleteGoodsResSell').on('click', function() {
		var selectedData = goodsResSellRegisterGridOptions.api.getSelectedRows();
		if (selectedData.length == 0) {
			mcxDialog.alert('선택된 행이 없습니다.');
			return;
		}

		//화면에서 삭제
		var removedData = gagaAgGrid.removeRowData(goodsResSellRegisterGridOptions, false);
	});
	
	//창종료
	var fnGoodsResSellRegisterFormClose = function(){
		uifnPopupClose('popupGoodsRsvtList');
		fnGoodsResSellDeleteCallBack();
	}

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

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