<%@ 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="content-wrapper" data-doctx="${pageName}">
	<div class="container-xxl flex-grow-1 px-0">

		<div class="card">
			<form name="frmEdit">

				<input type="text" name="ntcId" data-map="NTC_ID" hidden />

				<div class="row g-1">
					<c:if test="${isPublicOfficer}">
						<div class="col-md-12">
							<label is="name-label" for="cntnSeCd"
							class="w-px-120 bg-lighter pe-2 col-form-label text-sm-end required">
							접속구분
							</label>
							<select is="id-select" name="cntnSeCd" data-map="CNTN_SE_CD" class="form-select" required>
								<option value="01">행정망</option>
								<option value="02">인터넷망</option>
							</select>
						</div>
						<div class="col-md-12">
							<label is="name-label" for="sggCd"
							class="w-px-120 bg-lighter pe-2 col-form-label text-sm-end">
							시군구
							</label>
							<select is="id-select" name="sggCd" data-map="SGG_CD" class="form-select">
								<option value="">전체</option>
								<c:forEach items="${sggList}" var="item">
									<c:if test="${item.SGG_CD ne 'ADMIN'}">
										<option value="${item.SGG_CD}">${item.SGG_NM}</option>
									</c:if>
								</c:forEach>
							</select>
						</div>
					</c:if>
					<div class="col-md-12">
						<label is="name-label" for="ntcTtl"
						class="w-px-120 bg-lighter pe-2 col-form-label text-sm-end required">
						제목
						</label>
						<input type="text" is="id-input" name="ntcTtl" data-map="NTC_TTL"
						class="form-control w-px-800" required />
					</div>

					<div class="col-md-12">
						<label is="name-label" for="ntcCn"
						class="w-px-120 bg-lighter pe-2 col-form-label text-sm-end align-top required">
						내용
						</label>
						<textarea is="id-textarea" name="ntcCn" data-map="NTC_CN"
						rows="15"
						class="form-control w-px-800" required></textarea>
					</div>

				</div>

				<div class="row m-3">
					<div class="col-md-12">
						<span class="float-end">
							<button type="button" name="btnSave" class="btn btn-primary">저장</button>
							<button type="button" name="btnRemove" class="btn btn-primary">삭제</button>
						</span>
					</div>
				</div>
			</form>


		</div>

	</div>
</div>

<script>
LoadScript("mngt01020Script",wctx.url("/resources/js/fims/mngt/mngt01020.js?ver=${ver}"));

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

pageObject.childReq = [];
pageObject.childReq.push({
	refreshList : function(){ },
});

$(document).ready(function(){

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

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

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

	var ctrl = newMngt01020Control($P);

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

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

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

		var info = $P.formFields.get();

		var create = ctrl.dataset.empty;

		ajax.post({
			url : create ? ctrl.urls.create : ctrl.urls.update,
			data : info,
			success : (resp) => {
				if(resp.saved){
					dialog.close($P.selfDlgId());
					dialog.alert({
						content:"저장되었습니다.",
						onOK : () => $P.provided.refreshList()
					});
				}
			}
		});
	}

	$P.fnRemove = () => {

		var info = $P.formFields.get();

		ajax.post({
			url : ctrl.urls.remove,
			data : info,
			success : (resp) => {
				if(resp.saved){
					dialog.close("ntcInfoDialog");
					dialog.alert({
						content:"삭제되었습니다.",
						onOK : () => $P.provided.refreshList()
					});
				}
			}
		});
	}

	/**************************************************************************
	* element.on
	**************************************************************************/
	$P.$findn("btnSave").on('click', () => $P.fnSave());
	$P.$findn("btnRemove").on('click', () => $P.fnRemove());

	/**************************************************************************
	* 초기화
	**************************************************************************/
	var ntcInfo = ${ntcInfo};

	if($P.selfDlgId().startsWith("pop")){
		$P.$findn("btnSave").remove();
		$P.$findn("btnRemove").remove();
	}

	if(ntcInfo == null){
		$P.$findn("sggCd").html("");

		if(MY_INFO.orgID == "ADMIN"){
			$P.$findn("sggCd").append("<option value=''>전체</option>");
		}
		let selectableSggList = ${selectableSggList};
		if(selectableSggList != null){
			for(let i=0; i<selectableSggList.length; i++){
				$P.$findn("sggCd").append("<option value='"+selectableSggList[i].SGG_CD+"'>"+selectableSggList[i].SGG_NM+"</option>");
			}
		}

		ctrl.dataset.setData([]);

		$P.$findn("cntnSeCd").val("${cntnSeCd}");
		$P.$findn("cntnSeCd").attr("disabled","disabled");

		if(MY_INFO.orgID != "ADMIN"){
			if(selectableSggList != null && selectableSggList.length > 1){
				if("${cntnSeCd}" == "01"){
					$P.$findn("sggCd").val(MY_INFO.orgID);
				} else {
					if("${upSggCd}" != ""){
						$P.$findn("sggCd").val("${upSggCd}");
					} else {
						$P.$findn("sggCd").val(MY_INFO.orgID);
					}
					$P.$findn("sggCd").find("option").not(":selected").remove();
				}
			}
		}

		$P.$findn("btnRemove").attr("hidden","hidden");
	} else {

		ctrl.dataset.setData([ntcInfo]);

		$P.$findn("sggCd").attr("disabled","disabled");
		$P.$findn("cntnSeCd").attr("disabled","disabled");

		if(!MY_INFO.info.isAdmin){
			$P.$findn("btnSave").attr("hidden","hidden");
			$P.$findn("btnRemove").attr("hidden","hidden");
		}

	}

});

</script>