<%@ page 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" %>
<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>用户账户管理 - MZH 图书馆</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/app.css?v=20260428-visual-shell">
</head>
<body>
<%@ include file="/WEB-INF/jsp/common/header.jspf" %>
<main class="page-shell">
    <section class="dashboard-hero catalog-hero" aria-labelledby="manage-users-title">
        <div>
            <p class="eyebrow">系统账户</p>
            <h1 id="manage-users-title">用户账户与角色</h1>
            <p>维护登录账户、角色、密码和启用状态；读者联系方式、借阅上限和资格请在读者管理中处理。</p>
        </div>
        <div class="hero-actions">
            <a class="button button-primary" href="${pageContext.request.contextPath}/admin/users/new">新增用户账户</a>
        </div>
    </section>

    <c:if test="${not empty successMessage}">
        <div class="message message-success" role="status">
            <c:out value="${successMessage}" />
        </div>
    </c:if>
    <c:if test="${not empty errorMessage}">
        <div class="message message-error" role="alert">
            <c:out value="${errorMessage}" />
        </div>
    </c:if>

    <section class="toolbar-panel" aria-label="用户账户检索">
        <form class="search-form" action="${pageContext.request.contextPath}/admin/users" method="get">
            <div class="search-field">
                <label for="keyword">关键词</label>
                <input id="keyword" name="keyword" type="text" value="${fn:escapeXml(criteria.keyword)}">
                <c:if test="${not empty errors.keyword}">
                    <span class="field-error"><c:out value="${errors.keyword}" /></span>
                </c:if>
            </div>

            <div class="search-field">
                <label for="role">角色</label>
                <select id="role" name="role">
                    <option value="">全部角色</option>
                    <c:forEach var="role" items="${roles}">
                        <option value="${role.code}" <c:if test="${criteria.roleCode == role.code}">selected</c:if>>
                            <c:out value="${role.displayName}" />
                        </option>
                    </c:forEach>
                </select>
                <c:if test="${not empty errors.role}">
                    <span class="field-error"><c:out value="${errors.role}" /></span>
                </c:if>
            </div>

            <div class="search-field">
                <label for="active">启用状态</label>
                <select id="active" name="active">
                    <option value="">全部状态</option>
                    <option value="active" <c:if test="${criteria.activeStatus == 'active'}">selected</c:if>>启用</option>
                    <option value="inactive" <c:if test="${criteria.activeStatus == 'inactive'}">selected</c:if>>停用</option>
                </select>
                <c:if test="${not empty errors.active}">
                    <span class="field-error"><c:out value="${errors.active}" /></span>
                </c:if>
            </div>

            <button class="button button-primary" type="submit">检索</button>
            <a class="button button-secondary" href="${pageContext.request.contextPath}/admin/users">清空</a>
        </form>
    </section>

    <section class="table-panel" aria-labelledby="user-results-title">
        <h2 id="user-results-title">用户账户</h2>
        <c:choose>
            <c:when test="${empty users}">
                <p class="empty-state">没有符合当前筛选条件的用户账户。</p>
            </c:when>
            <c:otherwise>
                <div class="table-scroll">
                    <table class="data-table user-table">
                        <thead>
                        <tr>
                            <th scope="col">用户名</th>
                            <th scope="col">显示名称</th>
                            <th scope="col">角色</th>
                            <th scope="col">状态</th>
                            <th scope="col">创建时间</th>
                            <th scope="col">更新时间</th>
                            <th scope="col">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach var="account" items="${users}">
                            <tr>
                                <td><c:out value="${account.username}" /></td>
                                <td><c:out value="${account.displayName}" /></td>
                                <td><c:out value="${account.role.displayName}" /></td>
                                <td>
                                    <span class="status-pill status-${account.activeStatusCode}">
                                        <c:out value="${account.activeStatusName}" />
                                    </span>
                                </td>
                                <td><c:out value="${account.createdAtText}" /></td>
                                <td><c:out value="${account.updatedAtText}" /></td>
                                <td>
                                    <div class="table-actions">
                                        <a class="button button-secondary"
                                           href="${pageContext.request.contextPath}/admin/users/edit?id=${account.id}">编辑</a>
                                        <c:choose>
                                            <c:when test="${account.id == sessionScope.authenticatedUser.id or not account.active}">
                                                <button class="button button-secondary" type="button" disabled>停用</button>
                                            </c:when>
                                            <c:otherwise>
                                                <form action="${pageContext.request.contextPath}/admin/users/deactivate"
                                                      method="post"
                                                      onsubmit="return confirm('确定停用这个用户账户吗？');">
                                                    <input type="hidden" name="id" value="${account.id}">
                                                    <button class="button button-danger" type="submit">停用</button>
                                                </form>
                                            </c:otherwise>
                                        </c:choose>
                                    </div>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                </div>
            </c:otherwise>
        </c:choose>
    </section>
</main>
</body>
</html>
