<%@ 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-title">
        <div>
            <p class="eyebrow">图书管理</p>
            <h1 id="manage-title">管理图书</h1>
            <p>创建、更新、删除和查看馆藏记录的库存信息。</p>
        </div>
        <div class="hero-actions">
            <a class="button button-primary" href="${pageContext.request.contextPath}/books/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}/books" 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="title">书名</label>
                <input id="title" name="title" type="text" value="${fn:escapeXml(criteria.title)}">
            </div>

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

            <div class="search-field">
                <label for="categoryId">分类</label>
                <select id="categoryId" name="categoryId">
                    <option value="">全部分类</option>
                    <c:forEach var="category" items="${categories}">
                        <option value="${category.id}" <c:if test="${criteria.categoryId == category.id}">selected</c:if>>
                            <c:out value="${category.name}" />
                        </option>
                    </c:forEach>
                </select>
                <c:if test="${not empty errors.categoryId}">
                    <span class="field-error"><c:out value="${errors.categoryId}" /></span>
                </c:if>
            </div>

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

    <section class="table-panel" aria-labelledby="management-results-title">
        <h2 id="management-results-title">图书记录</h2>
        <c:choose>
            <c:when test="${empty books}">
                <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="book" items="${books}">
                            <tr>
                                <td><c:out value="${book.identifier}" /></td>
                                <td><c:out value="${book.title}" /></td>
                                <td><c:out value="${book.author}" /></td>
                                <td><c:out value="${book.categoryName}" /></td>
                                <td><c:out value="${book.availableCopies}" /> / <c:out value="${book.totalCopies}" /></td>
                                <td>
                                    <span class="status-pill status-${book.status.code}">
                                        <c:out value="${book.status.displayName}" />
                                    </span>
                                </td>
                                <td>
                                    <div class="table-actions">
                                        <a class="button button-secondary"
                                           href="${pageContext.request.contextPath}/books/edit?id=${book.id}">编辑</a>
                                        <form action="${pageContext.request.contextPath}/books/delete"
                                              method="post"
                                              onsubmit="return confirm('确定删除这条图书记录吗？');">
                                            <input type="hidden" name="id" value="${book.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>
