<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="path" uri="http://egovframework.go.kr/functions/path" %>

<%-- XssUtil 인스턴스 생성 --%>
<jsp:useBean id="xssUtil" class="egovframework.filter.XssUtil" scope="request" />

<%-- 현재 URL 경로 확실하게 가져오기 --%>
<c:set var="currentUrl" value="${requestScope['javax.servlet.forward.servlet_path']}" />
<%-- 활성화된 메뉴 정보 저장할 변수 --%>
<c:set var="activeMenuId" value="" scope="request" />
<c:set var="activeSubMenuId" value="" scope="request" />

<%-- 콘솔 디버깅 용도 (개발 환경에서만 사용) --%>
<!-- Current URL: ${currentUrl} -->

<!-- Sidebar body -->
<div class="sidebar-body">
    <ul class="nav treeview mb-4" data-accordion>
    <c:if test="${not empty sessionScope.sessionVO.menus}">
        <c:forEach var="menu" items="${sessionScope.sessionVO.menus}">
            <c:if test="${menu.useYn eq 'Y' and menu.viewYn eq 'Y'}">
                <%-- 메뉴 상태 변수 초기화 (unescape 처리) --%>
                <c:set var="menuUrl" value="${not empty menu.menuUrl ? xssUtil.unescape(menu.menuUrl) : '#'}" />
                <c:set var="menuUrlPattern" value="${not empty menu.urlPattern ? xssUtil.unescape(menu.urlPattern) : ''}" />
                <c:set var="isMenuActive" value="false" />
                <c:set var="hasActiveChild" value="false" />

                <%-- 메인 메뉴 활성화 상태 확인 (디버깅 정보 추가) --%>
                <c:if test="${not empty menuUrlPattern && menuUrl != '#'}">
                    <c:set var="patterns" value="${fn:split(menuUrlPattern, ',')}" />
                    <!-- Menu ID: ${menu.menuId}, Patterns: ${menuUrlPattern} -->
                    <c:forEach var="pattern" items="${patterns}">
                        <c:set var="trimmedPattern" value="${fn:trim(pattern)}" />
                        <!-- Checking pattern: "${trimmedPattern}" against URL: "${currentUrl}" -->
                        <c:if test="${path:match(trimmedPattern, currentUrl)}">
                            <c:set var="isMenuActive" value="true" />
                            <c:set var="activeMenuId" value="${menu.menuId}" scope="request" />
                            <!-- Pattern matched! Menu ${menu.menuId} is active -->
                        </c:if>
                    </c:forEach>
                </c:if>

                <%-- 서브메뉴 처리 및 활성화 상태 확인 --%>
                <c:if test="${not empty menu.children}">
                    <c:forEach var="subMenu" items="${menu.children}">
                        <c:if test="${subMenu.useYn eq 'Y' and subMenu.viewYn eq 'Y'}">
                            <%-- 서브메뉴 URL unescape 처리 --%>
                            <c:set var="subMenuUrl" value="${not empty subMenu.menuUrl ? xssUtil.unescape(subMenu.menuUrl) : '#'}" />
                            <c:set var="subMenuUrlPattern" value="${not empty subMenu.urlPattern ? xssUtil.unescape(subMenu.urlPattern) : ''}" />
                            <c:set var="isSubMenuActive" value="false" />

                            <%-- 서브메뉴 활성화 상태 확인 (디버깅 정보 추가) --%>
                            <c:if test="${not empty subMenuUrlPattern && subMenuUrl != '#'}">
                                <c:set var="subPatterns" value="${fn:split(subMenuUrlPattern, ',')}" />
                                <!-- SubMenu ID: ${subMenu.menuId}, Patterns: ${subMenuUrlPattern} -->
                                <c:forEach var="subPattern" items="${subPatterns}">
                                    <c:set var="trimmedSubPattern" value="${fn:trim(subPattern)}" />
                                    <!-- Checking subpattern: "${trimmedSubPattern}" against URL: "${currentUrl}" -->
                                    <c:if test="${path:match(trimmedSubPattern, currentUrl)}">
                                        <c:set var="isSubMenuActive" value="true" />
                                        <c:set var="activeSubMenuId" value="${subMenu.menuId}" scope="request" />
                                        <c:set var="hasActiveChild" value="true" />
                                        <!-- SubPattern matched! SubMenu ${subMenu.menuId} is active -->
                                    </c:if>
                                </c:forEach>
                            </c:if>
                        </c:if>
                    </c:forEach>
                </c:if>

                <%-- 메뉴 렌더링 --%>
                <li class="nav-item" data-menu-id="${menu.menuId}" data-url-pattern="${menuUrlPattern}">
                    <a href="<c:url value="${empty menuUrl ? '#' : menuUrl}" />" class="nav-link has-icon ${not empty menu.children ? 'treeview-toggle' : ''} ${isMenuActive || hasActiveChild ? 'active show' : ''}">
                        <c:if test="${not empty menu.menuIcon}"><i data-feather="${menu.menuIcon}"></i></c:if>${menu.menuNm}
                    </a>

                        <%-- 서브메뉴 렌더링 --%>
                    <c:if test="${not empty menu.children}">
                        <ul class="nav" style="${isMenuActive || hasActiveChild ? '' : ''}">
                            <c:forEach var="subMenu" items="${menu.children}">
                                <c:if test="${subMenu.useYn eq 'Y' and subMenu.viewYn eq 'Y'}">
                                    <%-- 서브메뉴 URL unescape 처리 --%>
                                    <c:set var="subMenuUrl" value="${not empty subMenu.menuUrl ? xssUtil.unescape(subMenu.menuUrl) : '#'}" />
                                    <c:set var="subMenuUrlPattern" value="${not empty subMenu.urlPattern ? xssUtil.unescape(subMenu.urlPattern) : ''}" />
                                    <c:set var="isSubMenuActive" value="${activeSubMenuId eq subMenu.menuId}" />

                                    <li class="nav-item ${isSubMenuActive ? 'show' : ''}" data-menu-id="${subMenu.menuId}" data-url-pattern="${subMenuUrlPattern}">
                                        <a href="<c:url value='${subMenuUrl}' />" class="nav-link">${subMenu.menuNm}</a>
                                    </li>
                                </c:if>
                            </c:forEach>
                        </ul>
                    </c:if>
                </li>
            </c:if>
        </c:forEach>
    </c:if>
    </ul>

</div>
<!-- /Sidebar body -->