<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : GoodsItemkindForm.html
 * @desc    : 품목변경관리 목록
 *============================================================================
 * Pastelmall
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.10.30   eskim       최초 작성
 *******************************************************************************
 -->
	<div id="main">
		<!-- 메인타이틀 영역 -->
		<div class="main-title">
		</div>
		<!-- //메인타이틀 영역 -->
		
		<!-- 메뉴 설명 -->
		<div class="infoBox menu-desc">
		</div>
		<form id="searchForm" name="searchForm" action="#" th:action="@{'/goods/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="10%"/>
						<col width="25%"/>
						<col width="10%"/>
						<col width="25%"/>
						<col width="10%"/>
						<col/>
					</colgroup>
					<tr>
						<th>카테고리</th>
						<td colspan="5">
							<input name="siteCd" id="siteCd" type="hidden" value="10"/>
							<input name="selLvl" id="selLvl" type="hidden"/>
							<input name="cateCd" id="cateCd" type="hidden"/>
							<input name="cateType" id="cateType" type="hidden"/>
							<select name="cateGb" id="cateGb" onchange="fnChangeSelect($(this).val(), 1);">
								<option value="">[카테고리구분]</option>
								<option th:if="${cateGbList}" th:each="oneData, status : ${cateGbList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
							<select id="selCate1" name="cate1No" onchange="fnChangeSelect($(this).val(), 2);">
								<option value="">[카테고리1]</option>
							</select>
							<select id="selCate2" name="cate2No" onchange="fnChangeSelect($(this).val(), 3);">
								<option value="">[카테고리2]</option>
							</select>
							<select id="selCate3" name="cate3No" onchange="fnChangeSelect($(this).val(), 4);">
								<option value="">[카테고리3]</option>
							</select>
							<select id="selCate4" name="cate4No" onchange="fnChangeSelect($(this).val(), 5);">
								<option value="">[카테고리4]</option>
							</select>
							<select id="selCate5" name="cate5No" >
								<option value="">[카테고리5]</option>
							</select>
						</td>
					</tr>
					<tr>
						<th>업체/브랜드</th>
						<td>
							<select name="supplyCompCd" id="supplyCompCd">
								<option value="" th:if="${sessionInfo.roleCd} != 'G001_B000'">[전체]</option>
								<option th:if="${supplyCompList}" th:each="oneData, status : ${supplyCompList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
							<select name="brandCd" id="brandCd">
								<option value="">[전체]</option>
							</select>
						</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="w40p" name="condition" id="condition" maxlength="50"/>
						</td>
						<th>품목</th>
						<td>
							<select name="itemkindCd" id="itemkindCdSearch">
								<option value="">[전체]</option>
								<option th:if="${itemkindList}" th:each="oneData, status : ${itemkindList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
					</tr>
					<tr>
						<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>
						<th>정상/이월 구분</th>
						<td>
							<select  name="formalGb" id="formalGb">
								<option value="">[전체]</option>
								<option th:if="${formalGbList}" th:each="oneData, status : ${formalGbList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
						<th>담당MD</th>
						<td>
							<select  name="mdNo" id="mdNo">
								<option value="">[전체]</option>
								<option th:if="${brandMdList}" th:each="oneData, status : ${brandMdList}" th:value="${oneData.cd}" th:text="${oneData.cdNm}"></option>
							</select>
						</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>
					<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>
					<input class="w100" type="text" id="itemKindCd" placeholder="변경할 품목코드" onkeyup="$(this).val($(this).val().replace(/[^a-zA-Z0-9]/gi, '').toUpperCase());" />
					<button type="button" class="btn btn-dark btn-sm" id="btnChangeItemKindCd" >선택 상품 품목코드 변경</button>
					<button type="button" class="btn btn-success btn-lg" id="btnSave">저장</button>
					<strong class="cBlue">* 저장 버튼을 눌러 저장해야 실제로 변경 됩니다.</strong>
				</li>
				<li class="aR">
					검색결과 : <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 lh60"></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=2019072202"></script>
