<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : SampleFileForm.html
 * @desc    : 샘플양식관리 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2020.11.03   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/sample/file/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">
			<div id="gridList" style="width: 100%; height: 470px" class="ag-theme-balham"></div>
		</div>
		<!-- //리스트 영역 -->
		
		<!-- 등록/수정 영역 -->
		<div class="panelStyle">
			<form id="detailForm" name="detailForm" action="#" th:action="@{'/system/sample/file/save'}">
				<input type="hidden" name="mode" value="N"/>
				
				<table class="frmStyle" aria-describedby="신규">
					<colgroup>
						<col style="width:10%;"/>
						<col style="width:40%;"/>
						<col style="width:10%;"/>
						<col/>
					</colgroup>
					<tr>
						<th>샘플파일ID<em class="required" title="필수"></em></th>
						<td>
							<input type="text" class="w100" name="sampleFileId" placeholder="" value="자동생성" readonly="readonly"/>
						</td>
						<th>샘플파일명<em class="required" title="필수"></em></th>
						<td>
							<input type="text" name="sampleFileNm" placeholder="" required="required" data-valid-name="샘플파일명"/>
						</td>
					</tr>
					<tr>
						<th>샘플파일<em class="required" title="필수"></em></th>
						<td colspan="3">
							<div class="uFile w600">
								<input type="file" id="file" name="file" class="uFileInput"/>
								<label for="file" class="uFileLabel">파일 선택</label>
								<input type="hidden" name="orgFileNm"/>
								<input type="hidden" name="sysFileNm"/>
							</div>
							<span style="margin-left: 10px; display: none;" id="sampleFileList">
								<a href="">SMS수신_미동의_Sample.xlsx</a>
							</span>
						</td>
					</tr>
					<tr>
						<th>사용여부<em class="required" title="필수"></em></th>
						<td colspan="3">
							<input type="hidden" name="useYn"/>
							<label class="chkBox"><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>
		</div>
		<!-- //등록/수정 영역 -->
	</div>

<script th:inline="javascript">
/*<![CDATA[*/
	// specify the columns
	var columnDefs = [
		{headerName: "샘플파일ID", field: "sampleFileId", width: 120, cellClass: 'text-center'},
		{
			headerName: "샘플파일명", field: "sampleFileNm", width: 300,
			cellRenderer: function(params) {
				return '<a href="javascript:void(0);">' + params.value + '</a>';
			}
		},
		{headerName: "시스템파일명", field: "sysFileNm", width: 400},
		{headerName: "사용여부", field: "useYn", width: 100, cellClass: 'text-center'}
	];

	// Get GridOptions
	var gridOptions = gagaAgGrid.getGridOptions(columnDefs);

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

		$('#detailForm input[name=mode]').val('U');
		$('#detailForm input[name=sampleFileId]').val(event.data.sampleFileId);
		$('#detailForm input[name=sampleFileNm]').val(event.data.sampleFileNm);
		$('#detailForm input[name=orgFileNm]').val(event.data.orgFileNm);
		$('#detailForm input[name=sysFileNm]').val(event.data.sysFileNm);
		$('#sampleFileList').html('<a href="javascript:void(0);" onclick="cfnDownloadSampleFile(\'' + event.data.sampleFileId + '\');">' + event.data.sysFileNm + '</a>');
		$('#sampleFileList').show();

		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() {
		let actionUrl = $('#searchForm').prop('action') + '?' + $('#searchForm').serialize();
		
		// Fetch data
		gagaAgGrid.fetch(actionUrl, gridOptions);
	});

	// 신규
	$('#btnNew').on('click', function() {
		$('#detailForm')[0].reset();
		$('#detailForm input[name=mode]').val('N');
		$('#detailForm input[name=sampleFileId]').val('자동생성');
		$('#detailForm input[name=file]').val('');
		$('#detailForm input:checkbox[name=chkUseYn]').prop('checked', true);
		$('#detailForm input:checkbox[name=chkUseYn]').attr('readonly', true);
		$('#sampleFileList').html('');
		$('#sampleFileList').hide();
	});

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

		if (gagajf.isNull($('#detailForm input[name=sysFileNm]').val())) {
			mcxDialog.alert('샘플파일을 선택해 주세요.');
			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');
						}
				);
			}
		});
	});

	// 파일첨부 선택 시
	$('#detailForm input[name=file]').on('change', function() {
		// multiple 속성이 있으면 files에는 다수의 객체가 할당됨
		var file = this.files[0];

		// 파일 업로드
		gagajf.ajaxFileUpload('/common/file/upload?subDir=/sample'
				, file
				, function(result) {
					// 업로드한 파일명 설정
					$('#detailForm input[name=orgFileNm]').val(result.oldFileName);
					$('#detailForm input[name=sysFileNm]').val(result.newFileName);
				}
		);
	});

	$(document).ready(function() {
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
	});
/*]]>*/
</script>

</html>
