<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : ShipCompanyForm.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/ship/company/list'}" onsubmit="$('#btnSearch').trigger('click'); return false;">
				<ul class="panelBar">
					<li class="center">
						<button type="button" class="btn btn-base btn-lg" id="btnSearch">조회</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-info btn-lg" id="btnSave">저장</button>
					<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>
		</div>
		<!-- //리스트 영역 -->
	</div>

<script th:inline="javascript">
/*<![CDATA[*/
	let useYnList = { "Y":"Yes", "N":"No" };

	let columnDefs = [
		{width: 40, minWidth: 40, cellClass: 'text-center', headerCheckboxSelection: true, checkboxSelection: true, filter: false},
		{headerName: "CRUD", field: "crud", width: 75, hide: true},
		{
			headerName: "배송업체코드", field: "shipCompCd", width: 100, cellClass: 'text-center',
			editable: function(params) { return params.data.crud == 'C' ? true : false; },
			cellEditorParams: { required: true },
		},
		{
			headerName: "배송업체명", field: "shipCompNm", width: 150, cellClass: 'text-center',
			editable: true,
			cellEditorParams: { required: true },
		},
		{headerName: "배송추적URL", field: "trackingUrl", width: 800, editable: true},
		{
			headerName: "사용여부", field: "useYn", width: 80, cellClass: 'text-center',
			editable: true,
			cellEditor: 'agRichSelectCellEditor',
			cellEditorParams: { values: gagaAgGrid.extractValues(useYnList), required: true },
			valueFormatter: function (params) { return gagaAgGrid.lookupValue(useYnList, params.value); },
			valueParser: function (params) { return gagaAgGrid.lookupKey(useYnList, params.newValue); }
		}
	];

	let gridOptions = gagaAgGrid.getGridOptions(columnDefs);

	// 다중 선택
	gridOptions.rowSelection = 'multiple';
	
	// 조회
	$('#btnSearch').on('click', function() {
		let actionUrl = $('#searchForm').prop('action') + '?' + $('#searchForm').serialize();
		
		// Fetch data
		gagaAgGrid.fetch(actionUrl, gridOptions);
	});

	// 행추가
	$('#btnAddRow').on('click', function() {
		var data = { };
		gagaAgGrid.addRowData(gridOptions, data, "shipCompCd");
	});
	
	// 행삭제
	$('#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/ship/company/list/delete', 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/ship/company/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>
