<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ include file="/jsp/common/include/localemessage.jsp" %>
<%
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setHeader("Expires", "0");
%>
<html>
<head>
	<title>FAQ 상세</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<jsp:include page="/jsp/common/include/css.jsp"/>
	<link rel="stylesheet" type="text/css" href="<c:url value="/css/editor-content.css"/>"/>
	<jsp:include page="/jsp/common/include/script.jsp"/>
	<script language="javascript">
		var url = '<c:url value="/onl/apim/portalfaq/portalFaqMan.json" />';
		var url_view = '<c:url value="/onl/apim/portalfaq/portalFaqMan.view" />';
		var isDetail = false;

		function decodeHTMLEntities(text) {
			var textArea = document.createElement('textarea');
			textArea.innerHTML = text;
			return textArea.value;
		}

		function detail(key) {
			if (!isDetail) return;
			$.ajax({
				type: "POST",
				url: url,
				dataType: "json",
				data: {cmd: 'DETAIL', id: key},
				success: function (json) {
					var data = json;

					$("#id").val(key);
					$("#faqQuestion").val(data.faqQuestion);
					$("#useYn").prop("checked", data.useYn === "Y");

					var decodedContent = decodeHTMLEntities(data.faqAnswer);
					$('#faqAnswer').summernote('code', decodedContent);

					$("#createdByName").text(data.createdByName);
					$("#createdDate").text(timeStampFormat(data.createdDate));
					$("#lastModifiedDate").text(timeStampFormat(data.lastModifiedDate));
				},
				error: function (e) {
					alert(e.responseText);
				}
			});
		}

		$(document).ready(function() {
			var returnUrl = getReturnUrlForReturn();
			var key = "${param.id}";
			isDetail = key != "" && key != "null";

			if (isDetail) {
				$("#title").append(" 수정");
				buttonControl(true);
			} else {
				$("#title").append(" 등록");
				$("#btn_modify").html('<i class="material-icons">save</i> <%= localeMessage.getString("button.register") %>');
				buttonControl(false);
			}

			// Summernote 에디터 초기화 (이미지 붙여넣기 data-uri 방식, 리사이징 지원)
			initSummernote('#faqAnswer', {
				placeholder: '여기에 답변을 입력하세요.',
				height: 300
			});

			if (key) {
				detail(key);
			}

			$("#btn_modify").click(function() {
				if (!checkRequired("ajaxForm")) return;

				// Summernote 에디터 내용 검증
				var faqAnswer = $('#faqAnswer').summernote('code');
				if (faqAnswer === "" || faqAnswer === "<p><br></p>" || faqAnswer === "<p></p>") {
					alert("답변을 입력하여 주십시오.");
					$('#faqAnswer').summernote('focus');
					return;
				}

				if (confirm("<%= localeMessage.getString("common.checkSave")%>") != true)
					return;

				var postData = $('#ajaxForm').serializeArray();
				postData = postData.filter(item => item.name !== "useYn");
				postData.push({name: "useYn", value: $("#useYn").is(":checked") ? "Y" : "N"});

				if (isDetail) {
					postData.push({ name: "cmd", value: "UPDATE" });
				} else {
					postData.push({ name: "cmd", value: "INSERT" });
				}

				$.ajax({
					type: "POST",
					url: url,
					data: postData,
					success: function (json) {
						alert("<%= localeMessage.getString("common.saveMsg") %>");
						goNav(returnUrl);
					},
					error: function (e) {
						alert(e.responseText);
					}
				});
			});

			$("#btn_delete").click(function() {
				if (confirm("<%= localeMessage.getString("common.confirmMsg")%>")) {
					var postData = $('#ajaxForm').serializeArray();
					postData.push({name: "cmd", value: "DELETE"});

					$.ajax({
						type: "POST",
						url: url,
						data: postData,
						success: function (args) {
							alert("<%= localeMessage.getString("common.deleteMsg") %>");
							goNav(returnUrl);
						},
						error: function (e) {
							alert(e.responseText);
						}
					});
				}
			});

			$("#btn_previous").click(function() {
				goNav(returnUrl);
			});
		});
	</script>
</head>
<body>
<div class="right_box">
	<div class="content_top">
		<ul class="path">
			<li><a href="#">${rmsMenuPath}</a></li>
		</ul>
	</div><!-- end content_top -->
	<div class="content_middle">
		<div class="search_wrap">
			<button type="button" class="cssbtn" id="btn_modify" level="W" status="DETAIL,NEW"><i class="material-icons">save</i> <%= localeMessage.getString("button.modify") %></button>
			<button type="button" class="cssbtn" id="btn_delete" level="W" status="DETAIL"><i class="material-icons">delete</i> <%= localeMessage.getString("button.delete") %></button>
			<button type="button" class="cssbtn" id="btn_previous" level="R" status="DETAIL,NEW"><i class="material-icons">arrow_back</i> <%= localeMessage.getString("button.previous") %></button>
		</div>
		<div class="title" id="title">FAQ</div>
		<form id="ajaxForm">
			<input type="hidden" name="id" id="id">
			<table class="table_row" cellspacing="0">
				<tr>
					<th style="width:20%;">사용여부</th>
					<td>
						<input type="checkbox" name="useYn" id="useYn" value="Y" ${portalFaqUI.useYn eq 'Y' ? 'checked' : ''}> 사용
					</td>
				</tr>
				<tr>
					<th style="width:20%;">질문 <font color="red">*</font></th>
					<td><input type="text" id="faqQuestion" name="faqQuestion" style="width:100%" data-required data-warning="질문을 입력하여 주십시오."/></td>
				</tr>
				<tr>
					<th style="width:20%;">답변 <font color="red">*</font></th>
					<td>
						<textarea id="faqAnswer" name="faqAnswer" style="width:100%;height:300px" data-required data-warning="답변을 입력하여 주십시오."></textarea>
					</td>
				</tr>
				<tr>
					<th style="width:20%;">등록자</th>
					<td id="createdByName"></td>
				</tr>
				<tr>
					<th style="width:20%;">등록일</th>
					<td id="createdDate"></td>
				</tr>
				<tr>
					<th style="width:20%;">최종수정일</th>
					<td id="lastModifiedDate"></td>
				</tr>
			</table>
		</form>
	</div>
</div>
</body>
</html>