<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : CommoncodeForm.html
 * @desc    : 공통코드관리 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.10.08   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="@{'/system/commoncode/list'}" onsubmit="$('#btnSearch').trigger('click'); return false;">
				<input type="hidden" name="cdGb" value="ROOT"/>
				<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">
			<!-- 다중 TABLE 배치 -->
			<ul class="division">
				<li style="width: 20%">
					<div class="panelBar">
						<h4>코드구분</h4>
					</div>
					<div id="gridList1" style="width: 100%; height: 570px" class="ag-theme-balham"></div>
				</li>
				<li style="width: 50%">
					<div class="panelBar">
						<h4>상세코드</h4>
					</div>
					<div id="gridList2" style="width: 100%; height: 570px" class="ag-theme-balham"></div>
				</li>
				<li style="width: 30%">
					<div class="panelBar">
						<h4>코드정보</h4>
					</div>
					<form id="detailForm" name="detailForm" action="#" th:action="@{'/system/commoncode/save'}">
						<table class="frmStyle" aria-describedby="등록/상세">
							<colgroup>
								<col style="width:30%;"/>
								<col/>
							</colgroup>
							<tr>
								<th>코드구분<i class="required" title="필수" aria-hidden="true"></i></th>
								<td>
									<input type="text" name="cdGb" class="w200" placeholder="" maxlength="4" required="required" data-valid-type="alphaNumeric" data-valid-name="코드구분" onkeyup="$(this).val($(this).val().toUpperCase());"/>
								</td>
							</tr>
							<tr>
								<th>코드<i class="required" title="필수" aria-hidden="true"></i></th>
								<td>
									<input type="text" name="cd" class="w200" placeholder="" maxlength="20" required="required" data-valid-name="코드"/>
								</td>
							</tr>
							<tr>
								<th>코드명<i class="required" title="필수" aria-hidden="true"></i></th>
								<td>
									<input type="text" name="cdNm" class="w200" placeholder="" maxlength="50" required="required" data-valid-name="코드명"/>
								</td>
							</tr>
							<tr>
								<th>코드설명</th>
								<td>
									<textarea class="textareaR4" name="cdDesc"></textarea>
								</td>
							</tr>
							<tr>
								<th>표시순서<i class="required" title="필수" aria-hidden="true"></i></th>
								<td>
									<input type="text" class="w100 text-right" name="dispOrd" placeholder="" maxlength="5" required="required" data-valid-type="numeric" data-valid-name="표시순서" />
								</td>
							</tr>
							<tr>
								<th>사용여부<i class="required" title="필수" aria-hidden="true"></i></th>
								<td>
									<input type="hidden" name="useYn"/>
									<label class="chkBox"><input type="checkbox" name="chkUseYn" value="Y"/>사용</label>
								</td>
							</tr>
						</table>
					</form>
					
					<!-- 버튼 배치 영역 -->
					<ul class="panelBar">
						<li class="right">
							<button type="button" class="btn btn-info btn-lg" id="btnNew">신규</button>
							<input type="button" value="저장" class="btn btn-base btn-lg" id="btnSave"/>
						</li>
					</ul>
					<!-- //버튼 배치 영역 -->
				</li>
			</ul>
			<!--//다중 TABLE 배치 -->
		</div>
	</div>

<script th:inline="javascript">
/*<![CDATA[*/
	// specify the columns
	let columnDefs1 = [
// 		{width: 40, minWidth: 40, cellClass: 'text-center', headerCheckboxSelection: true, checkboxSelection: true, filter: false},
// 		{headerName: "CRUD", field: "crud", width: 75, minWidth: 75, hide: true},
		{
			headerName: "코드구분", field: "cd", width: 80, cellClass: 'text-center',
			cellRenderer: function(params) {
				return '<a href="javascript:void(0);">' + params.value + '</button>';
			}
		},
		{headerName: "코드구분명", field: "cdNm", width: 150},
// 		{headerName: "코드설명", field: "cdDesc", width: 300},
// 		{headerName: "표시순서", field: "dispOrd", width: 80, cellClass: 'text-right'},
// 		{headerName: "사용여부", field: "useYn", width: 80, cellClass: 'text-center'}
	];
	
	let columnDefs2 = [
// 		{width: 40, minWidth: 40, cellClass: 'text-center', headerCheckboxSelection: true, checkboxSelection: true, filter: false},
// 		{headerName: "CRUD", field: "crud", width: 75, minWidth: 75, hide: true},
		{headerName: "코드구분", field: "cdGb", width: 80, cellClass: 'text-center'},
		{
			headerName: "코드", field: "cd", width: 80, cellClass: 'text-center',
			cellRenderer: function(params) {
				return '<a href="javascript:void(0);">' + params.value + '</button>';
			}
		},
		{headerName: "코드명", field: "cdNm", width: 150},
		{headerName: "코드설명", field: "cdDesc", width: 250},
		{headerName: "표시순서", field: "dispOrd", width: 80, cellClass: 'text-right'},
		{headerName: "사용여부", field: "useYn", width: 80, cellClass: 'text-center'}
	];
	
	// Get GridOptions
	let gridOptions1 = gagaAgGrid.getGridOptions(columnDefs1);
	let gridOptions2 = gagaAgGrid.getGridOptions(columnDefs2);
	
	// 중복 선택 가능
