<%@ 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="pageKorName" scope="request">민원 답변내용 미리보기</c:set>

<div class="d-flex flex-row justify-content-evenly" data-doctx="${pageName}">
	<div class="card">
		<form name="frmEdit">
			<input name="crdnId" type="hidden" data-map="CRDN_ID" />
			<input name="cvlcptLinkId" type="hidden" data-map="CVLCPT_LINK_ID" />

			<div class="row g-1">
				<div class="col-md-6">
					<label is="name-label" for="cvlcptAplyCn"
					class="w-px-150 bg-lighter pe-2 col-form-label text-sm-center">
						민원 내용
					</label>
				</div>
				<div class="col-md-6 text-end">
					<input type="text" name="countStauts"
					class="form-control w-px-150 text-center fw-bold" readonly />
					<button type="button" name="btnPrev" class="w-px-50 btn-PageUp">◀</button>
					<button type="button" name="btnNext" class="w-px-50 btn-PageDown">▶</button>
				</div>
				<div class="col-md-12">
					<div class="px-2">
						<textarea is="id-textarea" name="cvlcptAplyCn"
						data-map="CVLCPT_APLY_CN" rows="10" class="w-100"
						></textarea>
					</div>
				</div>

				<div class="col-md-12">
					<label is="name-label" for="cvlcptPrcsRsltCn"
					class="required w-px-150 bg-lighter pe-2 col-form-label text-sm-center">
						답변 내용
					</label>
				</div>
				<div class="col-md-12">
					<div class="px-2">
						<textarea is="id-textarea" name="cvlcptPrcsRsltCn"
						data-map="CVLCPT_PRCS_RSLT_CN"
						required rows="10" class="w-100"
						></textarea>
					</div>
				</div>

				<div class="col-md-6">
					변경할내용이있으면 수정하시오.
				</div>
				<div class="col-md-6 text-end">
					<button type="button" name="btnSave" class="btn btn-primary">
					변경 내용 저장
					</button>
				</div>
			</div>
		</form>
	</div>
</div>

<script>
LoadScript("cvlc02060Script",wctx.url("/resources/js/fims/cvlc/cvlc02060.js?ver=${ver}"));

pageObject["${pageName}"] = newDoctxFinder("${pageName}");
pageObject["${pageName}"].parentRes = {};

pageObject.childReq = [];
pageObject.childReq.push({
	getCountStatus : function(){ return ""; },
	getNextKey : function(){ return ""; },
	getPrevKey : function(){ return ""; },
	selectRow : function(){}
});

$(document).ready(function() {

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

	if(pageObject.parentRes.length > 0){
		$P.parentRes = pageObject.parentRes.pop();
	} else {
		$P.parentRes = pageObject.childReq.pop();
	}

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

	var ctrl = newCvlc02060Control($P);

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

	/**************************************************************************
	* pageObject.function
	**************************************************************************/
	$P.fnPrev = () => {
		var prevKey = $P.parentRes.getPrevKey($P.$findn("crdnId").val());
		if(prevKey != ""){
			$P.fnGo(prevKey);
		}
	}

	$P.fnNext = () => {
		var nextKey = $P.parentRes.getNextKey($P.$findn("crdnId").val());
		if(nextKey != ""){
			$P.fnGo(nextKey);
		}
	}

	$P.fnGo = (crdnId) => {
		ajax.get({
			url: ctrl.urls.getInfo,
			data: { "crdnId" : crdnId },
			headers: {
			    Accept: "application/json; charset=utf-8"
			},
			success : (resp) => {
				var cvlcptInfo = resp.cvlcptInfo;
				cvlcptInfo.CVLCPT_APLY_CN = StringSupport.escapeHTMLEntity(cvlcptInfo.CVLCPT_APLY_CN);
				ctrl.dataset.setData([cvlcptInfo]);

				var cs = $P.parentRes.getCountStatus($P.$findn("crdnId").val());
				$P.$findn("countStauts").val(cs);
				$P.parentRes.selectRow(crdnId);
			}
		});
	}

	$P.fnSave = async() => {

		if(!AppSupport.customValidate($P.$findn("frmEdit").find("input"))) return;

		if(await confirm2("현재 답변 내용을 저장하시겠습니까?")){
			var info = $P.formFields.get();

			if (!info) {
				return;
			}

			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();
				}
			});
		}
	}

	/**************************************************************************
	* element.on
	**************************************************************************/
	$P.$findn("btnPrev").on("click", () => $P.fnPrev() );
	$P.$findn("btnNext").on("click", () => $P.fnNext() );

	$P.$findn("btnSave").on("click", () => $P.fnSave() );

	/**************************************************************************
	* 초기화
	**************************************************************************/
	var cvlcptInfo = ${cvlcptInfo};
	cvlcptInfo.CVLCPT_APLY_CN = StringSupport.escapeHTMLEntity(cvlcptInfo.CVLCPT_APLY_CN);
	ctrl.dataset.setData([cvlcptInfo]);
	var cs = $P.parentRes.getCountStatus($P.$findn("crdnId").val());
	$P.$findn("countStauts").val(cs);

	if($("#"+$P.selfDlgId()).attr("data-ref-doctx") == "cvlc02010") {
		$P.$findn("btnSave").show();
	} else {
		$P.$findn("btnSave").hide();
	}
});

</script>