<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : GoodsNaverPriceForm.html
 * @desc    : 네이버최저가 조회
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.12.03   eskim       최초 작성
 *******************************************************************************
 -->
	<div id="main">
		<!-- 메인타이틀 영역 -->
		<div class="main-title">
		</div>
		<!-- //메인타이틀 영역 -->
		
		<!-- 메뉴 설명 -->
		<div class="infoBox menu-desc">
		</div>
		<form id="searchForm" name="searchForm" action="#" th:action="@{'/goods/naver/price/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="40%"/>
						<col width="10%"/>
						<col/>
					</colgroup>
					<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 rowspan="3">키워드</th>
						<td rowspan="3">
							<!-- <select name="search" id="search">
								<option value="searchGoodsCd">상품코드</option>
							</select> -->
							<textarea class="textareaR3 w150" name="condition" id="condition"></textarea>
						</td>
					</tr>
					<tr>
						<th rowspan="2">년도/시즌</th>
						<td rowspan="2">
							<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>
				</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="btnNaverPriceSearch" >네이버 최저가 조회</button>
					</li>
				</ul>
			</div>
			<!-- //검색조건 영역 -->
		</div>
		<!-- 패널 영역1 -->
		<div class="panelStyle">
			<!-- 검색결과 영역 -->
			<!-- 상단버튼 영역  -->
			<ul class="panelBar">
				<li>
					<button type="button" class="btn btn-default btn-lg" onclick="fnGoodsExcelDownLoad('');">엑셀다운로드</button>
				</li>
			</ul>
			<!-- //상단버튼 영역  -->
			<div id="gridList" style="width: 100%; height: 500px;" class="ag-theme-balham"></div>
			
			<label class="off">
				<a href="javascript:void(0);" id="GoodsExcelList" style="display: none;">상품목록 엑셀다운로드</a>
			</label>
		</div>
		</form>
		<!-- //패널 영역2 -->
<script type="text/javascript" src="/ux/plugins/gaga/gaga.paging.js?v=2019072202"></script>
<script th:inline="javascript">
/*<![CDATA[*/
	
	var sessRoleCd = [[${sessionInfo.roleCd}]];
	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: "brandCd", width: 80, cellClass: 'text-center'},
		{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: "listPrice" , width: 120, cellClass: 'text-right'
			,valueFormatter: function(params) {return params.value.addComma();},
			cellEditor: 'textCellEditor',
			cellEditorParams: { maxlength: 14, validType: 'numeric'}
		},
		{headerName: "판매가", field: "currPrice" , width: 120, cellClass: 'text-right'
			,valueFormatter: function(params) {return params.value.addComma();},
			cellEditor: 'textCellEditor',
			cellEditorParams: { maxlength: 14, validType: 'numeric'}
		},
		{headerName: "자사즉시할인가", field: "benefitPrice" , width: 120, cellClass: 'text-right'
			,valueFormatter: function(params) {return params.value.addComma();},
			cellEditor: 'textCellEditor',
			cellEditorParams: { maxlength: 14, validType: 'integer'}
		},
		{headerName: "네이버최저가", field: "lowestPrice" , width: 120, cellClass: 'text-right'
			,valueFormatter: function(params) {return params.value.addComma();},
			cellEditor: 'textCellEditor',
			cellEditorParams: { maxlength: 14, validType: 'numeric'}
		},
		{headerName: "차액", field: "currPrice" , width: 100, cellClass: 'text-right',
			valueFormatter: function(params) {
				var priceVal =  params.data.benefitPrice - params.data.lowestPrice;
				return priceVal.addComma();
			},
			cellStyle : function(params){
				var color = "";
				var priceVal =  params.data.benefitPrice - params.data.lowestPrice;
				if (priceVal > 0){
					//color = '#ff96689c';
					color = "red";
					//background-color
				}
				return { 'color': color};
			}
		},
		{headerName: "최저가몰명", field: "mallNm", width: 140, cellClass: 'text-center'},
		{headerName: "최저가몰링크", field: "mallLink", width: 200, cellClass: 'text-center'
			,cellRenderer: function(params) {
				return '<a href="javascript:void(0);">' + params.value + '</a>';
			}
		},
		{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;

	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);
		}else if (event.colDef.field == "mallLink"){
			window.open(event.data.mallLink, "_blank, scrollbars=yes");
			
		}
	}

	// 초기화 클릭시
	$('#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;
 		
 		var formId = "#searchForm";
 		gagaAgGrid.fetch($(formId).prop('action'), gridOptions, formId);

	}

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

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

		var searchFlag = false;
		var cnt = 0;

		for (i = 0; i < form.elements.length; i++ ) {
			var el = form.elements[i];

			if ($(el).prop("type") == "text"  || $(el).prop("type") == "textarea"  || ($(el).prop("type") == "select-one" && el.name != "search" && el.name != "pageSize")) {
				if (!(el.value == null || el.value == "")) {
					cnt++;
				}
			}
		}
		
		var mulYearSize = $('input:checkbox[name="multiStyleYear"]:checked').length;
		var mulSeasonSize = $('input:checkbox[name="multiSeasonCd"]:checked').length;

		if (mulYearSize > 0 || mulSeasonSize > 0){
			cnt++;
		}

		if(cnt > 0) searchFlag = true;
		
		if(searchFlag == false){
			mcxDialog.alert("검색조건을 입력하세요.");
			return false;
		}

		return true;
	}

	//엑셀다운로드
	var fnGoodsExcelDownLoad = function(flag){
		var formId = '#searchForm';
		if (gridOptions.api.getDisplayedRowCount() <= 0){
			mcxDialog.alert("조회된 데이터가 없습니다.<br/>다시 조회 후 다운로드 받으세요.");
			return;
		}

		var params =  $(formId).serialize();
		$('#GoodsExcelList').attr({ href : '/goods/naver/excel/list?' + params }).get(0).click();
	}
	
	// 업체변경시
	$('#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]'), "[전체]", "");
	});
	
	// 네이버 최저가 조회 클릭시
	$('#btnNaverPriceSearch').on('click', function() {
		if (gagajf.isNull($("#searchForm textarea[name=condition]").val())) {
			mcxDialog.alert("상품코드 입력 후 네이버최저가 조회를 해주세요.");
			return;
		}
		
		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 > 1000){
				mcxDialog.alertC("키워드 조회값을 500 이하로 조회 하세요.", {
					sureBtnText: "확인",
					sureBtnClick: function() {
						$('#searchForm textarea[name=condition]').focus();
					}
				});
				return false;
			}
		}
		
		var formId = "#searchForm";
 		gagaAgGrid.fetch('/goods/naver/lowest/price/save', gridOptions, formId);
		
	});


	$(document).ready(function() {

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

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

</html>