<%@ 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">
	<div class="card">
		<form id="frmEdit--${pageName}">
			<input id="crdnId--${pageName}" name="crdnId" type="hidden" data-map="CRDN_ID" />
			
			<div class="row g-1">
				<div class="col-md-4">
					<label for="cvlcptListNo--${pageName}" class="w-px-150 bg-lighter pe-2 col-form-label text-sm-end">
						목록번호
					</label>
					<input type="text" id="cvlcptListNo--${pageName}" name="cvlcptListNo" data-map="CVLCPT_LIST_NO"
						class="form-control" readonly />
				</div>
				<div class="col-md-4">
					<label for="cvlcptRcptNo--${pageName}" class="w-px-150 bg-lighter pe-2 col-form-label text-sm-end">
						접수번호
					</label>
					<input type="text" id="cvlcptRcptNo--${pageName}" name="cvlcptRcptNo" data-map="CVLCPT_RCPT_NO"
						class="form-control" readonly />
				</div>
				<div class="col-md-4">
					<label for="cvlcptAplcntNm--${pageName}" class="w-px-150 bg-lighter pe-2 col-form-label text-sm-end">
						신청자
					</label>
					<input type="text" id="cvlcptAplcntNm--${pageName}" name="cvlcptAplcntNm" data-map="CVLCPT_APLCNT_NM"
						class="form-control" readonly />
				</div>
				<div class="col-md-4">
					<label for="cvlcptRcptYmd--${pageName}" class="w-px-150 bg-lighter pe-2 col-form-label text-sm-end">
						접수일자
					</label>
					<input type="text" id="cvlcptRcptYmd--${pageName}" name="cvlcptRcptYmd" data-map="CVLCPT_RCPT_YMD"
						class="form-control" readonly />
				</div>
				<div class="col-md-4">
					<label for="cvlcptPrcsPrnmtDt--${pageName}" class="w-px-150 bg-lighter pe-2 col-form-label text-sm-end">
						처리기한
					</label>
					<input type="text" id="cvlcptPrcsPrnmtDt--${pageName}" name="cvlcptPrcsPrnmtDt" data-map="CVLCPT_PRCS_PRNMNT_DT"
						class="form-control" readonly />
				</div>
				<div class="col-md-12">
					<label for="cvlcptAplyCn--${pageName}" class="w-px-150 bg-lighter pe-2 col-form-label text-sm-center">
						민원 내용
					</label>
					<span class="float-end pe-4">
						<button type="button" class="btn btn-blue" id="btnMakeHwp--${pageName}" title="한글파일 다운">한글파일 다운</button>
						<div id="tempArea--${pageName}"></div>
					</span>
				</div>
				<div class="col-md-12">
					<div class="px-2">
						<textarea id="cvlcptAplyCn--${pageName}" name="cvlcptAplyCn" data-map="CVLCPT_APLY_CN"
						rows="25" style="width:100%" readonly
						></textarea>
					</div>
				</div>
				<div class="col-md-6">
					<label for="cvlcptAplySeCd--${pageName}" class="w-px-150 bg-lighter pe-2 col-form-label text-sm-center">
						접수경로
					</label>
					<select id="cvlcptAplySeCd--${pageName}" name="cvlcptAplySeCd" class="form-select" data-map="CVLCPT_APLY_SE_CD" 
					disabled="disabled">
						<option value=""></option>
						<c:forEach items="${FIM039List}" var="item">
							<option value="${item.code}">${item.value}</option>
		                </c:forEach>
					</select>
				</div>
				<div class="col-md-6" id="pageNavArea--${pageName}">
					<input type="text" id="countStauts--${pageName}" class="form-control w-px-150 text-center fw-bold" readonly />
					<button type="button" id="btnPrev--${pageName}" class="w-px-50 btn-PageUp">◀</button>
					<button type="button" id="btnNext--${pageName}" class="w-px-50 btn-PageDown">▶</button>
				</div>
			</div>
		</form>
	</div>
