<%@ 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="system-logs-title">
        <div>
            <p class="eyebrow">系统维护</p>
            <h1 id="system-logs-title">系统日志</h1>
            <p>查看管理账户变更和维护审计记录。</p>
        </div>
    </section>

    <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 system-log-search-form"
              action="${pageContext.request.contextPath}/admin/system-logs" method="get">
            <div class="search-field">
                <label for="operationType">操作</label>
                <select id="operationType" name="operationType">
                    <option value="">全部操作</option>
                    <c:forEach var="operationType" items="${operationTypes}">
                        <option value="${fn:escapeXml(operationType)}"
                                <c:if test="${criteria.operationType == operationType}">selected</c:if>>
                            <c:choose>
                                <c:when test="${operationType == 'user.create'}">创建用户</c:when>
                                <c:when test="${operationType == 'user.update'}">更新用户</c:when>
                                <c:when test="${operationType == 'user.deactivate'}">停用用户</c:when>
                                <c:otherwise><c:out value="${operationType}" /></c:otherwise>
                            </c:choose>
                        </option>
                    </c:forEach>
                    <c:if test="${not empty criteria.operationType and empty operationTypes}">
                        <option value="${fn:escapeXml(criteria.operationType)}" selected>
                            <c:out value="${criteria.operationType}" />
                        </option>
                    </c:if>
                </select>
                <c:if test="${not empty errors.operationType}">
                    <span class="field-error"><c:out value="${errors.operationType}" /></span>
                </c:if>
            </div>

            <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="createdFrom">开始日期</label>
                <input id="createdFrom" name="createdFrom" type="date"
                       value="${fn:escapeXml(criteria.createdFromText)}">
                <c:if test="${not empty errors.createdFrom}">
                    <span class="field-error"><c:out value="${errors.createdFrom}" /></span>
                </c:if>
            </div>

            <div class="search-field">
                <label for="createdTo">结束日期</label>
                <input id="createdTo" name="createdTo" type="date"
                       value="${fn:escapeXml(criteria.createdToText)}">
                <c:if test="${not empty errors.createdTo}">
                    <span class="field-error"><c:out value="${errors.createdTo}" /></span>
                </c:if>
            </div>

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

    <section class="table-panel" aria-labelledby="system-log-results-title">
        <h2 id="system-log-results-title">日志记录</h2>
        <c:choose>
            <c:when test="${empty logs}">
                <p class="empty-state">没有符合当前筛选条件的系统日志。</p>
            </c:when>
            <c:otherwise>
                <div class="table-scroll">
                    <table class="data-table system-log-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">IP 地址</th>
                            <th scope="col">详情</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach var="log" items="${logs}">
                            <tr>
                                <td><c:out value="${log.createdAtText}" /></td>
                                <td>
                                    <div><c:out value="${log.operatorLabel}" /></div>
                                    <c:if test="${not empty log.operatorMetaText}">
                                        <div class="muted-text"><c:out value="${log.operatorMetaText}" /></div>
                                    </c:if>
                                </td>
                                <td>
                                    <c:choose>
                                        <c:when test="${log.operationType == 'user.create'}">创建用户</c:when>
                                        <c:when test="${log.operationType == 'user.update'}">更新用户</c:when>
                                        <c:when test="${log.operationType == 'user.deactivate'}">停用用户</c:when>
                                        <c:otherwise><c:out value="${log.operationType}" /></c:otherwise>
                                    </c:choose>
                                </td>
                                <td>
                                    <c:out value="${log.targetTableName}" />
                                    <c:if test="${not empty log.targetId}">
                                        #<c:out value="${log.targetId}" />
                                    </c:if>
                                </td>
                                <td>
                                    <span class="status-pill status-${log.resultStatusCode}">
                                        <c:out value="${log.resultStatusName}" />
                                    </span>
                                </td>
                                <td><c:out value="${log.requestIp}" /></td>
                                <td><c:out value="${log.message}" /></td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                </div>
            </c:otherwise>
        </c:choose>
    </section>
</main>
</body>
</html>
