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

<span data-doctx="${pageName}">
	<div class="card">
		<form name="frmEdit">

			<input type="hidden" name="crdnId" data-map="CRDN_ID" />

			<div class="row g-1">
				<div class="col-md-12">
					<label is="name-label" for="chgAmtSe"
						class="w-px-120 bg-lighter pe-2 col-form-label text-sm-end required">금액 수정 구분</label>
					<select is="id-select" name="chgAmtSe" class="form-select" required>
						<option value="1">가산</option>
						<option value="2">감액</option>
					</select>
				</div>
				<div class="col-md-12">
				★ 가산은 단속원금에 만원을 더합니다.<br />
				★ 감액은 단속원금을 만원만 남깁니다.
				</div>
			</div>
		</form>
	</div>
	<div>
		<span class="container-page-btn">
			<span class="container-window-btn-right">
				<button type="button" class="btn btn-primary" name="btnSave" title="금액 수정">금액 수정</button>
				<button type="button" class="btn btn-primary" name="btnRestore" title="원금 복원">원금 복원</button>
			</span>
		</span>
	</div>
</span>

<!-- / 업무 버튼 표시 -->

<script>
LoadScript("crdn02050Script",wctx.url("/resources/js/fims/crdn/crdn02050.js?ver=${ver}"));

/**************************************************************************
* Global Variable
**************************************************************************/
pageObject["${pageName}"] = newDoctxFinder("${pageName}");

$(document).ready(function(){

	var $P = pageObject["${pageName}"];

	/**************************************************************************
	* DatasetControl, DatasetSupport, FormFields
	**************************************************************************/
	$P.formFields = new FimsFormFields($P.selectorn("frmEdit"));

	var ctrl = newCrdn02050Control($P);

	ctrl.dataset.onCurrentChange = (dataItem) => {
		$P.formFields.set(ctrl,dataItem);
	}

	/**************************************************************************
	* pageObject.function
	**************************************************************************/
	$P.fnSave = async() => {

		var alertMessage = "";

		if($P.$findn("chgAmtSe").val() == "1"){
			if(crdnInfo.OVTM_YN == "Y"){
				dialog.alert({
					content : "이미 가산 처리된 자료입니다.",
					init : function(){
						AppSupport.setDialogZindex();
						AppSupport.focusClose();
					}
				});
				return;
			}
			alertMessage = "가산 처리하시겠습니까?";

		} else if($P.$findn("chgAmtSe").val() == "2"){
			if(crdnInfo.OVTM_PRTTN_YN == "Y"){
				dialog.alert({
					content : "이미 감액 처리된 자료입니다.",
					init : function(){
						AppSupport.setDialogZindex();
						AppSupport.focusClose();
					}
				});
				return;
			}
			alertMessage = "감액 처리하시겠습니까?";
		}

		if(await confirm2(alertMessage)){
			var info = $P.formFields.get();
			if (!info) {
				return;
			}

			ajax.post({
				url : ctrl.urls.update,
				data : info,
				success : (resp) => {
					$P.saveCallback(resp);
				}
			});

		}
	}

	$P.fnRestore = async() => {

		if(crdnInfo.OVTM_YN != "Y" && crdnInfo.OVTM_PRTTN_YN != "Y"){
			dialog.alert({
				content: "2시간 초과 처리되지 않은 자료입니다.",
				init : function() {
					AppSupport.setDialogZindex();
					AppSupport.focusClose();
				}
			});
			return;
		}

		if(await confirm2("원금 복원하시겠습니까?")){
			var info = $P.formFields.get();
			info.chgAmtSe = "0";

			ajax.post({
				url : ctrl.urls.update,
				data : info,
				success : (resp) => {
					$P.saveCallback(resp);
				}
			});
		}
	}

	$P.saveCallback = (resp) => {
		if (resp.saved) {
			dialog.alert({
				content : "저장됐습니다.",
				init : function(){
					AppSupport.setDialogZindex();
					AppSupport.focusClose();
				}
			});
			dialog.close($P.selfDlgId());

			if ("${savedCallbackFuncName}" != "") {
				${savedCallbackFuncName}(resp.saved);
			}
		}
	};

	/**************************************************************************
	* element.on
	**************************************************************************/
	$P.$findn("btnSave").on("click", () => $P.fnSave() );
	$P.$findn("btnRestore").on("click", () => $P.fnRestore() );

	/**************************************************************************
	* 초기화
	**************************************************************************/
	var crdnInfo = ${crdnInfo};
	ctrl.dataset.setData([crdnInfo]);
});


</script>
