<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.util.List"%>
<%@ page import="com.eactive.eai.rms.common.acl.sitemap.ui.SitemapNode"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%!
	private void printNodeLabel(javax.servlet.jsp.JspWriter out, SitemapNode node) throws java.io.IOException {
		String url = node.getMenuUrl();
		boolean hasLink = url != null && !url.trim().isEmpty() && !"NAN".equalsIgnoreCase(url.trim());
		if (hasLink) {
			out.print("<a href=\"javascript:void(0);\" class=\"sitemap-name\" onclick=\"goPage('"
					+ escapeJs(url) + "','" + escapeJs(node.getMenuId()) + "');return false;\">"
					+ escapeHtml(node.getMenuName()) + "</a>");
		} else {
			out.print("<span class=\"sitemap-name\">" + escapeHtml(node.getMenuName()) + "</span>");
		}
	}

	private void printSitemapNode(javax.servlet.jsp.JspWriter out, SitemapNode node) throws java.io.IOException {
		out.print("<li>");
		printNodeLabel(out, node);

		List<SitemapNode> children = node.getChildren();
		if (children != null && !children.isEmpty()) {
			out.print("<ul>");
			for (SitemapNode child : children) {
				printSitemapNode(out, child);
			}
			out.print("</ul>");
		}
		out.print("</li>");
	}

	private String escapeHtml(String value) {
		if (value == null) {
			return "";
		}
		return value.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;");
	}

	private String escapeJs(String value) {
		if (value == null) {
			return "";
		}
		return value.replace("\\", "\\\\").replace("'", "\\'");
	}
%>
<%
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setHeader("Expires", "0");

	List<SitemapNode> sitemapTree = (List<SitemapNode>) request.getAttribute("sitemapTree");
%>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<jsp:include page="/jsp/common/include/css.jsp"/>
<jsp:include page="/jsp/common/include/script.jsp"/>
<style>
	.sitemap-columns { display: flex; flex-wrap: wrap; gap: 40px; }
	.sitemap-column { min-width: 180px; }
	.sitemap-category { font-size: 1.1em; font-weight: bold; padding-bottom: 6px; margin-bottom: 8px; border-bottom: 2px solid #ccc; }
	.sitemap-tree, .sitemap-tree ul { list-style-type: disc; margin: 0; padding-left: 18px; }
	.sitemap-tree { padding-left: 0; list-style-type: none; }
	.sitemap-tree li { line-height: 1.8; }
	a.sitemap-name { text-decoration: none; cursor: pointer; }
	a.sitemap-name:hover { text-decoration: underline; }
</style>
<script language="javascript">
	function goPage(url, menuId) {
		var serviceType = sessionStorage["serviceType"];

		var pageUrl = url;
		pageUrl += (pageUrl.indexOf("?") > -1 ? "&" : "?") + "menuId=" + menuId;
		pageUrl += "&serviceType=" + serviceType;

		parent.leftFrame.location.href = '<c:url value="/leftMenu.do"/>?menuId=' + menuId + '&serviceType=' + serviceType;
		location.href = pageUrl;
	}
</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="title">사이트맵</div> -->
				<div class="sitemap-columns">
<%
	for (SitemapNode category : sitemapTree) {
%>
					<div class="sitemap-column">
						<div class="sitemap-category">
<%
		printNodeLabel(out, category);
%>
						</div>
						<ul class="sitemap-tree">
<%
		List<SitemapNode> children = category.getChildren();
		if (children != null) {
			for (SitemapNode child : children) {
				printSitemapNode(out, child);
			}
		}
%>
						</ul>
					</div>
<%
	}
%>
				</div>
			</div><!-- end content_middle -->
		</div><!-- end right_box -->
	</body>
</html>
