<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : GoodsQuikDeliverySkipForm.html
 * @desc    : 총알배송제외 상품관리 조회
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2021.02.25   eskim       최초 작성
 *******************************************************************************
 -->
	<div id="main">
		<!-- 메인타이틀 영역 -->
		<div class="main-title">
		</div>
		<!-- //메인타이틀 영역 -->
		
		<!-- 메뉴 설명 -->
		<div class="infoBox menu-desc">
		</div>
		<form id="searchForm" name="searchForm" action="#" th:action="@{'/goods/quik/delivery/list'}">
		<input type="hidden" id="searchGb" name="searchGb" />
		<input type="hidden" id="search" name="search" value="searchGoodsCd"/>
 		<!-- 패널 영역1 -->
		<div class="panelStyle" >
			<!-- TITLE -->
			<div class="panelTitle">
				<h3><i class="fa fa-info-circle"></i>아래 검색조건 업체, 브랜드, 상품코드, 등록일 중 하나를 꼭 입력해 주세요.</h3>
				<span class="panelControl">
					<i class="fa fa-chevron-up"></i>
				</span>
			</div>
			<!-- //TITLE -->
			<div class="panelContent">
				<table class="frmStyle">
					<colgroup>
						<col width="10%"/>
						<col width="15%"/>
						<col width="10%"/>
						<col width="15%"/>
						<col width="10%"/>
						<col width="15%"/>
						<col width="10%"/>
						<col/>
					</colgroup>
					<tr>
						<th>업체<em class="required" title="필수"></em></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>브랜드<em class="required" title="필수"></em></th>
						<td  colspan="3">
							<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>
							<span id="brandText"></span>
							<input type="hidden" name="brandList"/>
						</td>
						<th rowspan="3" >상품코드<em class="required" title="필수"></em></th>
						<td rowspan="3">
							<textarea class="textareaR3 w150" name="condition" id="condition"></textarea>
						</td>
					</tr>
					<tr>
						<th >년도/시즌</th>
						<td colspan="5">
							<div class="multiCheckBox" style="width:140px;">
								<button type="button" class="sltBtn" data-name="[전체]">[전체]</button>
								<ul style="overflow:auto; height:140px" id="grpStyle">
									<li><label class="chkBox" onclick="uifnAllCheck(this,'grpStyle')"><input type="checkbox" name="전체선택" id="multiStyleYear" >전체선택</label></li>
									<li th:if="${styleYearList}" th:each="oneData, status : ${styleYearList}"><label class="chkBox" data-group="grpStyle"><input type="checkbox" name="multiStyleYear" th:id="${'styleYear' + oneData.cd}" th:value="${oneData.cd}" ><th:block th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></th:block></label></li>
								</ul>
							</div>
							<div class="multiCheckBox" style="width:170px;">
								<button type="button" class="sltBtn" data-name="[전체]">[전체]</button>
								<ul style="overflow:auto; height:170px" id="grpSeason">
									<li><label class="chkBox" onclick="uifnAllCheck(this,'grpSeason')"><input type="checkbox" name="전체선택" id="multiSeasonCd" >전체선택</label></li>
									<li th:if="${seasonList}" th:each="oneData, status : ${seasonList}"><label class="chkBox" data-group="grpSeason"><input type="checkbox" name="multiSeasonCd" th:id="${'seasonCd' + oneData.cd}" th:value="${oneData.cd}" ><th:block th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></th:block></label></li>
								</ul>
							</div>	
						</td>
					</tr>
					<tr>
						<th>등록일<em class="required" title="필수"></em></th>
						<td colspan="5" 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>
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<button type="button" class="btn btn-primary btn-lg" id="btnGoodsQuikDeliverySkipCreate" >상품저장</button>
					</li>
				</ul>
			</div>
			<!-- //검색조건 영역 -->
		</div>
		<!-- 패널 영역1 -->
		<div class="panelStyle">
			<ul class="panelBar">
				<li>
					<button type="button" class="btn btn-danger btn-lg" id="btnGoodsQuikDeliverySkipDelete">상품삭제</button>
				</li>
				<li class="right">
					<button type="button" class="btn btn-default btn-lg" onclick="cfnDownloadSampleFile('SF019');">상품등록 양식 다운로드</button>
					<button type="button" class="btn btn-base btn-lg" id="btnGoodsExcelUpLoad">엑셀상품저장</button>
					검색결과 : <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: 500px;" class="ag-theme-balham"></div>
			<ul class="panelBar">
				<li class="center">
					<div class="tablePaging" id="goodsListPagination"></div>
				</li>
			</ul>
		</div>
		</form>
		<!-- //패널 영역2 -->
