<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : GoodsEpSkipForm.html
 * @desc    : 네이버EP제외상품관리
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.11.03   eskim       최초 작성
 *******************************************************************************
 -->
	<div id="main">
		<!-- 메인타이틀 영역 -->
		<div class="main-title">
		</div>
		<!-- //메인타이틀 영역 -->
		<!-- 메뉴 설명 -->
		<div class="infoBox menu-desc">
		</div>
		<form id="goodsEpSkipListForm" name="goodsEpSkipListForm" action="#" th:action="@{'/goods/ep/skip/list'}">
		<input type="hidden" id="searchGb" name="searchGb" />
 		<!-- 패널 영역1 -->
		<div class="panelStyle" >
			<!-- TITLE -->
			<div class="panelTitle">
				<h3><i class="fa fa-info-circle"></i>아래 검색조건 중 하나를 꼭 입력해 주세요.</h3>
			</div>
			<!-- //TITLE -->
			<div class="panelContent">
				<table class="frmStyle">
					<colgroup>
						<col width="8%"/>
						<col width="17%"/>
						<col width="8%"/>
						<col width="17%"/>
						<col width="8%"/>
						<col width="17%"/>
						<col width="8%"/>
						<col />
					</colgroup>
					<tr>
						<th>업체</th>
						<td>
							<input type="text" class="w100" name="supplyCompSearchTxt" id="supplyCompSearchTxt" maxlength="20" />
							<button type="button" class="btn icn" id="btnSearchSupplyComp"><i class="fa fa-search"></i></button>
							<span id="supplyCompText"></span>
							<input type="hidden" name="supplyCompList"/>
						</td>
						<th>브랜드</th>
						<td>
							<input type="text" class="w100" name="searchTxt" id="searchTxt" maxlength="20" />
							<button type="button" class="btn icn" id="btnSearchBrand"><i class="fa fa-search"></i></button>
							<!-- <input type="text" class="w100" name="brandCd" readonly="readonly"/> -->
							<span id="brandText"></span>
							<input type="hidden" name="brandList"/>
						</td>
						<th>키워드</th>
						<td>
							<select name="search" id="search">
								<option value="searchGoodsCd">상품코드</option>
								<option value="searchGoodsNm">상품명</option>
								<option value="searchGoodsNum">품번</option>
								<option value="searchSupplyGoodsCd">업체상품코드</option>
							</select>
							<input type="text" class="w50p" name="condition" id="condition" maxlength="50"/>
						</td>
						<th>시즌/년도</th>
						<td>
							<select  name="styleYear" id="styleYear">
								<option value="">[전체]</option>
								<option th:if="${styleYearList}" th:each="oneData, status : ${styleYearList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
							<select  name="seasonCd" id="seasonCd">
								<option value="">[전체]</option>
								<option th:if="${seasonList}" th:each="oneData, status : ${seasonList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
					</tr>
					<tr>
						<th>비노출예약일</th>
						<td colspan="7" id="sellTerms"></td>
					</tr>
				</table>
				<ul class="panelBar">
					<li class="center">
						<button type="button" class="btn btn-gray btn-lg" id="btnInit" >초기화</button>
						<button type="button" class="btn btn-info btn-lg" id="btnSearch" >조회</button>
					</li>
				</ul>	
			</div>
			<!-- //검색조건 영역 -->
		</div>
		<!-- 패널 영역1 -->
		<div class="panelStyle">
			<!-- 검색결과 영역 -->
			<!-- 상단버튼 영역  -->
			<ul class="panelBar">
				<li class="left">
					<button type="button" class="btn btn-default btn-lg" onclick="cfnDownloadSampleFile('SF003');">상품엑셀조회 양식 다운로드</button>
					<button type="button" class="btn btn-base btn-lg" id="btnGoodsExcelUpLoad">엑셀조회</button>
					<button type="button" class="btn btn-success  btn-lg" id="btnGoodsEpSkipSave">비노출예약등록</button>
					<button type="button" class="btn btn-danger btn-lg" id="btnGoodsEpSkipDelete">비노출예약삭제</button>
				</li>
				<li class="right">
					검색결과 : <strong><span id="gridRowTotalCount">0</span> 건</strong>&nbsp;
					쪽번호 <span id="pgNo">0</span>/ <strong id="endPgNo">0</strong>&nbsp;&nbsp;
					<select id="pageSize" name="pageSize">
						<option value="50" selected="selected">50개씩 보기</option>
						<option value="100">100개씩 보기</option>
						<option value="500">500개씩 보기</option>
						<!-- <option value="1000">1000개씩 보기</option> -->
					</select>
					<input type="hidden" name="pageNo" id="pageNo" value ="1"/>
				</li>
			</ul>
			<!-- //상단버튼 영역  -->
			<div id="gridList" style="width: 100%; height: 550px;" class="ag-theme-balham"></div>
			<ul class="panelBar">
				<li class="center">
					<div class="tablePaging" id="goodsListPagination"></div>
				</li>
			</ul>
			<!-- 검색결과 영역 -->
		</div>
		</form>
		<!-- //패널 영역2 -->
	</div>
