<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : BatchForm.html
 * @desc    : 배치관리 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.12.02   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/batch/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="bizGb" class="w150">
								<option value="">[전체]</option>
								<option th:if="${bizGbList}" th:each="oneData, status : ${bizGbList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
							</select>
						</td>
						<th>배치주기</th>
						<td>
							<select name="batchFrq">
								<option value="">[전체]</option>
								<option value="H">[H] 시간배치</option>
								<option value="D">[D] 일배치</option>
								<option value="M">[M] 월배치</option>
								<option value="Y">[Y] 연배치</option>
							</select>
						</td>
						<th>검색어</th>
						<td>
							<select name="searchGb">
								<option value="">[선택]</option>
								<option value="batchId">배치ID</option>
								<option value="batchNm">배치명</option>
							</select>
							<input type="text" class="w300" name="searchTxt" placeholder="" maxlength="50"/>
						</td>
					</tr>
					<tr>
						<th>수행기간</th>
						<td colspan="5" id="terms">
						</td>
					</tr>
				</table>
				
				<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: 45%">
					<div class="panelBar">
						<h4>배치 목록</h4>
					</div>
					<div id="gridList1" style="width: 100%; height: 300px" class="ag-theme-balham"></div>
					
					<form id="detailForm" name="detailForm" action="#" th:action="@{'/system/batch/save'}">
						<input type="hidden" name="mode" value="N"/>
						
						<table class="frmStyle" aria-describedby="등록/수정 폼">
							<colgroup>
								<col style="width:20%;"/>
								<col style="width:30%;"/>
								<col style="width:20%;"/>
								<col/>
							</colgroup>
							<tr>
								<th>배치ID<em class="required" title="필수"></em></th>
								<td colspan="3">
									<input type="text" name="batchId" placeholder="" required="required" maxlength="50" data-valid-name="배치ID"/>
								</td>
							</tr>
							<tr>
								<th>배치명<em class="required" title="필수"></em></th>
								<td colspan="3">
									<input type="text" name="batchNm" placeholder="" maxlength="100" required="required" data-valid-name="배치명"/>
								</td>
							</tr>
							<tr>
								<th>업무구분<em class="required" title="필수"></em></th>
								<td>
									<select name="bizGb" class="w150" required="required" data-valid-name="업무구분">
										<option value="">[선택]</option>
										<option th:if="${bizGbList}" th:each="oneData, status : ${bizGbList}" th:value="${oneData.cd}" th:text="${'[' + oneData.cd + '] ' + oneData.cdNm}"></option>
									</select>
								</td>
								<th>배치주기<em class="required" title="필수"></em></th>
								<td>
									<select name="batchFrq" required="required" data-valid-name="배치주기">
										<option value="">[선택]</option>
										<option value="H">[H] 시간배치</option>
										<option value="D">[D] 일배치</option>
										<option value="M">[M] 월배치</option>
										<option value="Y">[Y] 연배치</option>
									</select>
								</td>
							</tr>
							<tr>
								<th>배치설명</th>
								<td colspan="3">
									<textarea class="textareaR2" name="batchDesc"></textarea>
								</td>
							</tr>
							<tr>
								<th>사용여부<em class="required" title="필수"></em></th>
								<td colspan="3">
									<input type="hidden" name="useYn"/>
									<label class="chkBox checked"><input type="checkbox" name="chkUseYn" value="Y" checked="checked" readonly="readonly"/>사용</label>
								</td>
							</tr>
						</table>
		
						<ul class="panelBar">
							<li class="right">
								<button type="button" class="btn btn-info btn-lg" id="btnNew">신규</button>
								<button type="button" class="btn btn-success btn-lg" id="btnSave">저장</button>
							</li>
						</ul>
					</form>
				</li>
				<li style="width: 55%">
					<div class="panelBar">
						<h4>배치 로그</h4>
					</div>
					<div id="gridList2" style="width: 100%; height: 570px" class="ag-theme-balham"></div>
				</li>
			</ul>
		</div>
	</div>

