<%@ 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-readers-title">
        <div>
            <p class="eyebrow">读者档案</p>
            <h1 id="manage-readers-title">读者档案与借阅资格</h1>
            <p>维护读者资料、联系方式、借阅上限和借阅资格；登录账户、角色和启用状态请在用户管理中处理。</p>
        </div>
        <div class="hero-actions">
            <a class="button button-primary" href="${pageContext.request.contextPath}/readers/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}/readers" method="get">
            <div class="search-field">
                <label for="identifier">读者编号</label>
                <input id="identifier" name="identifier" type="text" value="${fn:escapeXml(criteria.identifier)}">
            </div>

            <div class="search-field">
                <label for="name">姓名</label>
                <input id="name" name="name" type="text" value="${fn:escapeXml(criteria.name)}">
            </div>

            <div class="search-field">
                <label for="contact">电话或邮箱</label>
                <input id="contact" name="contact" type="text" value="${fn:escapeXml(criteria.contact)}">
            </div>

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

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

    <section class="table-panel" aria-labelledby="reader-results-title">
        <h2 id="reader-results-title">读者档案</h2>
        <c:choose>
            <c:when test="${empty readers}">
                <p class="empty-state">没有符合当前筛选条件的读者档案。</p>
            </c:when>
            <c:otherwise>
                <div class="table-scroll">
                    <table class="data-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="reader" items="${readers}">
                            <tr>
                                <td><c:out value="${reader.identifier}" /></td>
                                <td><c:out value="${reader.fullName}" /></td>
                                <td>
                                    <c:if test="${not empty reader.phone}">
                                        <div><c:out value="${reader.phone}" /></div>
                                    </c:if>
                                    <c:if test="${not empty reader.email}">
                                        <div><c:out value="${reader.email}" /></div>
                                    </c:if>
                                </td>
                                <td>
                                    <c:choose>
                                        <c:when test="${not empty reader.username}">
                                            <c:out value="${reader.username}" />
                                        </c:when>
                                        <c:otherwise>未关联</c:otherwise>
                                    </c:choose>
                                </td>
                                <td><c:out value="${reader.maxBorrowCount}" /></td>
                                <td>
                                    <span class="status-pill status-${reader.status.code}">
                                        <c:out value="${reader.status.displayName}" />
                                    </span>
                                </td>
                                <td>
                                    <div class="table-actions">
                                        <a class="button button-secondary"
                                           href="${pageContext.request.contextPath}/readers/edit?id=${reader.id}">编辑</a>
                                        <form action="${pageContext.request.contextPath}/readers/delete"
                                              method="post"
                                              onsubmit="return confirm('确定停用这个读者档案吗？');">
                                            <input type="hidden" name="id" value="${reader.id}">
                                            <button class="button button-danger" type="submit">停用</button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                </div>
            </c:otherwise>
        </c:choose>
    </section>
</main>
</body>
</html>