<script type="text/javascript" src="/ux/plugins/gaga/gaga.paging.js?v=20210114"></script>
<script th:inline="javascript">
/*<![CDATA[*/
	
	var sessRoleCd = [[${sessionInfo.roleCd}]];
	var goodsStatList = gagajf.convertToArray([[${goodsStatList}]]);
	var columnDefs = [];
	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 params.node.rowIndex + 1 }},
		{headerName: "업체", field: "supplyCompCd", width: 100, cellClass: 'text-center'},
		{headerName: "업체명", field: "supplyCompNm", width: 200, cellClass: 'text-center'},
		{headerName: "브랜드", field: "brandCd", width: 100, cellClass: 'text-center'},
		{headerName: "브랜드명", field: "brandGroupNm", width: 200, cellClass: 'text-center'},
		{headerName: "상품코드", field: "goodsCd", width: 150, 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: "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'}
	];

	// Get GridOptions
	var gridOptions = gagaAgGrid.getGridOptions(columnDefs);

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

	gridOptions.stopEditingWhenGridLosesFocus = true;

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

	// 초기화 클릭시
	$('#btnInit').on('click', function() {

		$('#searchForm')[0].reset();
		//$("#searchForm input[type=radio]").removeClass("checked");
		$("#searchForm input[type=checkbox]").removeClass("checked");
		//$("#searchForm input[type=radio]").parent("label").removeClass("checked");
		$("#searchForm input[type=checkbox]").parent("label").removeClass("checked");
		$("#searchForm input[type=radio][checked]").parent("label").addClass("checked");
		
		$(document).find('#searchForm').find(".multiCheckBox").each(function(i, e){
			let btnNm = $(this).find(".sltBtn").attr("data-name");
			$(this).find(".sltBtn").text(btnNm);
		});

	});

	// 조회클릭시
	$('#btnSearch').on('click', function() {
		fnGoodsListSearch('BASIC');
	});

	// 조회
	var fnGoodsListSearch = function(gbn) {
		
		if (typeof(gbn) != 'undefined' &&  gbn == 'EXCEL'){
			$("#searchForm input[name=searchGb]").val("EXCEL");
		}else{
			$("#searchForm input[name=searchGb]").val("BASIC");
		}

		if(!fnConditionCheck()) return;

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

	}
	
	//검색 조건 확인
	var fnConditionCheck = function(){
		var formId = '#searchForm';
		var form = document.searchForm;
		
		if (event.keyCode === 13) return false;

		if($("#searchForm input[name=searchGb]").val() == "EXCEL") return true;

		var searchFlag = false;
		var cnt = 0;

		if( !gagajf.isNull($("#searchForm input[name=supplyCompList]").val()) 
				|| !gagajf.isNull($("#searchForm textarea[name=condition]").val())
				|| !gagajf.isNull($("#searchForm input[name=brandList]").val())
				|| !gagajf.isNull($("#searchForm input[name=stDate]").val())
				|| !gagajf.isNull($("#searchForm input[name=stDate]").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;
		}
		
		if (!gagajf.isNull($('#searchForm textarea[name=condition]').val())){
			var arrData = $('#searchForm textarea[name=condition]').val().replace(/\r\n/g,"\n").split("\n"); 
			if (arrData.length > 500){
				mcxDialog.alertC("키워드 조회값을 500 이하로 조회 하세요.", {
					sureBtnText: "확인",
					sureBtnClick: function() {
						$('#searchForm textarea[name=condition]').focus();
					}
				});
				return false;
			}
		}
		return true;
	}
	
	// 업체 조회 선택시
	$('#btnSearchSupplyComp').on('click', function() {
		cfnOpenCompanyListPopup('fnSetSupplyCompInfo', 'M');
	});
	
	// 업체 조회 팝업에서 호출
	var fnSetSupplyCompInfo = function(result) {
		var arrSupplyComp = [];
		var supplyCompText = "";
		var sIndex = 0;
		$('#searchForm').find('#supplyCompText').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{
			supplyCompText = sIndex + " 개";
			$('#searchForm').find('#supplyCompText').html(supplyCompText);	
		}
		var jsonData = JSON.stringify(arrSupplyComp);
		$("#searchForm input[name=supplyCompList]").val(jsonData);
	}
	
	// 브랜드 조회 선택시
	$('#btnSearchBrand').on('click', function() {
		cfnOpenBrandListPopup('fnSetBrandInfo', 'M');
	});
	
	// 브랜드 조회 팝업에서 호출
	var fnSetBrandInfo = function(result) {
		var arrbrandCd = [];
		var brandText = "";
		var bIndex = 0;
		$('#searchForm').find('#brandText').html('');
		$('#searchForm input[name=searchTxt]').val('');
		result.forEach(function(brand){
			bIndex++; 
			arrbrandCd.push(brand.brandCd);
		});

		// 조회값이 하나일 경우 화면에 코드 노출 그외는 갯수 처리 
		if (bIndex == 1) {
			$('#searchForm input[name=searchTxt]').val(arrbrandCd[0]);
		}else{
			brandText = bIndex + " 개";
			$('#searchForm').find('#brandText').html(brandText);	
		}
		var jsonData = JSON.stringify(arrbrandCd);
		$("#searchForm input[name=brandList]").val(jsonData);
	}
	
	// 초기화 클릭시
	$('#btnInit').on('click', function() {

		$('#searchForm')[0].reset();
		//$("#searchForm input[type=radio]").removeClass("checked");
		$("#searchForm input[type=checkbox]").removeClass("checked");
		//$("#searchForm input[type=radio]").parent("label").removeClass("checked");
		$("#searchForm input[type=checkbox]").parent("label").removeClass("checked");
		$("#searchForm input[type=radio][checked]").parent("label").addClass("checked");
		$('#searchForm input[name=brandList]').val('');
		$('#searchForm input[name=supplyCompList]').val('');
		$('#searchForm').find('#brandText').html('');
		$('#searchForm').find('#supplyCompText').html('');
	});

	var fnSearchCallBack = function(result){

		$('#searchForm').find('#gridRowTotalCount').html(result.pageing.pageable.totalCount.addComma());
		$('#searchForm').find('#pageNo').val(result.pageing.pageable.pageNo.addComma());
		$('#searchForm').find('#pgNo').html(result.pageing.pageable.pageNo.addComma());
		$('#searchForm').find('#endPgNo').html(result.pageing.pageable.totalPage.addComma());
		gridOptions.api.setRowData(result.goodsList);
		gagaPaging.createPagination(result.pageing.pageable);
		
	}

	//페이징
	$('#searchForm select[name=pageSize]').on('change', function() {
		$("#searchForm input[name=pageNo]").val('1');
		fnGoodsListSearch($("#searchForm input[name=searchGb]").val());
	});
	
	// 상품삭제
	$('#btnGoodsQuikDeliverySkipDelete').click(function(e) {
		//상품선택여부 확인처리 추가
		var selectedData = gridOptions.api.getSelectedRows();

		if (selectedData.length == 0) {
			mcxDialog.alert('선택된 행이 없습니다.');
			return false;
		}
		
		var arrGoodsCd = [];
		$.each(selectedData, function(idx, item) {
			arrGoodsCd.push(item.goodsCd);
		});

		mcxDialog.confirm('삭제하시겠습니까?',  {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function(){
				var data = {arrGoodsCd : arrGoodsCd
			};
			
			var jsonData = JSON.stringify(data);
			gagajf.ajaxJsonSubmit('/goods/quik/delivery/delete', jsonData, fnGoodsQuikDeliverySkipDeleteCollBack);
			}
		});
	});
	
	var fnGoodsQuikDeliverySkipDeleteCollBack = function(){
		fnGoodsListSearch($("#searchForm input[name=searchGb]").val());
	}
	
	// 상품저장 클릭시
	$('#btnGoodsQuikDeliverySkipCreate').on('click', function() {
		if (gagajf.isNull($("#searchForm textarea[name=condition]").val())) {
			mcxDialog.alert("상품코드 입력 후 상품 저장을 선택해 주세요.");
			return;
		}
	
		gagajf.ajaxFormSubmit("/goods/quik/delivery/create", "#searchForm");
		
	});

	// 엑셀등록
	$("#btnGoodsExcelUpLoad").on('click', function() {
		cfnExcelUploadPopup('','fnExcelUpoadCallback');
	});
	
	var fnExcelUpoadCallback = function(data){
		
		var jsonData = JSON.stringify(data);
		gagajf.ajaxJsonSubmit('/goods/quik/delivery/excelupload/save', jsonData, fnGoodsExcelUpLoadCallBack);
		
	}
	
	var fnGoodsExcelUpLoadCallBack = function(result){
		fnGoodsListSearch("EXCEL");
	}
	
	
	$(document).ready(function() {

		cfnCreateCalendar('#sellTerms', 'stDate', 'edDate', true, '등록일', 'X');
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
	});

/*]]>*/
</script>
 	</div>

</html>