<%@ 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 wrapper-list">

		    <div class="container-page-btn">
		        <button type="button" name="btnReset" class="btn btn-outline-dark w-px-120" title="초기화">
		        초기화
		        </button>
		        <span class="container-window-btn-right">
		            <button type="button" name="btnSearch" class="btn btn-search w-px-120" title="검색">
		            검색
		            </button>
		        </span>
		    </div>

		    <form name="frmSearch">
		        <div class="container-search">
                    <div class="row g-1">
                    	<c:if test="${isPublicOfficer}">
	                    	<div class="col-12">
	                    		<label is="name-label" for="cntnSeCd"
	                    		class="form-label fw-bold form-search-title">접속구분</label>
	                    		<select is="id-select" name="cntnSeCd" class="form-select">
	                    			<option value="">전체</option>
	                        		<option value="01">행정망</option>
	                        		<option value="02">인터넷망</option>
	                        	</select>
	                    	</div>
	                    	<div class="col-12">
	                    		<label is="name-label" for="sggCd"
	                    		class="form-label fw-bold form-search-title">시군구</label>
	                    		<select is="id-select" name="sggCd" class="form-select">
	                    			<option value="">전체</option>
									<c:forEach items="${sggList}" var="item">
										<option value="${item.SGG_CD}">${item.SGG_NM}</option>
									</c:forEach>
	                        	</select>
	                    	</div>
                    	</c:if>
                        <div class="col-12">
                        	<select is="id-select" name="by" class="form-select">
                        		<option value="NTC_TTL">제목</option>
                        		<option value="NTC_CN">내용</option>
                        	</select>
                        	<input type="text" is="id-input" name="term" class="form-control" />
                        </div>
                    </div>
		        </div>

		    </form>

		    <div>
		        <div name="gridbuttonArea" class="container-page-btn">
		            <div class="d-flex flex-row justify-content-between">
						<span is="paing-info" name="ntcPagingInfo" class="dataTables_info"></span>
		                <ul name="ntcPaging" class="pagination pagination-primary"></ul>
					</div>

					<span class="container-window-btn-right">
						<span>
							<button type="button" class="btn btn-primary w-px-120"
							name="btnAdd" title="행정망 공지사항 추가">
							행정망 공지사항 추가
							</button>
							<button type="button" class="btn btn-primary w-px-120"
							name="btnAdd2" title="인터넷망 공지사항 추가">
							인터넷망 공지사항 추가
							</button>
						</span>
					</span>
		        </div>
		    </div>

		    <div class="card-datatable text-nowrap">
		        <div name="DataTables_Table_0_wrapper" class="dataTables_wrapper dt-bootstrap5 no-footer">
					<div name="table-responsive" class="table-responsive ox-scroll oy-scroll h-px-500">
						<table name="ntcTable" class="table-layout-fixed datatables-ajax table table-bordered dataTable no-footer">
							<thead class="sticky-thead">
								<tr name="ntcThead">
									<th style="width: 80px;" class="cmn">No.</th>
									<c:if test="${isPublicOfficer}">
										<th style="width: 180px;">접속구분</th>
										<th style="width: 180px;">시군구명</th>
									</c:if>
									<th style="width: 800px;">제목</th>
									<th style="width: 180px;">작성일시</th>
									<th class="dummy-th"></th>
								</tr>
							</thead>
		                    <tbody name="ntcTbody">
		                    </tbody>
							<template is="curly-brackets" name="ntcRow">
								<tr data-index="DATA-INDEX">
									<td onclick ondblclick class="text-end">ROW_NUM</td>
									<c:if test="${isPublicOfficer}">
										<td onclick ondblclick class="text-start">CNTN_SE_NM</td>
										<td onclick ondblclick class="text-start">SGG_NM</td>
									</c:if>
									<td onclick ondblclick class="text-start">NTC_TTL</td>
		                    		<td onclick ondblclick class="text-center">REG_DT</td>
		                    		<td class="dummy-td"></td>
	                    		</tr>
							</template>
							<template name="ntcNotFound">
								<tr>
									<td valign="top" colspan="${isPublicOfficer ? 6 : 4}" class="dataTables_empty text-center">
									공지사항 정보를 찾지 못했습니다.
									</td>
								</tr>
							</template>
						</table>
					</div>
				</div>
		    </div>

			<div class="toast-container position-fixed bottom-0 end-0 p-3">
				<div name="divToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
			    	<div class="toast-body bg-black text-white">
			    	  삭제 되었습니다.
			    	</div>
			  	</div>
			</div>
		</div>

	</div>

  	<div class="content-backdrop fade"></div>
</div>


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

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


