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

<!-- Page Body -->
<div class="content-wrapper">
	<div class="d-flex flex-row justify-content-evenly">
		<!-- 민원 상담 정보 -->
		<div class="col-xl-12 mx-1">
			<div class="card">
				<!-- 업무 버튼 -->
				<div class="container-page-btn">
					<label class="w-px-120 bg-label-danger pe-2 col-form-label text-sm-center">민원상담 정보</label>
					<!-- 버튼 우측 정렬 -->
					<span class="container-window-btn-right">
						<button type="button" class="btn btn-primary" id="btnCreate--${pageName}" title="민원상담 등록" onclick="fnCreate${pageName}();">
							민원상담 등록
						</button>
						<button type="button" class="btn btn-primary" id="btnUpdate--${pageName}" title="민원상담 수정" onclick="fnUpdate${pageName}();">
							민원상담 수정
						</button>
						<button type="button" class="btn btn-primary" id="btnRemove--${pageName}" title="민원상담 삭제" onclick="fnRemove${pageName}();">
							민원상담 삭제
						</button>
					</span> <!-- <span class="container-window-btn-right"> -->
				</div> <!-- <div class="container-page-btn"> -->

				<!-- 입력 영역 -->
				<form id="frmEdit--${pageName}" name="frmEdit">
					<!-- hidden -->
					<input type="hidden" id="cvlcptDscsnId--${pageName}" name="cvlcptDscsnId" data-map="CVLCPT_DSCSN_ID" />

					<div class="row g-1">
						<!-- DataTables(그리드) -->
						<div class="card-datatable text-nowrap">
							<div id="DataTables_Table_0_wrapper--${pageName}" class="dataTables_wrapper dt-bootstrap5 no-footer">
								<div id="table-responsive--${pageName}" class="table-responsive" style="overflow-x:scroll; height:336px; overflow-y:scroll;" >
									<table class="dataTable datatables-ajax table table-bordered no-footer" id="DataTables_Table_0--${pageName}" aria-describedby="DataTables_Table_0_info">
										<thead>
											<tr id="theadTr--${pageName}">
												<th>등록일시</th>
												<th>요일</th>
												<th>등록사용자</th>
												<th>민원접수구분</th>
												<th>상담내용</th>
												<th>수정일시</th>
												<th>수정사용자</th>
											</tr>
										</thead>
										<tbody id="tbody--${pageName}">
										</tbody>
										<template id="${infoPrefix}Row--${pageName}">
											<tr data-key="{CVLCPT_DSCSN_ID}">
												<td class="text-center" onclick="{onclick}">{REG_DT}</td>
												<td class="text-center" onclick="{onclick}">{WEEK_NM}</td>
												<td class="text-center" onclick="{onclick}">{RGTR_NM}</td>
												<td class="text-center" onclick="{onclick}">{CVLCPT_RCPT_SE_NM}</td>
												<td class="text-center" onclick="{onclick}">{DSCSN_CN}</td>
												<td class="text-center" onclick="{onclick}">{MDFCN_DT}</td>
												<td class="text-center" onclick="{onclick}">{MDFR_NM}</td>
											</tr>
										</template>
										<template id="${infoPrefix}NotFound--${pageName}">
											<tr class="odd">
												<td valign="top" colspan="7" class="dataTables_empty text-center">민원 상담 정보를 찾지 못했습니다.</td>
											</tr>
										</template>
									</table>
								</div>
							</div>
						</div> <!-- / DataTables(그리드) -->
						<!-- 상담 내용 -->
						<div class="col-md-12">
							<textarea class="form-control w-100" style="font-size:x-large;" id="dscsnCn--${pageName}" name="dscsnCn" data-map="DSCSN_CN" rows="5" data-maxlengthb="4000" readonly>
							</textarea>
						</div>
					</div> <!-- <div class="row g-1"> -->
				</form>
			</div> <!-- <div class="card"> -->
		</div> <!-- 민원 상담 정보 <div class="col-xl-12 mx-1"> -->
	</div> <!-- <div class="d-flex flex-row justify-content-evenly"> -->
