<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : SellStoreForm.html
 * @desc    : 판매매장관리 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.10.20   gagamel     최초 작성
 *******************************************************************************
 -->
	<div id="main">
		<!-- 메인타이틀 영역 -->
		<div class="main-title">
		</div>
		<!-- //메인타이틀 영역 -->
		
		<!-- 메뉴 설명 -->
		<div class="infoBox menu-desc">
		</div>
		<!-- //메뉴 설명 -->
		
		<!-- 검색조건 영역 -->
		<div class="panelStyle">
			<form id="searchForm" name="searchForm" action="#" th:action="@{'/business/sellstore/list'}" onsubmit="$('#btnSearch').trigger('click'); return false;">
				<table class="frmStyle" aria-describedby="검색조건">
					<colgroup>
						<col style="width:10%;"/>
						<col style="width:30%;"/>
						<col style="width:10%;"/>
						<col/>
					</colgroup>
					<tr>
						<th>판매매장</th>
						<td>
							<input type="text" class="w200" name="sellStoreNm" placeholder="판매매장명" required="required" data-valid-name="판매매장"/>
						</td>
						<th>사용여부</th>
						<td>
							<select name="useYn">
								<option value="Y">Yes</option>
								<option value="N">No</option>
							</select>
						</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">
			<!-- 버튼 배치 영역 -->
			<ul class="panelBar">
				<li class="left">
					<button type="button" class="btn btn-warning btn-lg" id="btnAddRow">행추가</button>
					<button type="button" class="btn btn-danger btn-lg" id="btnDeleteRow">행삭제</button>
				</li>
				<li class="right">
					<button type="button" class="btn btn-default btn-lg" id="btnExcel">엑셀다운로드</button>
				</li>
			</ul>
			<!-- //버튼 배치 영역 -->
			
			<div id="gridList" style="width: 100%; height: 570px" class="ag-theme-balham"></div>
			
			<!-- 버튼 배치 영역 -->
			<ul class="panelBar">
				<li class="right">
					<button type="button" class="btn btn-success btn-lg" id="btnSave">저장</button>
				</li>
			</ul>
			<!-- //버튼 배치 영역 -->
		</div>
		<!-- //리스트 영역 -->
	</div>

<script th:inline="javascript">
/*<![CDATA[*/
	let supplyCompList = gagajf.convertToArray([[${supplyCompList}]]);
	let useYnList = { "Y":"Yes", "N":"No" };

	let columnDefs = [
		{
			headerName: "공급업체", field: "supplyCompCd", width: 150, cellClass: 'text-center',
			cellEditor: 'agRichSelectCellEditor',
			cellEditorParams: { values: gagaAgGrid.extractValues(supplyCompList) },
			valueFormatter: function (params) { return gagaAgGrid.lookupValue(supplyCompList, params.value); },
			valueParser: function (params) { return gagaAgGrid.lookupKey(supplyCompList, params.newValue); }
		},
		{
			headerName: "판매매장코드", field: "sellStoreCd", width: 150, cellClass: 'text-center',
			cellEditor: 'textCellEditor',
			cellEditorParams: { maxlength: 20, required: true }
		},
		{
			headerName: "판매매장명", field: "sellStoreNm", width: 300, cellClass: 'text-center',
			cellEditor: 'textCellEditor',
			cellEditorParams: { maxlength: 50, required: true }
		},
		{
			headerName: "사용여부", field: "useYn", width: 150, cellClass: 'text-center',
			cellEditor: 'agRichSelectCellEditor',
			cellEditorParams: { values: gagaAgGrid.extractValues(useYnList) },
			valueFormatter: function (params) { return gagaAgGrid.lookupValue(useYnList, params.value); },
			valueParser: function (params) { return gagaAgGrid.lookupKey(useYnList, params.newValue); }
		}
	];

	let gridOptions = gagaAgGrid.getGridOptions(columnDefs);

	// Grid editable
	gridOptions.defaultColDef.editable = true;
	
	// 검색
	$('#btnSearch').on('click', function() {
		gagaAgGrid.fetch($('#searchForm').prop('action'), gridOptions, '#searchForm');
	});

	// 행추가
	$('#btnAddRow').on('click', function() {
		var data = { supplyCompCd: null, sellStoreCd: null, sellStoreNm: null, useYn: "Y" };
		gagaAgGrid.addRowData(gridOptions, data, "sellStoreCd");
	});
	
	// 행삭제
	$('#btnDeleteRow').on('click', function() {
		var removedData = gagaAgGrid.removeRowData(gridOptions);
		
		if (removedData.length == 0) {
			mcxDialog.alert('선택된 행이 없습니다.');
			return;
		}
		
		mcxDialog.confirm("삭제하시겠습니까?", {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function() {
				// delete 대신 update 처리해야 하므로 다음과 같이 useYn 값을 변환
				var updatedData = [];
				
				$.each(removedData, function(idx, item) {
					item.useYn = 'N';
					updatedData.push(item);
				});
				
				
				var jsonData = JSON.stringify(updatedData);
				gagajf.ajaxJsonSubmit('/business/sellstore/list/save', jsonData, function() {
					$('#btnSearch').trigger('click');
				});
			}
		});
	});
	
	// 저장
	$("#btnSave").on("click", function() {
		var changeData = gagaAgGrid.getChangedData(gridOptions);

		if (changeData.length < 1) {
			mcxDialog.alert('변경된 데이터가 없습니다.');
			return;
		}

		// Validation
		if (!gagaAgGrid.validation(gridOptions, changeData))
			return;

		mcxDialog.confirm('저장하시겠습니까?', {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function(){
				var jsonData = JSON.stringify(changeData);
				gagajf.ajaxJsonSubmit('/business/sellstore/list/save', jsonData, function() {
					$('#btnSearch').trigger('click');
				});
			}
		});
	});
	
	// 엑셀다운로드
	$('#btnExcel').on('click', function() {
		gagaAgGrid.exportToExcel('판매매장 목록', gridOptions);
	});
	
	$(document).ready(function() {
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
	});
/*]]>*/
</script>

</html>
