<%@ 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="reader-form-title">
        <p class="eyebrow">读者管理</p>
        <h1 id="reader-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 : reader.identifier}" />
        <c:set var="userIdValue" value="${hasFormValues ? formValues.userId : reader.userId}" />
        <c:set var="fullNameValue" value="${hasFormValues ? formValues.fullName : reader.fullName}" />
        <c:set var="phoneValue" value="${hasFormValues ? formValues.phone : reader.phone}" />
        <c:set var="emailValue" value="${hasFormValues ? formValues.email : reader.email}" />
        <c:set var="statusValue" value="${hasFormValues ? formValues.status : reader.status.code}" />
        <c:set var="maxBorrowCountValue" value="${hasFormValues ? formValues.maxBorrowCount : reader.maxBorrowCount}" />

        <form class="reader-form" action="${pageContext.request.contextPath}${formAction}" method="post" novalidate>
            <c:if test="${reader.id > 0}">
                <input type="hidden" name="id" value="${reader.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="fullName">姓名</label>
                    <input id="fullName" name="fullName" type="text" value="${fn:escapeXml(fullNameValue)}" required>
                    <c:if test="${not empty errors.fullName}">
                        <span class="field-error"><c:out value="${errors.fullName}" /></span>
                    </c:if>
                </div>

                <div class="form-field">
                    <label for="phone">电话</label>
                    <input id="phone" name="phone" type="tel" value="${fn:escapeXml(phoneValue)}">
                    <c:if test="${not empty errors.phone}">
                        <span class="field-error"><c:out value="${errors.phone}" /></span>
                    </c:if>
                </div>

                <div class="form-field">
                    <label for="email">邮箱</label>
                    <input id="email" name="email" type="email" value="${fn:escapeXml(emailValue)}">
                    <c:if test="${not empty errors.email}">
                        <span class="field-error"><c:out value="${errors.email}" /></span>
                    </c:if>
                </div>

                <div class="form-field">
                    <label for="userId">关联账户 ID</label>
                    <input id="userId" name="userId" type="number" min="1" value="${fn:escapeXml(userIdValue)}">
                    <c:if test="${not empty errors.userId}">
                        <span class="field-error"><c:out value="${errors.userId}" /></span>
                    </c:if>
                </div>

                <div class="form-field">
                    <label for="maxBorrowCount">最大借阅数量</label>
                    <input id="maxBorrowCount" name="maxBorrowCount" type="number" min="1" max="50"
                           value="${fn:escapeXml(maxBorrowCountValue)}" required>
                    <c:if test="${not empty errors.maxBorrowCount}">
                        <span class="field-error"><c:out value="${errors.maxBorrowCount}" /></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}/readers">取消</a>
            </div>
        </form>
    </section>
</main>
</body>
</html>
