<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" session="false"%>
<%@ include file="/WEB-INF/jsp/include/taglib.jsp"%>

<c:set var="prefixName" scope="request">반송 정보</c:set>

<!-- inner page html -->
<div class="content-pop">
	<!-- Content -->
	<div class="container flex-grow-1 px-0">

		<!-- Page Body -->
		<div class="card">
			<!-- 입력 영역 -->
			<form id="frmEdit--${pageName}" name="frmEdit">
				<!-- hidden -->
				<input type="hidden" id="sggCd--${pageName}" name="sggCd" data-map="SGG_CD" />
				<input type="hidden" id="taskSeCd--${pageName}" name="taskSeCd" data-map="TASK_SE_CD" />
				<input type="hidden" id="crdnId--${pageName}" name="crdnId" data-map="CRDN_ID" />
				<input type="hidden" id="sndngDtlId--${pageName}" name="sndngDtlId" data-map="SNDNG_DTL_ID" />

				<div class="row g-1">
					<!-- 배달 일자 -->
					<div class="col-md-12">
						<label for="dlvrYmd--${pageName}" class="w-px-120 bg-lighter pe-2 col-form-label text-sm-end required">배달일자</label>
						<input type="text" class="form-control form-date" id="dlvrYmd--${pageName}" name="dlvrYmd" data-map="DLVR_YMD"
							data-fmt-type="day" autocomplete="off" title="날짜 선택" maxlength="10" required />
						<button type="button" class="bx bx-lg bx-calendar bg-white"></button>
					</div>
					<!-- 미배달 사유 -->
					<div class="col-md-12">
						<label for="undlvrRsnCd--${pageName}" class="w-px-120 bg-lighter pe-2 col-form-label text-sm-end required">미배달사유</label>
						<select class="form-select" id="undlvrRsnCd--${pageName}" name="undlvrRsnCd" data-map="UNDLVR_RSN_CD" required>
							<c:forEach items="${FIM081List}" var="item">
								<option value="${item.code}">${item.value}</option>
							</c:forEach>
						</select>
					</div>
					<!-- 미배달 사유 -->
					<div class="col-md-12">
						<label for="undlvrRsnNm--${pageName}" class="w-px-120 bg-lighter pe-2 col-form-label text-sm-end required">미배달기타사유</label>
						<input type="text" class="form-control w-50" id="undlvrRsnNm--${pageName}" name="undlvrRsnNm" data-map="UNDLVR_RSN_NM" autocomplete="off" required />
					</div>
				</div>
			</form>
			<!-- 입력 영역 -->

			<!-- 업무 버튼 및 건수 표시 -->
			<div>
				<span class="container-page-btn">
					<!-- 업무 버튼 -->
					<span class="container-window-btn-right">
						<button type="button" class="btn btn-primary w-px-80" id="btnSave--${pageName}" title="저장">
							저장
						</button>
					</span>
				</span>
			</div> <!-- 업무 버튼 및 건수 표시 -->
		</div>	<!-- <div class="card"> -->
		<!-- Page Body -->

	</div> <!-- <div class="container flex-grow-1 px-0"> -->
	<!-- Content -->
</div> <!-- / inner page html <div class="content-pop"> -->