</div>

<script>
	/**************************************************************************
	 * Global Variable
	 **************************************************************************/
	// URL
	var ${pageName}PrefixUrl = "/sprt/sprt04";
	// FormFields
 	var ${pageName}Fields = new FormFields("#frmEdit--${pageName}");

	/**************************************************************************
	 * DatasetControl
	 **************************************************************************/
	var ${pageName}Control = new DatasetControl({
		  prefix : "cvlcptDscsnCn"
		, prefixName : "민원상담내용"
		, infoSize : "lg"
		, keymapper : info => info ? info.CVLCPT_DSCSN_ID : ""
		, dataGetter : obj => obj.cvlcptDscsnCnList
		, appendData : true
		, formats : {
			  REG_DT : datetimeFormat
			, MDFCN_DT : datetimeFormat
		}
	});

	/**************************************************************************
	 * DatasetControl 이벤트
	 **************************************************************************/
	// Dataset 변경 이벤트
	${pageName}Control.onDatasetChange = obj => {
		renderList${pageName}();
	}

	// 현재 선택 자료 변경 이벤트
	${pageName}Control.onCurrentChange = item => {
		if (!item) return;

		let key = item.data.CVLCPT_DSCSN_ID;

		$("#tbody--${pageName}").setCurrentRow(key);

		$("#dscsnCn--${pageName}").val(item.data.DSCSN_CN);						// 상담 내용
	};

	// 민원 상담 내용 Dialog
	${pageName}Control.getInfo = (params) => {
		let dialogTitle = "";

		if (params.callPurpose == "create") {
			dialogTitle = ${pageName}Control.prefixName + " 등록";
		} else if (params.callPurpose == "update") {
			dialogTitle = ${pageName}Control.prefixName + " 수정";
		} else {
			return;
		}

		ajax.get({
			  url : wctx.url("/" + params.taskSeCd + ${pageName}PrefixUrl + "/020/info.do")
			, data : params || {}
			, success : resp => {
				dialog.open({
					  id : ${pageName}Control.prefixed("Dialog")
					, title : dialogTitle
					, content : resp
					, size : "lg"
					, init : () => { }
					, onClose : () => { refreshDataInfo${pageName}(); }			// callback 자료 재조회
				});
			}
		});
	}

	// 삭제 callback
	${pageName}Control.onRemove = (resp) => {
  		let btnTitle = $("#btnRemove--${pageName}").attr("title");
  		let showMessage = resp.retMessage.replace(/[S]|[F]/g, btnTitle);

		dialog.alert(showMessage);

		if (resp.saved) {
			refreshDataInfo${pageName}();										// 자료 재조회
		}
	}

	// 삭제
	${pageName}Control.remove = (params) => {
		ajax.post({
			  url : wctx.url("/" + params.taskSeCd + ${pageName}PrefixUrl + "/010/remove.do")
			, data : params || {}
			, success : resp => ${pageName}Control.onRemove(resp)				// 삭제 callback
		});
	}

	/**************************************************************************
	 * function
	 **************************************************************************/
	// DataTables에 click 이벤트
	renderList${pageName} = () => {
		let ${infoPrefix}List = ${pageName}Control.dataset;
		let empty = ${infoPrefix}List.empty;

		let trs = empty ?
			[document.getElementById("${infoPrefix}NotFound--${pageName}").innerHTML] : <%-- from template#${infoPrefix}NotFound --%>
			${infoPrefix}List.inStrings(
				document.getElementById("${infoPrefix}Row--${pageName}").innerHTML, <%-- from template#${infoPrefix}Row --%>
				(str, dataItem) => str
					.replace(/{onclick}/gi, "${pageName}Control.setCurrent('" + dataItem.getValue("CVLCPT_DSCSN_ID") + "');")
			);

		$("#tbody--${pageName}").html(trs.join());
	}

	// 자료 재조회
	refreshDataInfo${pageName} = () => {
		let crdnId = $("#crdnId--${pageNameMain}").val();
		let vhrno = $("#vhrno--${pageNameMain}").val();

		// 단속 ID 가 없다면.. return
		if (crdnId == null || crdnId == "") {
			return;
		}

		let params = {
			  callPurpose : "view"
			, inquiryCondition : "crdnId_vhrno"
			, crdnId : crdnId
			, vhrno : vhrno
			, delYn : "N"
		};

		ajax.get({
			  url : wctx.url("/sprt/sprt02/080/info.do")
			, headers: { Accept: "application/json; charset=utf-8" }			// json
			, data : params || {}
			, success : resp => {
				${pageName}Control.dataset.clear();
				${pageName}Control.setData(resp.cvlcptDscsnCn);					// Dataset 셋팅
			}
		});
	}

	/**************************************************************************
	 * 초기 셋팅
	 **************************************************************************/
	// 검색조건 초기값 셋팅
	initForm${pageName} = () => {

	}

	// 이벤트 설정
	setEvent${pageName} = () => {

	}

	/**************************************************************************
	 * 버튼 clickEvent
	 **************************************************************************/
	// 민원 상담 내용 등록
	fnCreate${pageName} = () => {
		let crdnId = $("#crdnId--${pageNameMain}").val();
		let sggCd = $("#sggCd--${pageNameMain}").val();
		let taskSeCd = $("#taskSeCd--${pageNameMain}").val();

		// 단속 ID 가 없다면.. return
		if (crdnId == undefined || crdnId == "") {
			return;
		}

		let params = {
			  callPurpose : "create"
			, crdnId : crdnId
			, sggCd : sggCd
			, taskSeCd : taskSeCd
		};

		${pageName}Control.getInfo(params);
	}

	// 민원 상담 내용 수정
	fnUpdate${pageName} = () => {
		let crdnId = $("#crdnId--${pageNameMain}").val();
		let sggCd = $("#sggCd--${pageNameMain}").val();
		let taskSeCd = $("#taskSeCd--${pageNameMain}").val();
		let cvlcptDscsnId = ${pageName}Control.dataset.getValue("CVLCPT_DSCSN_ID");

		// 민원 상담 ID 가 없다면.. return
		if (cvlcptDscsnId == undefined || cvlcptDscsnId == "") {
			return;
		}

		let params = {
			  callPurpose : "update"
			, crdnId : crdnId
			, sggCd : sggCd
			, taskSeCd : taskSeCd
			, cvlcptDscsnId : cvlcptDscsnId
		};

		${pageName}Control.getInfo(params);
	}

	// 민원 상담 내용 삭제
	fnRemove${pageName} = () => {
		let crdnId = $("#crdnId--${pageNameMain}").val();
		let sggCd = $("#sggCd--${pageNameMain}").val();
		let taskSeCd = $("#taskSeCd--${pageNameMain}").val();
		let cvlcptDscsnId = ${pageName}Control.dataset.getValue("CVLCPT_DSCSN_ID");

		// 민원 상담 ID 가 없다면.. return
		if (cvlcptDscsnId == undefined || cvlcptDscsnId == "") {
			return;
		}

		let params = {
			  callPurpose : "remove"
			, crdnId : crdnId
			, sggCd : sggCd
			, taskSeCd : taskSeCd
			, cvlcptDscsnId : cvlcptDscsnId
		};

		dialog.alert({
			  content : "선택한 " + ${pageName}Control.prefixName + " 내용을 삭제하시겠습니까?"
			, onOK : () => {
				${pageName}Control.remove(params);
			}
		});
	}

	/**************************************************************************
	 * script 진입
	 **************************************************************************/
	$(document).ready(function() {
		// 이벤트 설정
		setEvent${pageName}();

		// 화면 초기 설정
		initForm${pageName}();

		// Data 셋팅
		${pageName}Control.setData(${cvlcptDscsnCn});
	});

// #sourceURL=sprt02080-info.jsp
</script>
