<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : SampleFileForm.html
 * @desc    : 샘플양식관리 Page
 *============================================================================
 * SISUN
 * Copyright(C) 2019 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2019.07.10   gagamel     최초 작성
 *******************************************************************************
 -->
	<div id="main">
		<!-- 메인타이틀 영역 -->
		<div class="main-title">
		</div>
		<!-- //메인타이틀 영역 -->

		<div class="panelStyle2">
			<form id="registerForm" name="registerForm" action="#" th:action="@{'/system/sample/file/save'}">
				<table class="frmStyle" aria-describedby="신규">
					<colgroup>
						<col style="width:10%;"/>
						<col style="width:40%;"/>
						<col style="width:10%;"/>
						<col/>
					</colgroup>
					<tr>
						<th>샘플파일ID<i class="star" aria-hidden="true"></i></th>
						<td>
							<input type="text" class="w100" name="sampleFileId" placeholder="" value="자동생성" readonly="readonly"/>
						</td>
						<th>샘플파일명<i class="star" aria-hidden="true"></i></th>
						<td>
							<input type="text" name="sampleFileNm" placeholder="" required="required" data-valid-name="샘플파일명"/>
						</td>
					</tr>
					<tr>
						<th>샘플파일<i class="star" aria-hidden="true"></i></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>사용여부<i class="star" aria-hidden="true"></i></th>
						<td colspan="3">
							<input type="hidden" name="useYn"/>
							<label><input type="checkbox" name="chkUseYn" value="Y" checked="checked" disabled="disabled"/>사용</label>
						</td>
					</tr>
				</table>

				<div class="panelBtnB">
					<button type="button" class="btn btn-info btn-lg" id="btnNew">신규</button>
					<button type="button" class="btn btn-success btn-lg" id="btnSave">저장</button>
				</div>
			</form>
		</div>

		<div class="panelStyle2">
			<div class="panelContent" style="overflow: hidden;">
				<div id="gridList" style="width: 100%;" class="ag-theme-balham"></div>
			</div>
		</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;

		fnBindDetail(event.data);
	}

	// 조회
	var fnSearch = function() {
		gagaAgGrid.fetch('/system/sample/file/list', gridOptions);
	}

	// 목록 > row 클릭 시
	var fnBindDetail = function(rowData) {
		$('#registerForm input[name=sampleFileId]').val(rowData.sampleFileId);
		$('#registerForm input[name=sampleFileNm]').val(rowData.sampleFileNm);
		$('#registerForm input[name=orgFileNm]').val(rowData.orgFileNm);
		$('#registerForm input[name=sysFileNm]').val(rowData.sysFileNm);
		$('#sampleFileList').html('<a href="javascript:void(0);" onclick="cfnDownloadSampleFile(\'' + rowData.sampleFileId + '\');">' + rowData.sysFileNm + '</a>');
		$('#sampleFileList').show();

		$('#registerForm input:checkbox[name=chkUseYn]').attr('disabled', false);
		$('#registerForm input:checkbox[name=chkUseYn]').removeClass('formControl');
		$("#registerForm input[name=useYn]").val(rowData.useYn);
		if (rowData.useYn == 'Y') {
			$("#registerForm input:checkbox[name=chkUseYn]").prop('checked', true);
			$("#registerForm input:checkbox[name=chkUseYn]").parent().addClass('checked');
		} else {
			$("#registerForm input:checkbox[name=chkUseYn]").prop('checked', false);
			$("#registerForm input:checkbox[name=chkUseYn]").parent().removeClass('checked');
		}
	}

	// 신규
	$('#btnNew').on('click', function() {
		$('#registerForm')[0].reset();
		$('#registerForm input[name=sampleFileId]').val('자동생성');
		$('#registerForm input[name=file]').val('');
		$('#registerForm input:checkbox[name=chkUseYn]').attr('disabled', true);
		$('#registerForm input:checkbox[name=chkUseYn]').addClass('formControl');
		$('#sampleFileList').html('');
		$('#sampleFileList').hide();
	});

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

		if (gagajf.isNull($('#registerForm input[name=sysFileNm]').val())) {
			mcxDialog.alert('샘플파일을 선택해 주세요.');
			return;
		}

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

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

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

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

	$(document).ready(function() {
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);

		// grid 높이 자동 조정
		uifnFitGrid('auto');

		fnSearch();
	});
/*]]>*/
</script>

</html>