$(document).ready(function(){

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

	$P.toast = new bootstrap.Toast($P.findn("divToast"), {
        animation: true,
        autohide: true,
        delay: 2000
    });

	/**************************************************************************
	* DatasetControl, DatasetSupport, FormFields
	**************************************************************************/
	var ctrl = newMngt01010Control($P);
	ctrl.defaultFetchSize = FETCH_XS;
	ctrl.query = { pageNum : 1, fetchSize : ctrl.defaultFetchSize };

	ctrl.dataset.onDatasetChange = (obj, option) => {
		var t = $P.getGridTemplate();
		var trs = Apply.fromDataset.getTbody(ctrl.dataset, t.found, t.notFound, t.replacer);
		$P.renderNtcList(obj?.Paging?.totalSize, ctrl.dataset.length, trs, option);

		$P.pagingSupport.setPagingInfo(obj);
	};

	ctrl.dataset.onCurrentChange = (dataItem) => {
		Apply.fromDataset.currentRow(ctrl.dataset, dataItem, $P.findn("ntcTbody"));
	}

	ctrl.dataset.onSelectionChange = (selectedArr) => {

	};

	$P.pagingSupport = new FimsPagingSupport({
		doq: $P,
        linkContainer: "[name='ntcPaging']"
	});

	/**************************************************************************
	* pageObject.function
	**************************************************************************/
	$P.fnReset = () => {
		var searchForm = $P.$findn("frmSearch");
		searchForm.find("input[type='radio']").not("[name='taskSeCd']").prop("checked", false);
		searchForm.find("input[type='checkbox']").prop("checked", false);
		searchForm.find("input[type='hidden']").val("");
		searchForm.find("input[type='text']").val("");

		searchForm.find("select[name='cntnSeCd']").val("");
		searchForm.find("select[name='sggCd']").val("");

		ctrl.dataset.setData([]);
	}

	$P.getParams = () => {
		var formFields = new FimsFormFields($P.selectorn("frmSearch"));
		var data = formFields.get();
		data.fetchSize = ctrl.defaultFetchSize;

		return data;
	}

	$P.searchNtcList = () => {
		ctrl.query = $P.getParams();
	    ctrl.load(1);
	}

	$P.scrollNtcList = () => {
		ctrl.load(ctrl.query.pageNum + 1);
	}

	$P.refreshNtcList = () => {
		if(ctrl.query.pageNum == null){
			return;
		}

        ctrl.reload({all : true});
    }

	$P.getGridTemplate = () => {
		var notFound = [$P.findn("ntcNotFound").innerHTML];
		var found = $P.findn("ntcRow").innerHTML;
		var replacer = (str, dataItem) => str
			.replace(/{onclick}/gi, "pageObject['${pageName}'].clickNtcList('{DATA-INDEX}');")
			.replace(/{ondblclick}/gi, "pageObject['${pageName}'].dblclickNtcList('{NTC_ID}');");

		return {
			found : found,
			notFound : notFound,
			replacer : replacer
		};
	}

	$P.renderNtcList = (total, listLength, trs, option) => {

		let noMore = (listLength >= total);
		var initScroll = (ctrl.query.pageNum < 2);
		if(option != null && option.reloaded){
			initScroll = false;
		}

		$P.findn("table-responsive").changeContent(trs, initScroll, noMore);

	    fn_securityModeToggle($("#securityMode--top").is(":checked")); //보안모드

	}

	$P.clickNtcList = (dataIndex) => {
		if(dataIndex == ""){
			return;
		}

		$P.$findn("ntcTbody").setCurrentRow(dataIndex);

		Apply.toDataset.current(ctrl.dataset, dataIndex);
	}

	$P.dblclickNtcList = (dataKey) => {
		$P.getInfo(dataKey);
	}

	$P.createNtc = (cntnSeCd) => {
		$P.getInfo(null,cntnSeCd);
	}

	$P.getInfo = (ntcId,cntnSeCd) => {
		var params = {};
		if(ntcId != null){
			params.ntcId = ntcId;
		} else {
			params.cntnSeCd = cntnSeCd;
		}

		ajax.get({
			url : ctrl.urls.getInfo,
			data : params,
			success : (resp) => {

				let dialogName = "ntcInfoDialog";
				let dialogId = dialogName+"-"+uuid();

				dialog.open({
					id : dialogId,
					title : "공지사항 상세",
					size : "xl",
					content : resp,
					init : () => {
						$("#"+dialogId).attr("name", dialogName);
						$("#"+dialogId).attr("data-ref-doctx","${pageName}");

						var parentRes = new Object();
						var childReq = pageObject.childReq.pop();

						for(var reqKey in childReq) {
						    if($P.provide[reqKey]){
						    	parentRes[reqKey] = $P.provide[reqKey];
						    } else {
						    	parentRes[reqKey] = function(){};
						    }
						}

						pageObject.parentRes.push(parentRes);
					}
				});

			}
		});
	}

	$P.provide = {
		"refreshList" : function(){
    		$P.refreshNtcList();
    	},
	};

	/**************************************************************************
	* element.on
	**************************************************************************/
	$P.$findn("btnReset").on('click', () => $P.fnReset());
	$P.$findn("btnSearch").on('click', () => $P.searchNtcList());
	$P.$findn("btnAdd").on('click', () => $P.createNtc('01'));
	$P.$findn("btnAdd2").on('click', () => $P.createNtc('02'));

	/**************************************************************************
	* 초기화
	**************************************************************************/
	Componentization.fnMakeResizableTable($P.findn("table-responsive"));
	Componentization.fnMakeScrollableTable($P.findn("table-responsive"), $P.scrollNtcList);

	$P.fnReset();
    fn_securityModeToggle($("#securityMode--top").is(":checked")); //보안모드

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

</script>
