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

<div class="card">
	<form id="frmEdit--${pageName}" name="frmEdit">

		<input type="hidden" id="crdnId--${pageName}" name="crdnId" data-map="CRDN_ID" />
		
		<div class="row g-1">
			<div class="col-md-12">
				<label for="levyExclRsnCd--${pageName}" 
					class="w-px-120 bg-lighter pe-2 col-form-label text-sm-end required">금액 수정 구분</label>
				<select class="form-select" id="chgAmtSe--${pageName}" name="chgAmtSe" 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" id="btnSave--${pageName}" title="금액 수정">금액 수정</button>
			<button type="button" class="btn btn-primary" id="btnRestore--${pageName}" title="원금 복원">원금 복원</button>
		</span>
	</span>
</div>
<!-- / 업무 버튼 표시 -->

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

$(document).ready(function(){
	
	var $P = pageObject["${pageName}"];
	/**************************************************************************
	 * DatasetControl, Dataset, FormFields
	 **************************************************************************/
	$P.formFields = new FimsFormFields("#frmEdit--${pageName}");
	
	$P.crdnControl = new DatasetControl({
	    urls : { update : wctx.url("/PVS/crdn/crdn02/050/update.do") },
	    keymapper : info => info ? info.CRDN_ID : "",
	    dataGetter : obj => obj.crdnInfo,
	    formats: {}
	});
	
	$P.crdnControl.save = (info) => {	
		if (!info) return;

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

	/**************************************************************************
	 * DatasetControl.on, Dataset.on
	 **************************************************************************/
	$P.crdnControl.onCurrentChange = (item) => {
		$P.formFields.set(item);
	}
	
	$P.crdnControl.onSave = (resp) => {
		if (resp.saved) {

			dialog.alert("저장됐습니다.");			
			dialog.close("overTimeProcessDialog--${openerPageName}");
			
			if ("${savedCallbackFuncName}" != "") {
				${savedCallbackFuncName}(resp.saved);
			}
		}
	};
	/**************************************************************************
	 * pageObject.function
	 **************************************************************************/
	$P.fnSave = () => {

		var alertMessage = "";
		if($("#chgAmtSe--${pageName}").val() == "1"){
			if(crdnInfo.OVTIME_YN == "Y"){
				dialog.alert({ content: "이미 가산 처리된 자료입니다." });
				return;
			}
			alertMessage = "가산 처리하시겠습니까?"; 
			
		} else if($("#chgAmtSe--${pageName}").val() == "2"){
			if(crdnInfo.OVTIME_PRTTN_YN == "Y"){
				dialog.alert({ content: "이미 감액 처리된 자료입니다." });
				return;
			}
			alertMessage = "감액 처리하시겠습니까?";
		}
		
		if(confirm(alertMessage)){
			$P.crdnControl.save($P.formFields.get());
		}
	}
	
	$P.fnRestore = () => {

		if(crdnInfo.OVTIME_YN != "Y" && crdnInfo.OVTIME_PRTTN_YN != "Y"){
			dialog.alert({ content: "2시간 초과 처리되지 않은 자료입니다." });
			return;
		}

		if(confirm("원금 복원하시겠습니까?")){
			var info = $P.formFields.get();
			info.chgAmtSe = "0";
			$P.crdnControl.save(info);
		}
	}
	
	/**************************************************************************
	 * element.on
	 **************************************************************************/
	$("#btnSave--${pageName}").on("click", () => $P.fnSave() );
	$("#btnRestore--${pageName}").on("click", () => $P.fnRestore() );
	/**************************************************************************
	 * 초기화
	 **************************************************************************/
	var crdnInfo = ${crdnInfo};
	$P.crdnControl.setData([crdnInfo]);
});


</script>