<script type="text/javascript" src="/ux/plugins/gaga/gaga.paging.js?v=2019072202"></script>
<script th:inline="javascript">
/*<![CDATA[*/
	var sessRoleCd = [[${sessionInfo.roleCd}]];
	var goodsStatList = gagajf.convertToArray([[${goodsStatList}]]);
	var formalGbList = gagajf.convertToArray([[${formalGbList}]]);
	var columnDefs = [
		{width: 40, minWidth: 40, cellClass: 'text-right', headerCheckboxSelection: true, checkboxSelection: true, filter: false},
		{headerName: 'No', width: 60, cellClass: 'text-center',
			valueGetter: function(params) { return cfnGridNumner('goodsEpSkipListForm',params.node.rowIndex, 'A');}
		},
		{headerName: "브랜드명", field: "brandEnm", width: 130, cellClass: 'text-center'},
		{headerName: "상품코드", field: "goodsCd", width: 140, cellClass: 'text-center'},
		{headerName: "상품명", field: "goodsNm", width: 200, cellClass: 'text-left'
			,cellRenderer: function(params) {
				return '<a href="javascript:void(0);">' + params.value + '</a>';
			}
		},
		{headerName: "상품상태", field: "goodsStat" , width: 100, cellClass: 'text-center',
			cellEditorParams: { values: gagaAgGrid.extractValues(goodsStatList) },
			valueFormatter: function (params) { return gagaAgGrid.lookupValue(goodsStatList, params.value); },
			valueParser: function (params) { return gagaAgGrid.lookupKey(goodsStatList, params.newValue); }
		},
		{headerName: "예약시작일", field: "applyStdt", width: 150, cellClass: 'text-center',
			cellRenderer: function(params) {
				return !gagajf.isNull(params.value) ? params.value.toDate("YYYYMMDDHHmmss").format("YYYY-MM-DD") : '';
			}
		},
		{headerName: "예약종료일", field: "applyEddt", width: 150, cellClass: 'text-center',
			cellRenderer: function(params) {
				return !gagajf.isNull(params.value) ? params.value.toDate("YYYYMMDDHHmmss").format("YYYY-MM-DD") : '';
			}
		},
		{headerName: "등록일시", field: "regDt", width: 150, cellClass: 'text-center',
			cellRenderer: function(params) {
				return !gagajf.isNull(params.value) ? params.value.toDate("YYYYMMDDHHmmss").format("YYYY-MM-DD HH:mm:ss") : '';
			}
		},
		{headerName: "등록자", field: "regNm", width: 100, cellClass: 'text-center'},
		{headerName: "수정일시", field: "updDt", width: 150, cellClass: 'text-center',
			cellRenderer: function(params) {
				return !gagajf.isNull(params.value) ? params.value.toDate("YYYYMMDDHHmmss").format("YYYY-MM-DD HH:mm:ss") : '';
			}
		},
		{headerName: "수정자", field: "updNm", width: 100, cellClass: 'text-center'}
	];
	
	// Get GridOptions
	var gridOptions = gagaAgGrid.getGridOptions(columnDefs);

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

	// Row Click
	gridOptions.onCellClicked = function(event) {
		var goodsCd = event.data.goodsCd;
		if (event.colDef.field == "goodsNm"){
			cfnOpenGoodsDetailPopup('U',goodsCd);
		}else if (event.colDef.field == "goodsCd"){
			
		}
	}

	// 초기화 클릭시
	$('#btnInit').on('click', function() {
		fnInit();
	});
	
	var fnInit = function(){
		$('#goodsEpSkipListForm')[0].reset();
		$("#goodsEpSkipListForm input[type=checkbox]").removeClass("checked");
		$("#goodsEpSkipListForm input[type=checkbox]").parent("label").removeClass("checked");
		$("#goodsEpSkipListForm input[type=radio][checked]").parent("label").addClass("checked");
		$('#goodsEpSkipListForm input[name=brandList]').val('');
		$('#goodsEpSkipListForm input[name=supplyCompList]').val('');
		$('#goodsEpSkipListForm').find('#brandText').html('');
		$('#goodsEpSkipListForm').find('#supplyCompText').html('');
	}
	
	// 조회클릭시
	$('#btnSearch').on('click', function() {
		$("#goodsEpSkipListForm input[name=pageNo]").val('1');
		fnGoodsEpSkipListSearch('BASIC');
	});

	// 조회
	var fnGoodsEpSkipListSearch = function(gbn) {
		
		if (typeof(gbn) != 'undefined' &&  gbn == 'EXCEL'){
			$("#goodsEpSkipListForm input[name=searchGb]").val("EXCEL");
		}else{
			$("#goodsEpSkipListForm input[name=searchGb]").val("BASIC");
		}
		
		if(!fnConditionCheck()) return;
		
		gagaPaging.init('goodsEpSkipListForm', fnSearchCallBack, 'goodsListPagination', $('#goodsEpSkipListForm').find('#pageSize').val());
		gagaPaging.load($("#goodsEpSkipListForm input[name=pageNo]").val());
	}

	// 조회 (등록 창 닫을 때)
	var fnGoodsRsvtEpSkipListSearch = function(gbn) {

		if (typeof(gbn) != 'undefined' &&  gbn == 'EXCEL'){
			$("#goodsEpSkipListForm input[name=searchGb]").val("EXCEL");
		}else{
			$("#goodsEpSkipListForm input[name=searchGb]").val("BASIC");
		}

		gagaPaging.init('goodsEpSkipListForm', fnSearchCallBack, 'goodsListPagination', $('#goodsEpSkipListForm').find('#pageSize').val());
		gagaPaging.load($("#goodsEpSkipListForm input[name=pageNo]").val());
	}

	//검색 조건 확인
	var fnConditionCheck = function(){
		var formId = '#goodsEpSkipListForm';
		var form = document.goodsEpSkipListForm;

		if($("#goodsEpSkipListForm input[name=searchGb]").val() == "EXCEL") return true;
		
		var searchFlag = false;
		var cnt = 0;

		if( !gagajf.isNull($("#goodsEpSkipListForm input[name=supplyCompList]").val())
				|| !gagajf.isNull($("#goodsEpSkipListForm input[name=brandList]").val())
			){
			searchFlag = true;
		}else{
			for (i = 0; i < form.elements.length; i++ ) {
				var el = form.elements[i];
	
				if ($(el).prop("type") == "text" || ($(el).prop("type") == "select-one" && el.name != "search" && el.name != "pageSize")) {
					if (!(el.value == null || el.value == "")) {
						cnt++;
					}
				}
			}
			if(cnt > 0) searchFlag = true;
		}

		if(searchFlag == false){
			mcxDialog.alert("검색조건을 입력하세요.");
			return false;
		}
		
		var fromDate = $('#goodsEpSkipListForm input[name=stDate]').val();
		var toDate = $('#goodsEpSkipListForm input[name=edDate]').val();
		
		if (!gagajf.isNull(fromDate) || !gagajf.isNull(toDate)) {
			
			if (gagajf.isNull(fromDate) || gagajf.isNull(toDate)) {
				mcxDialog.alertC("등록일 조회시 시작일자와 종료일자를 입력하세요.", {
					sureBtnText: "확인",
					sureBtnClick: function(){
						$('#goodsEpSkipListForm input[name=stDate]').focus();
					}
				});
				return false;
			}

			if (fromDate > toDate) {
				mcxDialog.alertC("노출기간 시작일자는 종료일자 보다 클 수 없습니다.", {
					sureBtnText: "확인",
					sureBtnClick: function(){
						$('#goodsEpSkipListForm input[name=stDate]').focus();
					}
				});
				return false;
			} 
		}

		return true;
	}
	
	var fnSearchCallBack = function(result){

		$('#goodsEpSkipListForm').find('#gridRowTotalCount').html(result.pageing.pageable.totalCount.addComma());
		$('#goodsEpSkipListForm').find('#pageNo').val(result.pageing.pageable.pageNo.addComma());
		$('#goodsEpSkipListForm').find('#pgNo').html(result.pageing.pageable.pageNo.addComma());
		$('#goodsEpSkipListForm').find('#endPgNo').html(result.pageing.pageable.totalPage.addComma());
		gridOptions.api.setRowData(result.goodsEpSkipList);
		gagaPaging.createPagination(result.pageing.pageable);
	}
	
	//페이징 
	$('#goodsEpSkipListForm select[name=pageSize]').on('change', function() {
		$("#goodsEpSkipListForm input[name=pageNo]").val('1');
		fnGoodsEpSkipListSearch($("#goodsEpSkipListForm input[name=searchGb]").val());
	});
	
	//업체변경시
	/* $('#goodsEpSkipListForm select[name=supplyCompCd]').on('change', function() {
		var actionUrl = '/renderer/supplyCompany/brand/list/' + $(this).val();

		if(sessRoleCd == "G001_B000"){
			actionUrl = '/renderer/brand/AuthBrandlist';
		}
		$("#goodsEpSkipListForm select[name=brandCd] option:gt(0)").remove();

		cfnCreateCombo(actionUrl, $('#goodsEpSkipListForm select[name=brandCd]'), "[전체]", "");
	});
	 */
	// 업체 조회 선택시
	$('#btnSearchSupplyComp').on('click', function() {
		cfnOpenCompanyListPopup('fnSetSupplyCompInfo', 'M');
	});
	
	// 업체 조회 팝업에서 호출
	var fnSetSupplyCompInfo = function(result) {
		var arrSupplyComp = [];
		var supplyCompText = "";
		var sIndex = 0;
		$('#goodsEpSkipListForm').find('#supplyCompText').html('');
		$('#goodsEpSkipListForm input[name=supplyCompSearchTxt]').val('');
		result.forEach(function(supplyComp){
			sIndex++; 
			arrSupplyComp.push(supplyComp.supplyCompCd);
		});

		// 조회값이 하나일 경우 화면에 코드 노출 그외는 갯수 처리 
		if (sIndex == 1) {
			$('#goodsEpSkipListForm input[name=supplyCompSearchTxt]').val(arrSupplyComp[0]);
		}else{
			supplyCompText = sIndex + " 개";
			$('#goodsEpSkipListForm').find('#supplyCompText').html(supplyCompText);	
		}
		var jsonData = JSON.stringify(arrSupplyComp);
		$("#goodsEpSkipListForm input[name=supplyCompList]").val(jsonData);
	}
	
	// 브랜드 조회 선택시
	$('#btnSearchBrand').on('click', function() {
		cfnOpenBrandListPopup('fnSetBrandInfo', 'M');
	});
	
	// 브랜드 조회 팝업에서 호출
	var fnSetBrandInfo = function(result) {
		var arrbrandCd = [];
		var brandText = "";
		var bIndex = 0;
		$('#goodsEpSkipListForm').find('#brandText').html('');
		$('#goodsEpSkipListForm input[name=searchTxt]').val('');
		result.forEach(function(brand){
			bIndex++; 
			arrbrandCd.push(brand.brandCd);
		});

		// 조회값이 하나일 경우 화면에 코드 노출 그외는 갯수 처리 
		if (bIndex == 1) {
			$('#goodsEpSkipListForm input[name=searchTxt]').val(arrbrandCd[0]);
		}else{
			brandText = bIndex + " 개";
			$('#goodsEpSkipListForm').find('#brandText').html(brandText);	
		}
		var jsonData = JSON.stringify(arrbrandCd);
		$("#goodsEpSkipListForm input[name=brandList]").val(jsonData);
	}
	
	//엑셀 상품 조회
	$('#btnGoodsExcelUpLoad').on('click', function() {
		cfnExcelUploadPopup('goodsEpSkipExcelUpload', 'goodsEpSkipExcelUpload');
	});
	
	var goodsEpSkipExcelUpload = function(result){
		var data = {procJob : result.procJob
					,excelFileNm : result.excelFileNm
					};
		var jsonData = JSON.stringify(data);
		gagajf.ajaxJsonSubmit('/goods/search/excelupload/save', jsonData, fngoodsEpSkipExcelUploadCallBack);
	}
	
	var fngoodsEpSkipExcelUploadCallBack = function(result){
		fnGoodsEpSkipListSearch("EXCEL");
	}
	
	//예약등록
	$('#btnGoodsEpSkipSave').click(function(e) {
		var actionUrl = "/goods/ep/skip/popup/form";
		cfnOpenModalPopup(actionUrl, 'popupGoodsEpSkip');
	});
	
	//예약삭제
	$('#btnGoodsEpSkipDelete').click(function(e) {
		//상품선택여부 확인처리 추가
		var selectedData = gridOptions.api.getSelectedRows();

		if (selectedData.length == 0) {
			mcxDialog.alert('선택된 행이 없습니다.');
			return false;
		}
		
		var arrGoodsCd = [];
		var arrGoodsEpSkipSq = [];
		var chkFlag = false;
		$.each(selectedData, function(idx, item) {
			
			var toDateStr = new Date().format("YYYYMMDDHHmmss");
			if (toDateStr > item.applyEddt){
				chkFlag = true;
				mcxDialog.alertC("종료된 네이버 EP 제외 상품은 삭제할 수 없습니다.",  {
					sureBtnText: "확인",
					sureBtnClick: function(){
						$('#goodsRsvtEpSkipForm input[name=applyEdYMD]').focus();
					}
				});
				return false;
			}
		
			arrGoodsCd.push(item.goodsCd);
			arrGoodsEpSkipSq.push(item.goodsEpSkipSq);
		});

		if (chkFlag){
			return;
		}
		
		mcxDialog.confirm('삭제하시겠습니까?', {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function(){
				var data = {arrGoodsCd : arrGoodsCd
						,arrGoodsEpSkipSq : arrGoodsEpSkipSq
							};
			
				var jsonData = JSON.stringify(data);
				gagajf.ajaxJsonSubmit('/goods/rsvt/ep/skip/delete', jsonData, fnGoodsEpSkipResDeleteCallBack);
			}
		});
	});
	
	var fnGoodsEpSkipResDeleteCallBack = function(){
		//fnGoodsRsvtEpSkipListSearch($("#goodsEpSkipListForm input[name=searchGb]").val());
	}
	
	$(document).ready(function() {

		cfnCreateCalendar('#sellTerms', 'applyStdt', 'applyEddt', true, '예약일', 'X');
		var chkBeforSkipFlag = '&nbsp;&nbsp;<label class="chkBox"><input type="checkbox" name="beforSkipFlag" value="Y" >이전데이터 제외</label>';
		$("#goodsEpSkipListForm").find('#sellTerms').append(chkBeforSkipFlag);
		
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);

	});

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