// 	gridOptions1.rowSelection = 'multiple';
// 	gridOptions2.rowSelection = 'multiple';

	// Cell Click
	gridOptions1.onCellClicked = function(event) {
		if (event.colDef.field != 'cd')
			return;

		fnBindDetail(event.data, 1);
	}
	
	gridOptions2.onCellClicked = function(event) {
		if (event.colDef.field != 'cd')
			return;

		fnBindDetail(event.data, 2);
	}
	
	// 목록 > row 클릭 시
	let lvl = 1;
	let fnBindDetail = function(rowData, flag) {
		lvl = flag;
		
		$('#detailForm input[name=cdGb]').val(rowData.cdGb);
		$('#detailForm input[name=cd]').val(rowData.cd);
		$('#detailForm input[name=cdNm]').val(rowData.cdNm);
		$('#detailForm input[name=cdDesc]').val(rowData.cdDesc);
		$('#detailForm input[name=dispOrd]').val(rowData.dispOrd);
		
		$("#detailForm input[name=useYn]").val(rowData.useYn);
		if (rowData.useYn == 'Y') {
			$("#detailForm input:checkbox[name=chkUseYn]").prop('checked', true);
			$("#detailForm input:checkbox[name=chkUseYn]").parent().addClass('checked');
		} else {
			$("#detailForm input:checkbox[name=chkUseYn]").prop('checked', false);
			$("#detailForm input:checkbox[name=chkUseYn]").parent().removeClass('checked');
		}
		
		if (flag == 1) {
			fnGetDetailCode(rowData.cd);
		}
	}
	
	// 코드구분 조회
	$('#btnSearch').on('click', function() {
		let actionUrl = $('#searchForm').prop('action') + '?' + $('#searchForm').serialize();
		
		// Fetch data
		gagaAgGrid.fetch(actionUrl, gridOptions1);
	});
	
	// 상세코드 조회
	let gCd = '';
	let fnGetDetailCode = function(cd) {
		gCd = cd;
		let actionUrl = $('#searchForm').prop('action') + '?cdGb=' + cd;
		gagaAgGrid.fetch(actionUrl, gridOptions2);
	}
	
	// 신규
	$('#btnNew').on('click', function() {
		$('#detailForm')[0].reset();
		$('#detailForm input:checkbox[name=chkUseYn]').attr('checked', true);
		$('#detailForm input:checkbox[name=chkUseYn]').attr('disabled', true);
		$('#detailForm input[name=cdGb]').focus();
	});
	
	// 저장
	$('#btnSave').on('click', function() {
		// 입력 값 체크
		if (!gagajf.validation('#detailForm'))
			return false;
		
		$('#detailForm input[name=useYn]').val($('#detailForm input:checkbox[name=chkUseYn]').is(":checked") ? 'Y' : 'N');
		
		gagajf.ajaxFormSubmit('/system/commoncode/save', '#detailForm', function(result) {
			if (lvl == 1) {
				$('#btnSearch').trigger('click');
			} else {
				fnGetDetailCode(gCd);
			}
		});
	});
	
	$(document).ready(function() {
		// Create a agGrid
		gagaAgGrid.createGrid('gridList1', gridOptions1);
		gagaAgGrid.createGrid('gridList2', gridOptions2);
	});
/*]]>*/
</script>

</html>