</div>
<script>
pageObject["${pageName}"] = {};
pageObject["${pageName}"].provided = {};

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.provided = pageObject.parentRes.pop();
	} else {
		$P.provided = pageObject.childReq.pop();
		$("#pageNavArea--${pageName}").hide();
	}
	
	/**************************************************************************
	 * DatasetControl, Dataset, FormFields
	 **************************************************************************/
	$P.formFields = new FimsFormFields("#frmEdit--${pageName}");

	$P.cvlcptControl = new DatasetControl({
	    urls : { load : "", getInfo : wctx.url("/"+$P.taskSeCd+"/cvlc/cvlc01/050/info.do") }, 
	    keymapper:info => info ? info.CRDN_ID : "",
	    dataGetter:obj => obj.cvlcptInfo,
	    formats: { 
	    	CVLCPT_RCPT_YMD : dateFormat,
	    	CVLCPT_PRCS_PRNMNT_DT : dateFormat
	    }
	});
	
	/**************************************************************************
	 * DatasetControl.on, Dataset.on
	 **************************************************************************/
	$P.cvlcptControl.onCurrentChange = (item) => {
		$P.formFields.set(item);
	}
	
	/**************************************************************************
	 * pageObject.function
	 **************************************************************************/
	
	$P.fnPrev = () => {
		var prevKey = $P.provided.getPrevKey($("#crdnId--${pageName}").val());
		if(prevKey != ""){
			$P.fnGo(prevKey);
		}
	}
	
	$P.fnNext = () => {
		var nextKey = $P.provided.getNextKey($("#crdnId--${pageName}").val());
		if(nextKey != ""){
			$P.fnGo(nextKey);
		}
	}
	
	$P.fnGo = (crdnId) => {
		ajax.get({
			url: $P.cvlcptControl.urls.getInfo,
			data: { "crdnId" : crdnId },
			headers: {          
			    Accept: "application/json; charset=utf-8"
			},
			success : resp => {
				var cvlcptInfo = resp.cvlcptInfo;
				cvlcptInfo.CVLCPT_APLY_CN = escapeHTMLEntity(cvlcptInfo.CVLCPT_APLY_CN);
				$P.cvlcptControl.setData([cvlcptInfo]);
				
				var cs = $P.provided.getCountStatus($("#crdnId--${pageName}").val());
				$("#countStauts--${pageName}").val(cs);
				$P.provided.selectRow(crdnId);
			}
		});
	}
	
	$P.fnMakeHwp = () => {
		
		ajax.post({
			url : wctx.url("/"+$P.taskSeCd+"/cvlc/cvlc01/050/makeFileFromHwpFormat.do"),
			data : { crdnId : $("#crdnId--${pageName}").val() },
			success : resp => {
				
		        var a = document.createElement("a");
	            a.href = resp.filePath;
	            a.download = resp.fileName;
	            document.getElementById("tempArea--${pageName}").appendChild(a);
	            a.click();
	            document.getElementById("tempArea--${pageName}").removeChild(a);
	            
			}
		});
	}
	
	/**************************************************************************
	 * element.on
	 **************************************************************************/
	$("#btnPrev--${pageName}").on("click", () => $P.fnPrev() );
	$("#btnNext--${pageName}").on("click", () => $P.fnNext() );
	$("#btnMakeHwp--${pageName}").on("click", () => $P.fnMakeHwp() );
	
	/**************************************************************************
	 * 초기화
	 **************************************************************************/
	var cvlcptInfo = ${cvlcptInfo};
	cvlcptInfo.CVLCPT_APLY_CN = escapeHTMLEntity(cvlcptInfo.CVLCPT_APLY_CN);
	$P.cvlcptControl.setData([cvlcptInfo]);
	var cs = $P.provided.getCountStatus($("#crdnId--${pageName}").val());
	$("#countStauts--${pageName}").val(cs);
});
</script>