<!DOCTYPE html>
<html lang="ko"
	xmlns:th="http://www.thymeleaf.org">
<!--
 *******************************************************************************
 * @source  : SplashImageForm.html
 * @desc    : 스플래시이미지관리 Page
 *============================================================================
 * STYLE24
 * Copyright(C) 2020 TSIT, All rights reserved.
 *============================================================================
 * VER  DATE         AUTHOR      DESCRIPTION
 * ===  ===========  ==========  =============================================
 * 1.0  2021.07.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="@{'/display/splash/image/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">
					<span class="infoTxt cBlue">
						<i class="fa fa-info-circle" aria-hidden="true"></i>이미지 클릭 시 해당 이미지를 볼 수 있습니다.
					</span>
				</li>
			</ul>
			<!-- //버튼 배치 영역 -->
			
			<div id="gridList" style="width: 100%; height: 370px" class="ag-theme-balham lh60"></div>
		</div>
		<!-- //리스트 영역 -->
		
		<!-- 등록/수정 영역 -->
		<div class="panelStyle">
			<form id="detailForm" name="detailForm" action="#" th:action="@{'/display/splash/image/save'}">
				<input type="hidden" name="mode" value="N"/>
				
				<table class="frmStyle" aria-describedby="신규">
					<colgroup>
						<col style="width:12%;"/>
						<col style="width:38%;"/>
						<col style="width:12%;"/>
						<col/>
					</colgroup>
					<tr>
						<th>스플래시이미지번호<em class="required" title="필수"></em></th>
						<td>
							<input type="text" class="w100" name="splashImgSq" placeholder="" value="자동생성" readonly="readonly"/>
						</td>
						<th>스플래시이미지<em class="required" title="필수"></em></th>
						<td>
							<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="splashImg"/>
							</div>
						</td>
					</tr>
					<tr>
						<th>노출기간<em class="required" title="필수"></em></th>
						<td>
							<input type="text" class="schDate w80" name="dispStdt" maxlength="10" required="required" data-valid-type="calendar" data-valid-name="노출시작일자"/>
							~
							<input type="text" class="schDate w80" name="dispEddt" maxlength="10" required="required" data-valid-type="calendar" data-valid-name="노출종료일자"/>
						</td>
						<th>노출여부<em class="required" title="필수"></em></th>
						<td>
							<input type="hidden" name="dispYn"/>
							<label class="chkBox"><input type="checkbox" name="chkDispYn" value="Y" checked="checked"/>노출</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[*/
	var uploadImgUrl = [[${@environment.getProperty('upload.image.view')}]];
	
	// specify the columns
	var columnDefs = [
		{
			headerName: "스플래시이미지번호", field: "splashImgSq", width: 150, cellClass: 'text-center',
			cellRenderer: function(params) {
				return '<a href="javascript:void(0);">' + params.value + '</a>';
			}
		},
		{
			headerName: "이미지파일명", field: "imgNm", width: 150,
			cellRenderer: function (params) { return params.data.splashImg; }
		},
		{
			headerName: "이미지", field: "splashImg", width: 100,
			cellRenderer: function(params) {
				return '<img width="60" src="' + uploadImgUrl + "/splashimg/" + params.value + '?RS=60" alt="" onclick="cfnOpenImagePreViewPopup(\'splashImgView\', \'' + uploadImgUrl + "/splashimg/" + params.value + '\')" onerror="this.src=\'/image/no.png\';"/>';
			}
		},
		{
			headerName: "노출시작일", field: "dispStdt", width: 150, cellClass: 'text-center',
			cellRenderer: function (params) { return gagaAgGrid.toDateFormat(params.value); }
		},
		{
			headerName: "노출종료일", field: "dispEddt", width: 150, cellClass: 'text-center',
			cellRenderer: function (params) { return gagaAgGrid.toDateFormat(params.value); }
		},
		{headerName: "노출여부", field: "dispYn", width: 100, cellClass: 'text-center'},
		{headerName: "최종수정자", field: "updNm", width: 100, cellClass: 'text-center'},
		{
			headerName: "최종수정일시", field: "updDt", width: 150, cellClass: 'text-center',
			cellRenderer: function (params) { return gagaAgGrid.toDateTimeFormat(params.value); }
		},
	];

	// Get GridOptions
	var gridOptions = gagaAgGrid.getGridOptions(columnDefs);
	gridOptions.rowHeight = 60; // 이미지가 있을 경우 높이 지정
	
	// Cell Click
	gridOptions.onCellClicked = function(event) {
		if (event.colDef.field != 'splashImgSq')
			return;

		$('#detailForm input[name=mode]').val('U');
		$('#detailForm input[name=splashImgSq]').val(event.data.splashImgSq);
		$('#detailForm input[name=orgFileNm]').val(event.data.splashImg);
		$('#detailForm input[name=splashImg]').val(event.data.splashImg);
		$('#detailForm input[name=dispStdt]').val(event.data.dispStdt.toDate('YYYYMMDD').format('YYYY-MM-DD'));
		$('#detailForm input[name=dispEddt]').val(event.data.dispEddt.toDate('YYYYMMDD').format('YYYY-MM-DD'));

		if (event.data.dispYn == 'Y') {
			$("#detailForm input:checkbox[name=chkDispYn]").prop('checked', true);
			$("#detailForm input:checkbox[name=chkDispYn]").parent().addClass('checked');
		} else {
			$("#detailForm input:checkbox[name=chkDispYn]").prop('checked', false);
			$("#detailForm input:checkbox[name=chkDispYn]").parent().removeClass('checked');
		}
		
		$('#detailForm input:checkbox[name=chkDispYn]').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=splashImgSq]').val('자동생성');
		$('#detailForm input[name=file]').val('');
		var date = new Date();
		$('#detailForm input[name=dispStdt]').val(date.format("YYYY-MM-DD"));
		$('#detailForm input[name=dispEddt]').val(date.format("YYYY-MM-DD"));
		$('#detailForm input:checkbox[name=chkDispYn]').prop('checked', true);
		$('#detailForm input:checkbox[name=chkDispYn]').attr('readonly', true);
	});

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

		if (gagajf.isNull($('#detailForm input[name=splashImg]').val())) {
			mcxDialog.alert('스플래시이미지 파일을 선택해 주세요.');
			return;
		}
		
		// 노출기간 체크
		let stdt = $('#detailForm input[name=dispStdt]').val().toDate('YYYY-MM-DD');
		let eddt = $('#detailForm input[name=dispEddt]').val().toDate('YYYY-MM-DD');
		console.log(stdt + ':' + eddt);
		
		if (stdt > eddt) {
			mcxDialog.alert('노출시작일자는 노출종료일자 보다 클 수 없습니다.');
			return;
		}

		$('#detailForm input[name=splashImgSq]').val('');
		$('#detailForm input[name=dispYn]').val($('#detailForm input:checkbox[name=chkDispYn]').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?policy=image&subDir=/splashimg'
				, file
				, function(result) {
					// 업로드한 파일명 설정
					$('#detailForm input[name=orgFileNm]').val(result.oldFileName);
					$('#detailForm input[name=splashImg]').val(result.newFileName);
				}
		);
	});

	$(document).ready(function() {
		// Create a agGrid
		gagaAgGrid.createGrid('gridList', gridOptions);
		
		var date = new Date();
		$('#detailForm input[name=dispStdt]').val(date.format("YYYY-MM-DD"));
		$('#detailForm input[name=dispEddt]').val(date.format("YYYY-MM-DD"));
	});
/*]]>*/
</script>

</html>
