<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : StockSyncForm.html
 * @desc    : 출고처재고연계관리 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.11.07   gagamel     최초 작성
 *******************************************************************************
 -->
	<div id="main">
		<!-- 메인타이틀 영역 -->
		<div class="main-title">
		</div>
		<!-- //메인타이틀 영역 -->
		
		<!-- 메뉴 설명 -->
		<div class="infoBox menu-desc">
		</div>
		<!-- //메뉴 설명 -->
		
		<!-- 검색조건 영역 -->
		<div class="panelStyle">
			<!-- Search -->
			<form id="searchForm" name="searchForm" action="#" th:action="@{'/business/stock/sync/delvloc/list'}" onsubmit="$('#btnSearch').trigger('click'); return false;">
				<table class="frmStyle" aria-describedby="검색조건">
					<colgroup>
						<col style="width:10%;"/>
						<col style="width:20%;"/>
						<col style="width:10%;"/>
						<col style="width:20%;"/>
						<col style="width:10%;"/>
						<col/>
					</colgroup>
					<tr>
						<th>공급업체</th>
						<td>
							<select name="supplyCompCd">
								<option th:if="${supplyCompList}" th:each="oneData, status : ${supplyCompList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
						<th>출고처분류</th>
						<td>
							<select name="delvLocClsf">
								<option value="">[전체]</option>
								<option th:if="${delvLocClsfList}" th:each="oneData, status : ${delvLocClsfList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
						<th>검색어</th>
						<td>
							<select name="searchGb">
								<option value="CODE">출고처코드</option>
								<option value="NAME">출고처명</option>
							</select>
							<input type="text" name="searchTxt" class="w200" maxlength="20"/>
						</td>
					</tr>
				</table>
				
				<ul class="panelBar">
					<li class="center">
						<button type="button" class="btn btn-base btn-lg" id="btnSearch">조회</button>
						<button type="button" class="btn btn-gray btn-lg" onclick="$('#searchForm')[0].reset();">초기화</button>
					</li>
				</ul>
			</form>
		</div>
		<!-- //검색조건 영역 -->
		
		<div class="panelStyle">
			<!-- 다중 TABLE 배치 -->
			<ul class="division">
				<li style="width: 30%">
					<div class="panelBar">
						<h4>출고처</h4>
					</div>
					<div id="gridList1" style="width: 100%; height: 570px" class="ag-theme-balham"></div>
				</li>
				<li>
					<ul class="panelBar">
						<li>
							<h4 id="grid2Title">재고연계</h4>
						</li>
						<li class="right">
							<button type="button" class="btn btn-success btn-lg" id="btnSave">저장</button>
						</li>
					</ul>
					<div id="gridList2" style="width: 100%; height: 570px" class="ag-theme-balham"></div>
				</li>
			</ul>
			<!--//다중 TABLE 배치 -->
		</div>
	</div>

<script th:inline="javascript">
/*<![CDATA[*/
	let delvLocClsfList = gagajf.convertToArray([[${delvLocClsfList}]]);
	let formalGbList = gagajf.convertToArray([[${formalGbList}]]);
	
	// specify the columns
	let columnDefs1 = [
		{headerName: "공급업체코드", field: "supplyCompCd", width: 100, cellClass: 'text-center', hide: true},
		{
			headerName: "출고처코드", field: "delvLocCd", width: 100, cellClass: 'text-center',
			cellRenderer: function(params) {
				return '<a href="javascript:void(0);">' + params.value + '</a>';
			}
		},
		{headerName: "출고처명", field: "delvLocNm", width: 150, cellClass: 'text-center'},
		{
			headerName: "출고처분류", field: "delvLocClsf", width: 120, cellClass: 'text-center',
			valueGetter: function (params) {
				return gagaAgGrid.lookupValue(delvLocClsfList, params.data.delvLocClsf);
			}
		}
	];
	
	let columnDefs2 = [
		{headerName: "CRUD", field: "crud", width: 75, hide: true},
		{headerName: "출고처코드", field: "delvLocCd", hide: true},
		{headerName: "브랜드코드", field: "brandCd", width: 100, cellClass: 'text-center'},
		{headerName: "브랜드국문명", field: "brandKnm", width: 150, cellClass: 'text-center' },
		{headerName: "브랜드영문명", field: "brandEnm", width: 150, cellClass: 'text-center' },
		{headerName: "ERP브랜드", field: "erpBrandCd", width: 100, cellClass: 'text-center'},
		{
			headerName: "정상이월구분", field: "formalGb", width: 110, cellClass: 'text-center', hide: true,
			valueGetter: function (params) {
				return gagaAgGrid.lookupValue(formalGbList, params.data.formalGb);
			}
		},
		{headerName: "이전재고연계", field: "prevStockSyncYn", width: 120, cellClass: 'text-center', hide: true},
		{
			headerName: "재고연계", field: "stockSyncYn", width: 120, cellClass: 'text-center',
			headerComponent: 'ComboboxHeaderComponent', cellRenderer: 'ComboboxCellRenderer', filter: false, editable: false
		},
		{headerName: "이전출고지정", field: "prevDelvSyncYn", width: 120, cellClass: 'text-center', hide: true},
		{
			headerName: "출고지정", field: "delvSyncYn", width: 120, cellClass: 'text-center',
			headerComponent: 'ComboboxHeaderComponent', cellRenderer: 'ComboboxCellRenderer', filter: false, editable: false
		}
	];
	
	// Get GridOptions
	let gridOptions1 = gagaAgGrid.getGridOptions(columnDefs1);
	let gridOptions2 = gagaAgGrid.getGridOptions(columnDefs2);
	
	// 중복 선택 가능
	gridOptions2.rowSelection = 'multiple';
	
	// Cell Click
	let delvData = '';
	gridOptions1.onCellClicked = function(event) {
		if (event.colDef.field != 'delvLocCd')
			return;

		delvData = event.data;
		fnGetStockSyncBaseList();
	}
	
	// 출고처 조회
	$('#btnSearch').on('click', function() {
		gagaAgGrid.fetch($('#searchForm').prop('action'), gridOptions1, '#searchForm');
	});
	
	var fnGetStockSyncBaseList = function() {
		$('#grid2Title').html('[' + delvData.delvLocNm + '] 재고연계');
		let actionUrl = "/business/stock/sync/base/list/" + delvData.supplyCompCd + '/' + delvData.delvLocCd;
		gagaAgGrid.fetch(actionUrl, gridOptions2);
	}
	
	// 저장
	$('#btnSave').on('click', function() {
		// 전체 데이터
		var allData = gagaAgGrid.getAllRowData(gridOptions2);
		
		var changedData = [];
		allData.forEach(function(item, index) {
			if (item.prevStockSyncYn != item.stockSyncYn || item.prevDelvSyncYn != item.delvSyncYn) {
				changedData.push(item);
			}
		});
		
		if (changedData.length == 0) {
			mcxDialog.alert('변경된 데이터가 없습니다.');
			return;
		}
		
		mcxDialog.confirm("저장하시겠습니까?", {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function() {
				var jsonData = JSON.stringify(changedData);
				gagajf.ajaxJsonSubmit('/business/stock/sync/base/save', jsonData, function() {
					fnGetStockSyncBaseList();
				});
			}
		});
	});
	
	$(document).ready(function() {
		// Create a agGrid
		gagaAgGrid.createGrid('gridList1', gridOptions1);
		gagaAgGrid.createGrid('gridList2', gridOptions2);
	});
/*]]>*/
</script>

</html>