<script th:inline="javascript">
/*<![CDATA[*/
	var goodsImgParam = "?width=60&height=60";
	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 cfnGridNumner('searchForm',params.node.rowIndex, 'A');}
		},
		{headerName: "이미지", field: "sysImgNm", width: 100, height: 60, cellClass: 'text-center'
			,cellRenderer: function(params) {
				return '<img width="60" src="'+ _goodsUrl+ "/" + params.value + '" alt=""  onerror="this.src=\'/image/no.gif\';"/>';
			}
		},
		{headerName: "상품코드", field: "goodsCd", width: 140, cellClass: 'text-center'
			,cellRenderer: function(params) {
				return '<a href="javascript:void(0);">' + params.value + '</a>';
			}
		},
		{headerName: "상품명", field: "goodsNm", width: 200, cellClass: 'text-left'},
		{headerName: "품목명", field: "itemkindNm", width: 250, cellClass: 'text-left'},
		{headerName: "기존품목코드", field: "itemkindCd", width: 150, cellClass: 'text-center' },
		{headerName: "변경품목코드", field: "itemkindCdNew", width: 150, cellClass: 'text-center', editable : true,
			cellEditor: 'textCellEditor',
			cellEditorParams: { maxlength: 5, required: true, onblur: '$(this).val($(this).val().toUpperCase())' }
		}
	];

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

	// 중복 선택 가능
	gridOptions.rowSelection = 'multiple';
	gridOptions.suppressRowClickSelection = true;
	gridOptions.rowHeight = 60; //이미지가 있을경우 높이 지정해야함.

	gridOptions.stopEditingWhenGridLosesFocus = true;

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

	var fnChangeSelect = function(val, selLvl){
		$("#cateCd").val('');
		$('#tcateCd').val('');
		$('#mcateCd').val('');
		$('#scateCd').val('');
		$('#dcateCd').val('');
		if(val==''){
			if(selLvl>1){
				selLvl = selLvl - 1;
				if(selLvl>2){
					$("#cateCd").val($("#selCate"+(selLvl-1)).val());
				}
			}else{
				selLvl = '';
			}
		}else{
			if(selLvl>2){
				$("#cateCd").val(val);
			}
		}
		$("#searchForm input[name=selLvl]").val(selLvl);
		$("#searchForm input[name=searchGb]").val("BASIC");
		gagajf.ajaxFormSubmit('/display/category/list', "#searchForm", fnChangeCategory);
	}

	var fnChangeCategory = function(data){
		var selLvl = $("#searchForm input[name=selLvl]").val();
		var html = '<option value="">[선택]</option>';
		for(var i=1; i<6; i++){
			if(i>selLvl){
				$("#selCate"+i).html(html);
			}
		}
		for(var i=0; i<data.length; i++){
			var cd = '';
			var nm = '';
			if(data[i].useYn=='Y'){
				if(selLvl=='1'){
					cd = data[i].cateGb;
					nm = gagaAgGrid.lookupValue(cateGbList, cd);
				}else{
					cd = data[i].cateCd;
					nm = data[i].cateNm;
					$("#cateType").val(data[i].cateType);
				}
				html += '<option value="'+cd+'">['+cd+'] '+nm+'</option>';
			}
		}
		$("#selCate"+selLvl).html(html);
	}

	// 조회
	$('#btnSearch').on('click', function() {
		if(gagajf.isNull($("#selCate2").val()) && gagajf.isNull($("#supplyCompCd").val()) && gagajf.isNull($("#condition").val()) && gagajf.isNull($("#itemkindCdSearch").val())
			&& gagajf.isNull($("#styleYear").val()) && gagajf.isNull($("#formalGb").val()) && gagajf.isNull($("#mdNo").val())){
			mcxDialog.alert('검색조건을 입력해 주세요.');
			return;
		}
		
		$("#searchForm input[name=searchGb]").val("BASIC");
		fnSearch();
	});

	$("#pageSize").on("change", function(){
		var rowCnt = gridOptions.api.getDisplayedRowCount();
		if(rowCnt > 0){
			fnSearch();
		}
	});

	var fnSearch = function(){
		gagaPaging.init('searchForm', fnSearchCallBack, 'goodsListPagination', $('#searchForm').find('#pageSize').val());
		gagaPaging.load(1);
	};

	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);
	}

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

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

	var fnGoodsExcelUploadCallBack = function(result){
		$("#searchForm input[name=searchGb]").val("EXCEL");
		fnSearch();
	}

	$("#btnInit").on("click", function(){
		$("#searchForm")[0].reset();
	});

	$("#btnChangeItemKindCd").on("click", function(){
		if($("#itemKindCd").val()==''){
			mcxDialog.alert('품목코드를 입력해 주세요.');
			return;
		}
		var selectedData = gagaAgGrid.selectedRowData(gridOptions);
		selectedData.forEach(function(data){
			data.itemkindCdNew = $("#itemKindCd").val();
		});
		gridOptions.api.refreshCells();
	});

	$("#btnSave").on("click", function(){
		var rowData = gagaAgGrid.getAllRowData(gridOptions);
		var dataArr = [];
		rowData.forEach(function(row){
			if(typeof row.itemkindCdNew!='undefined' && row.itemkindCdNew!=''){
				var data = {  goodsCd : row.goodsCd
							, itemkindCd : row.itemkindCdNew
				}
				dataArr.push(data);
			}
		});
		if(dataArr.length==0){
			mcxDialog.alert('변경된 데이터가 없습니다.');
			return;
		}
		mcxDialog.confirm('저장하시겠습니까?',{
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function(){
				var jsonData = JSON.stringify(dataArr);
				console.log(jsonData);
				gagajf.ajaxJsonSubmit('/goods/itemKind/change/save', jsonData, fnSearch);
			}
		});
	});
	
	// 업체변경시
	$('#searchForm select[name=supplyCompCd]').on('change', function() {
		var actionUrl = '/renderer/supplyCompany/brand/list/' + $(this).val();
		if(sessRoleCd == "G001_B000"){
			actionUrl = '/renderer/brand/AuthBrandlist';
		}
		$("#searchForm select[name=brandCd] option:gt(0)").remove();

		cfnCreateCombo(actionUrl, $('#searchForm select[name=brandCd]'), "[전체]", "");
	});

	$(document).ready(function() {

		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
	});

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

</html>