<script th:inline="javascript">
/*<![CDATA[*/
	let bizGbList = gagajf.convertToArray([[${bizGbList}]]);
	
	// specify the columns
	let columnDefs1 = [
		{
			headerName: "배치ID", field: "batchId", width: 200,
			cellRenderer: function(params) {
				return '<a href="javascript:void(0);">' + params.value + '</a>';
			}
		},
		{headerName: "배치명", field: "batchNm", width: 200},
		{
			headerName: "업무구분", field: "bizGb", width: 80, cellClass: 'text-center',
			cellRenderer: function (params) { return gagaAgGrid.lookupValue(bizGbList, params.value); }
		},
		{
			headerName: "배치주기", field: "batchFrq", width: 80, cellClass: 'text-center',
			cellRenderer: function(params) {
				let batchFrqNm = '시간배치';
				if (params.value == 'D') batchFrqNm = '일배치';
				else if (params.value == 'M') batchFrqNm = '월배치';
				else if (params.value == 'Y') batchFrqNm = '연배치';
				return batchFrqNm;
			}
		},
		{headerName: "배치설명", field: "batchDesc", width: 400, hide: true},
		{headerName: "사용여부", field: "useYn", width: 80, cellClass: 'text-center'}
	];
	
	let columnDefs2 = [
// 		{headerName: "배치ID", field: "batchId", width: 200},
		{headerName: "배치명", field: "batchNm", width: 200},
// 		{
// 			headerName: "업무구분", field: "bizGb", width: 100, cellClass: 'text-center',
// 			cellRenderer: function (params) { return gagaAgGrid.lookupValue(bizGbList, params.value); }
// 		},
		{
			headerName: "배치주기", field: "batchFrq", width: 80, cellClass: 'text-center',
			cellRenderer: function(params) {
				let batchFrqNm = '시간배치';
				if (params.value == 'D') batchFrqNm = '일배치';
				else if (params.value == 'M') batchFrqNm = '월배치';
				else if (params.value == 'Y') batchFrqNm = '연배치';
				return batchFrqNm;
			}
		},
		{
			headerName: "상태", field: "batchStat", width: 80, cellClass: 'text-center',
			cellRenderer: function(params) {
				return params.value == 'I' ? '실행중' : '실행완료';
			}
		},
		{headerName: "시작일시", field: "batchStdt", width: 180, cellClass: 'text-center'},
		{headerName: "종료일시", field: "batchEddt", width: 180, cellClass: 'text-center'},
		{
			headerName: "수행시간(초)", field: "runTime", width: 100, cellClass: 'text-center',
			cellRenderer: function(params) { return gagaAgGrid.toAddComma(params.value); }
		}
	];

	// Get GridOptions
	let gridOptions1 = gagaAgGrid.getGridOptions(columnDefs1);
	let gridOptions2 = gagaAgGrid.getGridOptions(columnDefs2);

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

		$('#detailForm input[name=mode]').val('U');
		$('#detailForm input[name=batchId]').val(event.data.batchId);
		$('#detailForm input[name=batchId]').attr('readonly', true);
		$('#detailForm input[name=batchNm]').val(event.data.batchNm);
		$('#detailForm select[name=bizGb]').val(event.data.bizGb);
		$('#detailForm select[name=batchFrq]').val(event.data.batchFrq);
		$('#detailForm textarea[name=batchDesc]').val(event.data.batchDesc);

		if (event.data.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');
		}
		
		$('#detailForm input:checkbox[name=chkUseYn]').attr('readonly', false);
	}

	// 조회
	$('#btnSearch').on('click', function() {
		// Fetch data
		gagaAgGrid.fetch($('#searchForm').prop('action'), gridOptions1, '#searchForm');
		gagaAgGrid.fetch('/system/batch/log/list', gridOptions2, '#searchForm');
	});

	// 신규
	$('#btnNew').on('click', function() {
		$('#detailForm')[0].reset();
		$('#detailForm input[name=mode]').val('N');
		$('#detailForm input[name=batchId]').attr('readonly', false);
		$('#detailForm input:checkbox[name=chkUseYn]').prop('checked', true);
		$("#detailForm input:checkbox[name=chkUseYn]").parent().addClass('checked');
		$('#detailForm input:checkbox[name=chkUseYn]').attr('readonly', true);
		$('#detailForm input[name=batchId]').focus();
	});

	// 저장 처리
	$('#btnSave').on('click', function() {
		// 입력 값 체크
		if (!gagajf.validation('#detailForm'))
			return;

		$('#detailForm input[name=useYn]').val($('#detailForm input:checkbox[name=chkUseYn]').is(":checked") ? 'Y' : 'N');

		mcxDialog.confirm("저장하시겠습니까?", {
			cancelBtnText: "취소",
			sureBtnText: "확인",
			sureBtnClick: function() {
				gagajf.ajaxFormSubmit($('#detailForm').prop('action')
						, $('#detailForm')
						, function() {
							$('#btnNew').trigger('click');
							$('#btnSearch').trigger('click');
						}
				);
			}
		});
	});

	$(document).ready(function() {
		cfnCreateCalendar('#terms', 'batchStdt', 'batchEddt');
		$('#batchStdt').val(new Date().format("YYYY-MM-DD"));
		$('#batchEddt').val(new Date().format("YYYY-MM-DD"));
		
		// Create a agGrid
		gagaAgGrid.createGrid('gridList1', gridOptions1);
		gagaAgGrid.createGrid('gridList2', gridOptions2);
	});
/*]]>*/
</script>

</html>
