<%@ page 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" %>
<header class="app-header ${not empty sessionScope.authenticatedUser ? 'app-header-auth' : 'app-header-public'}">
    <c:choose>
        <c:when test="${not empty sessionScope.authenticatedUser}">
            <c:set var="currentPath" value="${requestScope['javax.servlet.forward.servlet_path']}" />
            <c:if test="${empty currentPath}">
                <c:set var="currentPath" value="${pageContext.request.servletPath}" />
            </c:if>
            <aside class="app-sidebar" aria-label="主导航">
                <a class="sidebar-brand" href="${pageContext.request.contextPath}/dashboard">
                    <span class="brand-text">图书管理系统</span>
                </a>

                <nav class="side-nav" aria-label="模块导航">
                    <c:if test="${sessionScope.userRole == 'administrator' or sessionScope.userRole == 'librarian'}">
                        <a class="side-nav-link ${currentPath == '/reports' ? 'is-active' : ''}"
                           href="${pageContext.request.contextPath}/reports">
                            <span class="nav-text">报表中心</span>
                        </a>
                    </c:if>
                    <a class="side-nav-link ${currentPath == '/catalog' ? 'is-active' : ''}"
                       href="${pageContext.request.contextPath}/catalog">
                        <span class="nav-text">馆藏检索</span>
                    </a>
                    <c:if test="${sessionScope.userRole == 'administrator' or sessionScope.userRole == 'librarian'}">
                        <a class="side-nav-link ${(currentPath == '/books' or fn:startsWith(currentPath, '/books/')) ? 'is-active' : ''}"
                           href="${pageContext.request.contextPath}/books">
                            <span class="nav-text">图书管理</span>
                        </a>
                        <a class="side-nav-link ${(currentPath == '/book-categories' or fn:startsWith(currentPath, '/book-categories/')) ? 'is-active' : ''}"
                           href="${pageContext.request.contextPath}/book-categories">
                            <span class="nav-text">图书分类管理</span>
                        </a>
                        <a class="side-nav-link ${(currentPath == '/readers' or fn:startsWith(currentPath, '/readers/')) ? 'is-active' : ''}"
                           href="${pageContext.request.contextPath}/readers">
                            <span class="nav-text">读者档案</span>
                        </a>
                        <a class="side-nav-link ${(currentPath == '/borrowing' or fn:startsWith(currentPath, '/borrowing/')) ? 'is-active' : ''}"
                           href="${pageContext.request.contextPath}/borrowing">
                            <span class="nav-text">借阅流通</span>
                        </a>
                    </c:if>
                    <c:if test="${sessionScope.userRole == 'reader'}">
                        <a class="side-nav-link ${(currentPath == '/reader/loans' or fn:startsWith(currentPath, '/reader/loans/')) ? 'is-active' : ''}"
                           href="${pageContext.request.contextPath}/reader/loans">
                            <span class="nav-text">读者借阅历史</span>
                        </a>
                    </c:if>
                    <c:if test="${sessionScope.userRole == 'administrator'}">
                        <a class="side-nav-link ${(currentPath == '/admin/users' or fn:startsWith(currentPath, '/admin/users/')) ? 'is-active' : ''}"
                           href="${pageContext.request.contextPath}/admin/users">
                            <span class="nav-text">用户账户</span>
                        </a>
                        <a class="side-nav-link ${(currentPath == '/admin/system-logs' or fn:startsWith(currentPath, '/admin/system-logs/')) ? 'is-active' : ''}"
                           href="${pageContext.request.contextPath}/admin/system-logs">
                            <span class="nav-text">系统日志</span>
                        </a>
                    </c:if>
                </nav>

                <div class="sidebar-footer">
                    <a href="${pageContext.request.contextPath}/logout">退出登录</a>
                </div>
            </aside>

            <div class="app-topbar">
                <div class="breadcrumb">已登录 <span>/</span> 工作台</div>
                <form class="global-search" action="${pageContext.request.contextPath}/catalog" method="get">
                    <label class="sr-only" for="globalTitle">搜索图书、读者、功能</label>
                    <input id="globalTitle" name="title" type="search" placeholder="搜索图书、读者、功能...">
                    <button type="submit" aria-label="搜索">搜</button>
                </form>
                <div class="topbar-actions">
                    <span class="user-summary">
                        <span class="user-meta">
                            <span class="user-pill">
                                <c:out value="${sessionScope.authenticatedUser.displayName}" />
                            </span>
                            <span class="role-label">
                                <c:out value="${sessionScope.authenticatedUser.role.displayName}" />
                            </span>
                        </span>
                    </span>
                </div>
            </div>
        </c:when>
        <c:otherwise>
            <a class="auth-brand" href="${pageContext.request.contextPath}/dashboard">MZH 图书馆</a>
        </c:otherwise>
    </c:choose>
</header>
