<%@ 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><c:out value="${formTitle}" /> - 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="form-panel" aria-labelledby="book-form-title">
        <p class="eyebrow">图书管理</p>
        <h1 id="book-form-title"><c:out value="${formTitle}" /></h1>

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

        <c:set var="hasFormValues" value="${not empty formValues}" />
        <c:set var="identifierValue" value="${hasFormValues ? formValues.identifier : book.identifier}" />
        <c:set var="titleValue" value="${hasFormValues ? formValues.title : book.title}" />
        <c:set var="authorValue" value="${hasFormValues ? formValues.author : book.author}" />
        <c:set var="categoryValue" value="${book.categoryId}" />
        <c:set var="totalCopiesValue" value="${hasFormValues ? formValues.totalCopies : book.totalCopies}" />
        <c:set var="availableCopiesValue" value="${hasFormValues ? formValues.availableCopies : book.availableCopies}" />
        <c:set var="statusValue" value="${hasFormValues ? formValues.status : book.status.code}" />

        <form class="book-form" action="${pageContext.request.contextPath}${formAction}" method="post" novalidate>
            <c:if test="${book.id > 0}">
                <input type="hidden" name="id" value="${book.id}">
            </c:if>

            <div class="form-grid">
                <div class="form-field">
                    <label for="identifier">图书编号</label>
                    <input id="identifier" name="identifier" type="text" value="${fn:escapeXml(identifierValue)}" required>
                    <c:if test="${not empty errors.identifier}">
                        <span class="field-error"><c:out value="${errors.identifier}" /></span>
                    </c:if>
                </div>

                <div class="form-field">
                    <label for="title">书名</label>
                    <input id="title" name="title" type="text" value="${fn:escapeXml(titleValue)}" required>
                    <c:if test="${not empty errors.title}">
                        <span class="field-error"><c:out value="${errors.title}" /></span>
                    </c:if>
                </div>

                <div class="form-field">
                    <label for="author">作者</label>
                    <input id="author" name="author" type="text" value="${fn:escapeXml(authorValue)}" required>
                    <c:if test="${not empty errors.author}">
                        <span class="field-error"><c:out value="${errors.author}" /></span>
                    </c:if>
                </div>

                <div class="form-field">
                    <label for="categoryId">分类</label>
                    <select id="categoryId" name="categoryId" required>
                        <option value="">请选择分类</option>
                        <c:forEach var="category" items="${categories}">
                            <option value="${category.id}" <c:if test="${categoryValue == 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>

                <div class="form-field">
                    <label for="totalCopies">馆藏总数</label>
                    <input id="totalCopies" name="totalCopies" type="number" min="0" value="${fn:escapeXml(totalCopiesValue)}" required>
                    <c:if test="${not empty errors.totalCopies}">
                        <span class="field-error"><c:out value="${errors.totalCopies}" /></span>
                    </c:if>
                </div>

                <div class="form-field">
                    <label for="availableCopies">可借数量</label>
                    <input id="availableCopies" name="availableCopies" type="number" min="0" value="${fn:escapeXml(availableCopiesValue)}" required>
                    <c:if test="${not empty errors.availableCopies}">
                        <span class="field-error"><c:out value="${errors.availableCopies}" /></span>
                    </c:if>
                </div>

                <div class="form-field">
                    <label for="status">状态</label>
                    <select id="status" name="status" required>
                        <option value="">请选择状态</option>
                        <c:forEach var="status" items="${statuses}">
                            <option value="${status.code}" <c:if test="${statusValue == 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>
            </div>

            <div class="form-actions">
                <button class="button button-primary" type="submit">保存</button>
                <a class="button button-secondary" href="${pageContext.request.contextPath}/books">取消</a>
            </div>
        </form>
    </section>
</main>
</body>
</html>
