<%@ 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="borrowing-title">
        <div>
            <p class="eyebrow">借阅管理</p>
            <h1 id="borrowing-title">管理借阅</h1>
            <p>创建借阅记录、处理归还、续借有效借阅并查看逾期项目。</p>
        </div>
        <a class="button button-primary" href="${pageContext.request.contextPath}/borrowing/new">新增借阅</a>
    </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 borrowing-search-form" action="${pageContext.request.contextPath}/borrowing" method="get">
            <div class="search-field">
                <label for="readerIdentifier">读者编号</label>
                <input id="readerIdentifier" name="readerIdentifier" type="text"
                       value="${fn:escapeXml(criteria.readerIdentifier)}">
            </div>

            <div class="search-field">
                <label for="bookIdentifier">图书编号</label>
                <input id="bookIdentifier" name="bookIdentifier" type="text"
                       value="${fn:escapeXml(criteria.bookIdentifier)}">
            </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>
                    <option value="${overdueStatus}" <c:if test="${criteria.statusCode == overdueStatus}">selected</c:if>>
                        逾期
                    </option>
                </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}/borrowing">清空</a>
        </form>
    </section>

    <section class="table-panel" aria-labelledby="borrowing-results-title">
        <h2 id="borrowing-results-title">借阅记录</h2>
        <c:choose>
            <c:when test="${empty borrowRecords}">
                <p class="empty-state">没有符合当前筛选条件的借阅记录。</p>
            </c:when>
            <c:otherwise>
                <div class="table-scroll">
                    <table class="data-table borrowing-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>
                            <th scope="col">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach var="record" items="${borrowRecords}">
                            <tr>
                                <td>
                                    <strong><c:out value="${record.readerIdentifier}" /></strong>
                                    <div class="muted-text"><c:out value="${record.readerName}" /></div>
                                </td>
                                <td>
                                    <strong><c:out value="${record.bookIdentifier}" /></strong>
                                    <div class="muted-text"><c:out value="${record.bookTitle}" /></div>
                                </td>
                                <td><c:out value="${record.borrowedAtText}" /></td>
                                <td><c:out value="${record.dueAtText}" /></td>
                                <td>
                                    <c:choose>
                                        <c:when test="${not empty record.returnedAtText}">
                                            <c:out value="${record.returnedAtText}" />
                                        </c:when>
                                        <c:otherwise>未归还</c:otherwise>
                                    </c:choose>
                                </td>
                                <td><c:out value="${record.renewalCount}" /> / <c:out value="${maxRenewals}" /></td>
                                <td>
                                    <span class="status-pill status-${record.displayStatusCode}">
                                        <c:out value="${record.displayStatusName}" />
                                    </span>
                                </td>
                                <td>
                                    <c:choose>
                                        <c:when test="${record.status.code == 'active'}">
                                            <div class="table-actions">
                                                <form action="${pageContext.request.contextPath}/borrowing/return"
                                                      method="post"
                                                      onsubmit="return confirm('确定归还这本书吗？');">
                                                    <input type="hidden" name="id" value="${record.id}">
                                                    <button class="button button-secondary" type="submit">归还</button>
                                                </form>
                                                <c:if test="${record.renewalCount < maxRenewals}">
                                                    <form action="${pageContext.request.contextPath}/borrowing/renew"
                                                          method="post"
                                                          onsubmit="return confirm('确定续借这条记录吗？');">
                                                        <input type="hidden" name="id" value="${record.id}">
                                                        <button class="button button-secondary" type="submit">续借</button>
                                                    </form>
                                                </c:if>
                                            </div>
                                        </c:when>
                                        <c:otherwise>
                                            <span class="muted-text">已完成</span>
                                        </c:otherwise>
                                    </c:choose>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                </div>
            </c:otherwise>
        </c:choose>
    </section>
</main>
</body>
</html>