<script>
	/**************************************************************************
	 * Global Variable
	 **************************************************************************/
	pageObject["${pageName}"] = {};

	/**************************************************************************
	 * script 진입
	 **************************************************************************/
	$(document).ready(function() {
		// pageObject
		let $P = pageObject["${pageName}"];

	 	// infoDialog 호출 용도(view 조회(편집불가), create 등록, update 수정, delete 삭제, restore 복구)
		$P.callPurpose = "${callPurpose}";
		// FormFields
	 	$P.formFields = new FimsFormFields("#frmEdit--${pageName}");

		/**************************************************************************
		 * DatasetControl
		 **************************************************************************/
		$P.control = new DatasetControl({
			  prefix: "sndbk"
			, prefixName: "반송"
			, keymapper: info => info ? info.SNDNG_DTL_ID : ""
			, dataGetter: obj => obj.sndbkInfo
		});

		/**************************************************************************
		 * DatasetControl 이벤트
		 **************************************************************************/
		 $P.control.onCurrentChange = (item) => {
			if (!item) return;

			// Dataset 셋팅
			$P.formFields.set($P.control, item);

			// 업무구분에 따른 URL 변경
			$P.fnSetURL(item.data.TASK_SE_CD);

			// 호출용도가 등록 이라면..
			if ($P.callPurpose == "create") {
				// 배달일자
				$("#dlvrYmd--${pageName}").datepicker("setDate", new Date());
				// 미배달 사유 변경 이벤트 실행
				$("#undlvrRsnCd--${pageName}").val("99").attr("selected", "selected");
				$("#undlvrRsnCd--${pageName}").trigger("change");
			}
		}

		// 저장 callback
		$P.control.onSave = (resp) => {
	  		let dialogTitle = $("#" + $P.control.prefix + "Dialog--${openerPageName}").find("h5.modal-title").html();
	  		let showMessage = resp.rtnMsg.replace(/[S]|[F]/g, dialogTitle);

			dialog.alert({
				  content: showMessage
				, init: () => { AppSupport.setDialogZindex(); }
				, onOK: () => { }
			});

			if (resp.saved) {
				dialog.close($P.control.prefix + "Dialog--${openerPageName}");
			}
		}

		// 저장
		$P.control.save = (info) => {
			if (!info) return;

	  		let create = ($P.callPurpose == "create");

			ajax.post({
				  url: !create ? $P.control.urls.update : $P.control.urls.create
				, data: info || {}
				, success: resp => $P.control.onSave(resp)
			});
		}

		/**************************************************************************
		 * 사용자 함수(function)
		 **************************************************************************/
		// 업무구분에 따른 URL 설정
		$P.fnSetURL = (taskSeCd) => {
			$P.control.urls.create = wctx.url("${infoPrefixUrl}" + "/230/create.do");
			$P.control.urls.update = wctx.url("${infoPrefixUrl}" + "/230/update.do");
		}

		/**************************************************************************
		 * 버튼 clickEvent
		 **************************************************************************/
		// 저장 버튼 클릭 이벤트
		$P.fnSave = () => {
			// validate 확인
			if (!AppSupport.customValidate($("#frmEdit--${pageName}").find("input, select, textarea"))) return;

			// 파리미터 설정
			let params = $P.formFields.get();									// formFields
			params.sndngDtlSttsCd = "05";										// FIM050 - 05 반송

			dialog.alert({
				  content: "현재 " + $P.control.prefixName + " 정보를 저장하시겠습니까?"
				, init: () => { AppSupport.setDialogZindex(); }
				, onOK: () => {
					$P.control.save(params);
				}
			});
		}

		/**************************************************************************
		 * 초기 셋팅
		 **************************************************************************/
		// 이벤트 설정
		$P.setEvent = () => {
			// form-date 항목에서 키보드로 입력시 날짜 포맷팅 적용
			$("#frmEdit--${pageName}").find(".form-date").each(function() {
				$(this).on("input",function() {
					let value = this.value.replaceAll("-", "");

					if (value.length > 7) {
						this.value = value.substring(0,4) + "-" + value.substring(4,6) + "-" + value.substring(6);
					} else if(value.length > 5) {
						this.value = value.substring(0,4) + "-" + value.substring(4);
					}
				});
			});

			// 미배달 사유 변경 이벤트
 			$("#undlvrRsnCd--${pageName}").change(function() {
 				if ($("#undlvrRsnCd--${pageName}").val() == "99") {
 					$("#undlvrRsnNm--${pageName}").val("");
 				} else {
 					$("#undlvrRsnNm--${pageName}").val($("#undlvrRsnCd--${pageName} option:selected").text());
 				}
 			});

			// 버튼 이벤트
			$("#btnSave--${pageName}").on("click", () => $P.fnSave());			// 저장
		}

		// 초기 화면 설정
		$P.initForm = () => {
			// 달력 초기화
			AppSupport.initDatepicker("frmEdit--${pageName}");
			$("#dlvrYmd--${pageName}").datepicker("setDate", new Date());

			if ($P.callPurpose == "view") {
				// input 요소들을 disabled
				$("#frmEdit--${pageName}").find("input,textarea,select").prop("disabled", true);
				// button 요소들을 disabled
				$("#btnSave--${pageName}").prop("disabled", true);		// 저장 버튼
			}
		}

		/**************************************************************************
		 * 최초 실행 함수
		 **************************************************************************/
		// 1. 이벤트 설정
		$P.setEvent();

		// 2. 초기 화면 설정
		$P.initForm();

		// 3. Dataset 설정
		$P.control.setData([${sndbkInfo}]);
	});

</script>
