<!DOCTYPE html>
<html lang="ko"
	  xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : CategoryGoodsForm.html
 * @desc    : 카테고리별상품전시관리 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2021.1.19    bin2107     최초 작성
 *******************************************************************************
 -->
<div id="main">
	<!-- 메인타이틀 영역 -->
	<div class="main-title">
	</div>
	<!-- //메인타이틀 영역 -->

	<!-- 메뉴 설명 -->
	<div class="infoBox menu-desc">
	</div>
	<!-- //메뉴 설명 -->

	<!-- 검색조건 영역 -->
	<form id="searchForm" name="searchForm" action="#" th:action="@{'/display/category/goods/list'}" onsubmit="$('#btnSearch').trigger('click'); return false;">
		<input type="hidden" id="cateLeafYn" value="N"/>
		<div class="panelStyle">
			<input type="hidden" id="searchGb" name="searchGb" />
			<table class="frmStyle" aria-describedby="검색조건">
				<colgroup>
					<col width="10%"/>
					<col/>
				</colgroup>
				<tr>
					<th>카테고리</th>
					<td>
						<input type="hidden" name="selLvl"/>
						<input type="hidden" name="cateCd" id="cateCd"/>
						<select name="siteCd">
							<!--<option value="">[사이트]</option> -->
							<option th:if="${siteList}" th:each="oneData, status : ${siteList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
						</select>
						<select name="cateGb" id="selCate1" onchange="fnChangeSearchCondition($(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 name="cate1No" id="selCate2" onchange="fnChangeSearchCondition($(this).val(), 2);">
							<option value="">[카테고리1]</option>
						</select>
						<select name="cate2No" id="selCate3" onchange="fnChangeSearchCondition($(this).val(), 3);">
							<option value="">[카테고리2]</option>
						</select>
						<select name="cate3No" id="selCate4" onchange="fnChangeSearchCondition($(this).val(), 4);">
							<option value="">[카테고리3]</option>
						</select>
						<select name="cate4No" id="selCate5" onchange="fnChangeSearchCondition($(this).val(), 5);">
							<option value="">[카테고리4]</option>
						</select>
					</td>
				</tr>
			</table>
		</div>
	<!-- 검색조건 영역 -->

	<!-- 패널 영역1 -->
	<div class="panelStyle">
		<!-- 상단버튼 영역  -->
		<ul class="panelBar">
			<li>
				<span id="goodsSearch" class="btnLeft" style="display:none">
					<span style="padding:5.5px 15px; background:#d8eafc !important; border-top:1px solid #ddd; border-bottom:0.5px solid #dae0fd; font-weight: bold;">상품코드</span>
					<input type="text" class="w100" name="goodsCd" id="goodsCd" />
					<button type="button" class="btn btn-base btn-lg" id="btnSearch">조회</button>
					<button type="button" class="btn btn-default btn-lg" onclick="cfnDownloadSampleFile('SF003');">엑셀조회 양식 다운로드</button>
					<button type="button" class="btn btn-base btn-lg" id="btnExcelSearch">엑셀조회</button>
				</span>
			</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: 700px;" class="ag-theme-balham lh60"></div>-->
		<div id="gridList" style="width: 100%; height: 570px" class="ag-theme-balham"></div>
		<ul class="panelBar">
			<li class="center">
				<div class="tablePaging" id="categoryGoodsListPagination"></div>
			</li>
		</ul>
		<ul class="panelBar">
			<li class="aR" id="btnGoodsEdit" style="display:none">
				<button type="button" class="btn btn-primary btn-lg leafCateBtn" id="btnAddGoods" style="display:none">상품추가</button>
<!--				<button type="button" class="btn btn-primary btn-lg" id="btnDispChange">순서변경</button>-->
				<!--<button type="button" class="btn btn-primary btn-lg" id="btnSelectTop">선택최상위로</button>-->
				<button type="button" class="btn btn-danger btn-lg" id="btnSelectDel">선택삭제</button>
			</li>
		</ul>
	</div>
	</form>
	<!-- 상품이동/복사 -->
	<div class="panelStyle" id="addCategory" style="display:none">
		<form id="saveForm" name="saveForm" action="#" th:action="@{'/display/goods/category/save'}">
			<input type="hidden" name="addSearchGb"/>
			<input type="hidden" naem="addLeafYn" id="addLeafYn" value="N"/>
			<input type="hidden" name="addSelLvl"/>
			<input type="hidden" name="addCateCd" id="addCateCd"/>
			<ul class="panelBar">
				<li>
					<strong>* 상품 선택 후 하단 leaf 카테고리 선택시 상품이동/ 상품복사 가능합니다.</strong>
				</li>
				<li class="right">
					<button type="button" class="btn btn-primary btn-lg leafCateBtn" id="btnMoveGoods">상품이동</button>
					<button type="button" class="btn btn-primary btn-lg leafCateBtn" id="btnCopyGoods">상품복사</button>
				</li>
			</ul>

			<table class="frmStyle">
				<colgroup>
					<col style="width:20%"/>
					<col style="width:20%"/>
					<col style="width:20%"/>
					<col style="width:20%"/>
					<col style="width:20%"/>
				</colgroup>
				<tr>
					<th>카테고리구분</th>
					<th>카테고리1</th>
					<th>카테고리2</th>
					<th>카테고리3</th>
					<th>카테고리4</th>
				</tr>
				<tr>
					<td>
						<select name="addCateGb" id="cateLvl1" onchange="fnChangeAddCategory($(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>
					</td>
					<td>
						<select name="addCate1No" id="cateLvl2" onchange="fnChangeAddCategory($(this).val(), 2);">
							<option value="">[카테고리1]</option>
						</select>
					</td>
					<td>
						<select name="addCate2No" id="cateLvl3" onchange="fnChangeAddCategory($(this).val(), 3);">
							<option value="">[카테고리2]</option>
						</select>
					</td>
					<td>
						<select name="addCate3No" id="cateLvl4" onchange="fnChangeAddCategory($(this).val(), 4);">
							<option value="">[카테고리3]</option>
						</select>
					</td>
					<td>
						<select name="addCate4No" id="cateLvl5" onchange="fnChangeAddCategory($(this).val(), 5);">
							<option value="">[카테고리4]</option>
						</select>
					</td>
				</tr>
			</table>
		</form>
	</div>
	<!-- 상품이동/복사 -->

</div>
<script type="text/javascript" src="/ux/plugins/gaga/gaga.paging.js?v=2019072202"></script>
<script th:inline="javascript">
	/*<![CDATA[*/
	let siteList = gagajf.convertToArray([[${siteList}]]);
	let cateGbList = gagajf.convertToArray([[${cateGbList}]]);
	let cateTypeList = gagajf.convertToArray([[${cateTypeList}]]);
	let formalGbList = gagajf.convertToArray([[${formalGbList}]]);
	let conentsLocList = gagajf.convertToArray([[${conentsLocList}]]);
	let goodsStatList = gagajf.convertToArray([[${goodsStatList}]]);

	let columnDefs1 = [
		{width: 40, minWidth: 40, cellClass: 'text-center', headerCheckboxSelection: true, checkboxSelection: true, filter: false},
		{headerName: "상품코드", field: "goodsCd", width: 150, cellClass: 'text-center'},
		{headerName: "상품타이틀명", field: "goodsTnm", width: 150, cellClass: 'text-center'},
		{headerName: "상품명", field: "goodsNm", width: 200, cellClass: 'text-center'},
		{headerName: "카테고리번호", field: "cateNo", width: 120, cellClass: 'text-center'},
		{headerName: "표시순서", field: "dispOrd", width: 80, cellClass: 'text-center'},
		{headerName: "상품타입", field: "goodsType", width: 100, cellClass: 'text-center'},
		{headerName: "재고", field: "currStockQty", width: 80, cellClass: 'text-right',
			valueFormatter: function(params) {	return params.value.addComma();}
		},
		{headerName: "정상가", field: "listPrice", width: 80, cellClass: 'text-right',
			valueFormatter: function(params) {	return params.value.addComma();}
		},
		{headerName: "현재판매가", field: "currPrice", width: 80, cellClass: 'text-right',
			valueFormatter: function(params) {	return params.value.addComma();}
		},
		{headerName: "할인율", field: "dcRate", width: 80, cellClass: 'text-center'},
		{headerName: "상품상태", field: "goodsStat", width: 80, cellClass: 'text-center'
			,valueFormatter: function (params) { return gagaAgGrid.lookupValue(goodsStatList, params.value); }
		},
		{headerName: "브랜드코드", field: "brandCd", width: 80, cellClass: 'text-center'},
		{headerName: "브랜드명", field: "brandGrpNm", width: 80, cellClass: 'text-center'},
		{headerName: "공급업체상품코드", field: "supplyGoodsCd", width: 120, cellClass: 'text-center'},
		{headerName: "스타일구분", field: "styleYear", width: 80, cellClass: 'text-center'},
		{headerName: "정상/이월", field: "formalGb", width: 80, cellClass: 'text-center'},
		{headerName: "등록자ID", field: "regNo", width: 80, cellClass: 'text-center'},
		{headerName: "등록자", field: "regNm", width: 80, cellClass: 'text-center'},
		{headerName: "등록일시", field: "regDt", width: 150, cellClass: 'text-center'}
	];

	let gridOptions = gagaAgGrid.getGridOptions(columnDefs1);
	gridOptions.rowSelection = "multiple";

	/**
	 * 엑셀 상품 조회
	 */
	$("#btnExcelSearch").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");
		fnCategoryGoodsListSearch();
	}

	/**
	 * 순서변경 버튼 클릭 시
	 */
	$("#btnDispChange").bind('click', function(){
		//console.log('cateCd>>>'+$('#searchForm input[name=cateCd]').val());
		var params = 'siteCd=G000_10';
		params += '&selLvl='+ $('#searchForm input[name=selLvl]').val();
		params += '&cateGb=' + $('#searchForm select[name=cateGb]').val();
		params += '&cate1No=' + $('#searchForm select[name=cate1No]').val();
		params += '&cate2No=' + $('#searchForm select[name=cate2No]').val();
		params += '&cate3No=' + $('#searchForm select[name=cate3No]').val();
		params += '&cate4No=' + $('#searchForm select[name=cate4No]').val();
		params += '&searchGb=' + $('#searchForm select[name=searchGb]').val();
		params += '&cateCd=' + + $('#searchForm input[name=cateCd]').val();
		params += '&goodsAddYn=Y';

		cfnOpenGoodsDispOrdChangePopup('', params);
	});

	var checkValidation = function (){
		if(gagajf.isNull($("#goodsSearch").find("input[name=goodsCd]").val())){
			mcxDialog.alert("상품코드를 입력하세요.");
			return false;
		}
		return true;
	}

	/**
	 * 상품조회 버튼 클릭시
	 */
	$("#btnSearch").on('click', function(){
		// 기간체크
		if(!checkValidation()){
			return false;
		}

	    $("#searchForm input[name=searchGb]").val("BASIC");
		gagaPaging.load(1);
	});

    //페이징
    $('#searchForm select[name=pageSize]').on('change', function() {
        $("#searchForm input[name=pageNo]").val('1');
        $("#searchForm input[name=searchGb]").val("BASIC");
        gagaPaging.load(1);
    });

	/**
	 * 상품추가 버튼 클릭 시
	 */
	$("#btnAddGoods").bind('click', function(){
		cfnOpenGoodsPopup("fnCategoryGoodsAdd");
	});

	var fnCategoryGoodsAdd = function (result){
		mcxDialog.confirm('상품을 추가하시겠습니까?', {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function () {
				var multiGoods = [];
				$.each(result, function(idx, item) {
					multiGoods.push(item.goodsCd);
				});

				var data = {
					mode : 'add'
					,cateNo : $("#cateCd").val()
					,multiGoods : multiGoods
				}

				var jsonData = JSON.stringify(data);
				gagajf.ajaxJsonSubmit('/display/category/goods/save', jsonData, fnCategoryGoodsListSearch);
				uifnPopupClose('popupGoods');
			}
		});
	}

	/**
	 * 상품삭제 버튼 클릭 시
	 */
	$("#btnSelectDel").bind('click', function(){
		var selectData = gridOptions.api.getSelectedRows();
		if(selectData<1){
			mcxDialog.alert("상품을 선택하세요.");
			return false;
		}

		mcxDialog.confirm('삭제 하시겠습니까?', {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function () {
				var data = {
					mode : 'delete'
					,goodsList : selectData
				};

				var jsonData = JSON.stringify(data);
				gagajf.ajaxJsonSubmit('/display/category/goods/delete', jsonData, fnCategoryGoodsListSearch);
			}
		});
	});

	/**
	 * 상품복사 버튼 클릭 시
	 */
	$("#btnCopyGoods").bind('click', function(){
		var selectData = gridOptions.api.getSelectedRows();
		if(selectData<1){
			mcxDialog.alert("상품을 선택하세요.");
			return false;
		}

		if($("#addLeafYn").val() != 'Y'){
			mcxDialog.alert("leaf카테고리를 선택하세요.");
			return false;
		}

		if($("#cateCd").val() == $("#addCateCd").val()){
			mcxDialog.alert("상품을 복사할 카테고리가 동일합니다.");
			return false;
		}

		mcxDialog.confirm('복사 하시겠습니까?', {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function () {
				var data = {
					mode : 'copy'
					,cateNo : $("#cateCd").val()
					,addCateNo : $("#addCateCd").val()
					,goodsList : selectData
				};

				var jsonData = JSON.stringify(data);
				gagajf.ajaxJsonSubmit('/display/category/goods/move', jsonData, fnCategoryGoodsListSearch);
			}
		});
	});

	/**
	* 상품이동 버튼 클릭 시
	*/
	$("#btnMoveGoods").bind('click', function(){
		var selectData = gridOptions.api.getSelectedRows();
		if(selectData<1){
			mcxDialog.alert("상품을 선택하세요.");
			return false;
		}

		if($("#addLeafYn").val() != 'Y'){
			mcxDialog.alert("leaf카테고리를 선택하세요.");
			return false;
		}

		if($("#cateCd").val() == $("#addCateCd").val()){
			mcxDialog.alert("상품을 이동할 카테고리가 동일합니다.");
			return false;
		}

		mcxDialog.confirm('이동 하시겠습니까?', {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function () {
				var data = {
					mode : 'move'
					,cateNo : $("#cateCd").val()
					,addCateNo : $("#addCateCd").val()
					,goodsList : selectData
				};

				var jsonData = JSON.stringify(data);
				gagajf.ajaxJsonSubmit('/display/category/goods/move', jsonData, fnCategoryGoodsListSearch);
			}
		});

	});

	/**
	 * 상품이동/복사 폼의 카테고리 선택 시
	 */
	var fnChangeAddCategory = function (val, selLvl){
		if(gagajf.isNull(val)){
			if(selLvl > 1){
				selLvl = selLvl - 1;
			}else{
				selLvl = 1;
			}
		}

		$("#saveForm input[name=addSelLvl]").val(selLvl);
		$("#saveForm input[name=addSearchGb]").val("BASIC");

		var data = { siteCd : 'G000_10'
			, selLvl : selLvl
			, cateGb : $('#saveForm select[name=addCateGb]').val()
			, cate1No : $('#saveForm select[name=addCate1No]').val()
			, cate2No : $('#saveForm select[name=addCate2No]').val()
			, cate3No : $('#saveForm select[name=addCate3No]').val()
			, cate4No : $('#saveForm select[name=addCate4No]').val()
			, searchGb : $("#saveForm input[name=addSearchGb]").val()
		};

		var jsonData = JSON.stringify(data);
		gagajf.ajaxJsonSubmit('/display/category/list', jsonData, fnChangeAddCate);
	}

	// 상품이동/복사 폼의 카테고리 선택 후 데이터 뿌려줌
	var fnChangeAddCate = function (data){
		let selLvl = Number($('#saveForm input[name=addSelLvl]').val()) + 1;
		for (let i = 2; i <= 5; i++) {
			if (i >= selLvl) {
				$('#cateLvl' + i).html('<option value="">[카테고리' + (i - 1) + ']</option>');
			}
		}

		let tag = '';
		for (let i = 0; i < data.length; i++) {
			if (data[i].useYn == 'Y') {
				tag += '<option value="' + data[i].cateNo + '">[' + data[i].cateNo + '] ' + data[i].cateNm + '</option>';
			}
		}

		$("#cateLvl" + selLvl).append(tag);

		if(selLvl > 2){
			if(data.length<1){
				$("#addLeafYn").val('Y');
				$("#addCateCd").val($("#cateLvl"+(selLvl-1)).val());
			}else{
				$("#addLeafYn").val('N');
				$("#addCateCd").val('');
			}
			$('#saveForm input[name=addSelLvl]').val(selLvl);
		}
	}

	/**
	 * 검색폼의 카테고리 선택 시
	 */
	var fnChangeSearchCondition = function(val, selLvl) {
		/* if (gagajf.isNull(val)) {
			if (selLvl > 1) {
				selLvl = selLvl - 1;
				if(gagajf.isNull($("#selCate2").val())){
					$("#goodsSearch").hide();
					$("#btnGoodsEdit").hide();
					gagaPaging.load(1);
				}
			} else {
				selLvl = 1;
				$("#goodsSearch").hide();
				$("#btnGoodsEdit").hide();
			}
		}else{
			if (selLvl > 1) {
				$("#goodsSearch").show();
				$("#btnGoodsEdit").show();	// 순서변경,선택최상위로,선택삭제
			}else{
				$("#goodsSearch").hide();
				$("#btnGoodsEdit").hide();
			}
		} */
		$('#searchForm input[name=selLvl]').val(selLvl);
		$("#searchForm input[name=searchGb]").val("BASIC");

		var data = { siteCd : 'G000_10'
			, selLvl : selLvl
			, cateGb : $('#searchForm select[name=cateGb]').val()
			, cate1No : $('#searchForm select[name=cate1No]').val()
			, cate2No : $('#searchForm select[name=cate2No]').val()
			, cate3No : $('#searchForm select[name=cate3No]').val()
			, cate4No : $('#searchForm select[name=cate4No]').val()
			, searchGb : $("#searchForm input[name=searchGb]").val()
		};

		var jsonData = JSON.stringify(data);
		gagajf.ajaxJsonSubmit('/display/category/list', jsonData, fnChangeCate);
	}

	// 조회하고 데이터 뿌려주기
	var fnChangeCate = function (data){
		let selLvl = Number($('#searchForm input[name=selLvl]').val()) + 1;
		for (let i = 2; i <= 5; i++) {
			if (i >= selLvl) {
				$('#selCate' + i).html('<option value="">[카테고리' + (i - 1) + ']</option>');
			}
		}

		let leafYn = "Y";
		let tag = '';
		for (let i = 0; i < data.length; i++) {
			if (data[i].useYn == 'Y') {
				tag += '<option value="' + data[i].cateNo + '">[' + data[i].cateNo + '] ' + data[i].cateNm + '</option>';
				leafYn = "N";
			}
		}
		
		$("#selCate" + selLvl).append(tag);

		// 대카테고리 이상이면 상품 조회해서 그리드
		if(selLvl > 2){
			/* if(data.length<1){
				$("#cateLeafYn").val('Y');
				$("#cateCd").val($("#selCate"+(selLvl-1)).val());
			}else{
				$("#cateLeafYn").val('N');
				$("#cateCd").val('');
			} 
			$('#searchForm input[name=selLvl]').val(selLvl);
			fnCategoryGoodsListSearch();
			*/
			
			if(leafYn == "Y"){
				$("#cateLeafYn").val('Y');
				$("#cateCd").val($("#selCate"+(selLvl-1)).val());
				$('#searchForm input[name=selLvl]').val(selLvl);
				$("#btnGoodsEdit").show();
				$("#addCategory").show();
				$("#goodsSearch").show();
				fnCategoryGoodsListSearch();
			}else{
				$("#cateLeafYn").val('N');
				$("#cateCd").val('');
				$("#btnGoodsEdit").hide();
				$("#addCategory").hide();
				$("#goodsSearch").hide();
				fnGridInit();
				
			}
		}
	}

	// 저장후 콜백
	var fnCategoryGoodsListSearch = function(){
		gagaPaging.init('searchForm', fnSearchCallBack, 'categoryGoodsListPagination', $('#searchForm').find('#pageSize').val());
		gagaPaging.load(1);
		if($("#cateLeafYn").val()=='Y'){
			/* $("#addCategory").show();	// 상품이동/복사 show
			$("#btnAddGoods").show();	// [상품추가] show
			$("#btnDispChange").show();	// [순서변경] show */
			$("#btnGoodsEdit").show();
			$("#addCategory").show();
			$("#goodsSearch").show();
		}else{
			/* $("#addCategory").hide();	// 상품이동/복사 hide
			$("#btnAddGoods").hide();	// [상품추가] hide
			$("#btnDispChange").hide();	// [순서변경] hide */
		}
	}

	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.categoryGoodsList);
		gagaPaging.createPagination(result.pageing.pageable);
	}

	// 신규
	$('#btnNew').on('click', function(){
		//$("#detailForm")[0].reset();
// 		$("#detailForm input[name=selLvl]").val('');
		for (let i = 2; i <= 5; i++) {
			$("#cateLvl" + i).show();
		}
	});

	var fnChangeSearchCondition2 = function(val, selLvl) {
		if (gagajf.isNull(val)) {
			if (selLvl > 1) {
				selLvl = selLvl - 1;
			} else {
				selLvl = 1;
			}
		}
		$('#searchForm input[name=selLvl]').val(selLvl);

		// Fetch data
		gagaAgGrid.fetch($('#searchForm').prop('action'), gridOptions, '#searchForm', function(data) {
			let selLvl = Number($('#searchForm input[name=selLvl]').val()) + 1;

			for (let i = 2; i <= 5; i++) {
				if (i >= selLvl) {
					$('#selCate' + i).html('<option value="">[카테고리' + (i - 1) + ']</option>');
				}
			}

			let tag = '';
			for (let i = 0; i < data.length; i++) {
				if (data[i].useYn == 'Y') {
					tag += '<option value="' + data[i].cateNo + '">[' + data[i].cateNo + '] ' + data[i].cateNm + '</option>';
				}
			}

			$("#selCate" + selLvl).append(tag);
			//$("#btnNew").click();
		});
	}

	// 카테고리유형 변경 시
	$('#detailForm select[name=cateType]').on('change', function() {
		if ($(this).val() == 'G031_10') { // 상품분류카테고리
			$("#detailForm select[name=contentsLoc]").prop('disabled', true);
		} else if ($(this).val() == 'G031_20') { // 컨텐츠카테고리
			$("#detailForm select[name=contentsLoc]").prop('disabled', false);
		}
	});

	// 등록/수정폼의 상위카테고리 선택 시
	var fnChangeUpperCategory = function(val, selLvl) {
		if (gagajf.isNull(val)) {
			if (selLvl > 1) {
				selLvl = selLvl - 1;
			} else {
				selLvl = 1;
			}
		}
		$('#detailForm input[name=selLvl]').val(selLvl);

		gagajf.ajaxFormSubmit($('#searchForm').prop('action'), "#detailForm", function(data) {
			let selLvl = Number($('#detailForm input[name=selLvl]').val()) + 1;

			for (let i = 2; i <= 5; i++) {
				if (i >= selLvl) {
					$('#cateLvl' + i).html('<option value="">[카테고리' + (i - 1) + ']</option>');
				}
			}

			let tag = '';
			for (let i = 0; i < data.length; i++) {
				if (data[i].useYn == 'Y') {
					tag += '<option value="' + data[i].cateNo + '">[' + data[i].cateNo + '] ' + data[i].cateNm + '</option>';
				}
			}

			$("#cateLvl" + selLvl).append(tag);
		});
	}

	// 저장
	$('#btnSave').on('click', function() {
		// 입력 값 체크
		if (!gagajf.validation('#detailForm'))
			return false;

		let selLvl = Number($('#detailForm input[name=selLvl]').val());
		if (gagajf.isNull($('#detailForm select[name=cate1No]').val())) {
			if (selLvl > 2) {
				mcxDialog.alertC('카테고리1을 선택해 주세요.', {
					sureBtnText: "확인",
					sureBtnClick: function() {
						$('#detailForm select[name=cate1No]').focus();
					}
				});
				return;
			}
		}

		if (gagajf.isNull($('#detailForm select[name=cate2No]').val())) {
			if (selLvl > 3) {
				mcxDialog.alertC('카테고리2를 선택해 주세요.', {
					sureBtnText: "확인",
					sureBtnClick: function() {
						$('#detailForm select[name=cate2No]').focus();
					}
				});
				return;
			}
		}

		if (gagajf.isNull($('#detailForm select[name=cate3No]').val())) {
			if (selLvl > 4) {
				mcxDialog.alertC('카테고리3을 선택해 주세요.', {
					sureBtnText: "확인",
					sureBtnClick: function() {
						$('#detailForm select[name=cate3No]').focus();
					}
				});
				return;
			}
		}

		if (gagajf.isNull($('#detailForm select[name=cate4No]').val())) {
			if (selLvl > 5) {
				mcxDialog.alertC('카테고리4를 선택해 주세요.', {
					sureBtnText: "확인",
					sureBtnClick: function() {
						$('#detailForm select[name=cate4No]').focus();
					}
				});
				return;
			}
		}

		mcxDialog.confirm('저장하시겠습니까?', {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function() {
				$("#detailForm select[name=contentsLoc]").prop('disabled', false);

				var jsonData = JSON.stringify($('#detailForm').serializeObject());
				gagajf.ajaxFormSubmit($('#detailForm').prop('action'), '#detailForm', function() {
					$('#btnNew').trigger('click');
				});
			}
		});
	});

	// 카테고리 갱신
	$("#btnRefresh4Srch").on("click", function() {
		mcxDialog.confirm('갱신하시겠습니까?', {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function() {
				gagajf.ajaxJsonSubmit('/display/category/refresh', null);
			}
		});
	});
	
	// 그리드 초기화
	var fnGridInit = function() {
		$("#gridList").html('');
		gagaAgGrid.createGrid('gridList', gridOptions);
		$("#categoryGoodsListPagination").html('');
		$("#gridRowTotalCount").html('0');
		$("#pgNo").html('0');
		$("#endPgNo").html('0');
		$(window).scrollTop(0);
	}

	$(document).ready(function() {
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);

		$("#detailForm select[name=contentsLoc]").prop('disabled', false);
	});
	/*]]>*/
</script>

</html>
