<%@ 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="user-form-title">
        <p class="eyebrow">系统管理</p>
        <h1 id="user-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="usernameValue" value="${hasFormValues ? formValues.username : user.username}" />
        <c:set var="displayNameValue" value="${hasFormValues ? formValues.displayName : user.displayName}" />
        <c:set var="roleValue" value="${hasFormValues ? formValues.role : user.role.code}" />
        <c:set var="activeValue" value="${hasFormValues ? formValues.active : user.active}" />

        <form class="user-form" action="${pageContext.request.contextPath}${formAction}" method="post" novalidate>
            <c:if test="${user.id > 0}">
                <input type="hidden" name="id" value="${user.id}">
                <input type="hidden" name="username" value="${fn:escapeXml(usernameValue)}">
            </c:if>

            <div class="form-grid">
                <div class="form-field">
                    <label for="username">用户名</label>
                    <c:choose>
                        <c:when test="${user.id > 0}">
                            <input id="username" type="text" value="${fn:escapeXml(usernameValue)}" disabled>
                        </c:when>
                        <c:otherwise>
                            <input id="username" name="username" type="text" value="${fn:escapeXml(usernameValue)}" required>
                        </c:otherwise>
                    </c:choose>
                    <c:if test="${not empty errors.username}">
                        <span class="field-error"><c:out value="${errors.username}" /></span>
                    </c:if>
                </div>

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

                <div class="form-field">
                    <label for="role">角色</label>
                    <select id="role" name="role" required>
                        <option value="">请选择角色</option>
                        <c:forEach var="role" items="${roles}">
                            <option value="${role.code}" <c:if test="${roleValue == role.code}">selected</c:if>>
                                <c:out value="${role.displayName}" />
                            </option>
                        </c:forEach>
                    </select>
                    <c:if test="${not empty errors.role}">
                        <span class="field-error"><c:out value="${errors.role}" /></span>
                    </c:if>
                </div>

                <div class="form-field">
                    <label for="active">启用状态</label>
                    <select id="active" name="active" required>
                        <option value="true" <c:if test="${activeValue == true or activeValue == 'true'}">selected</c:if>>
                            启用
                        </option>
                        <option value="false" <c:if test="${activeValue == false or activeValue == 'false'}">selected</c:if>>
                            停用
                        </option>
                    </select>
                    <c:if test="${not empty errors.active}">
                        <span class="field-error"><c:out value="${errors.active}" /></span>
                    </c:if>
                </div>

                <div class="form-field">
                    <label for="password">
                        <c:choose>
                            <c:when test="${user.id > 0}">新密码</c:when>
                            <c:otherwise>密码</c:otherwise>
                        </c:choose>
                    </label>
                    <c:choose>
                        <c:when test="${user.id > 0}">
                            <input id="password" name="password" type="password" autocomplete="new-password">
                        </c:when>
                        <c:otherwise>
                            <input id="password" name="password" type="password" autocomplete="new-password" required>
                        </c:otherwise>
                    </c:choose>
                    <c:if test="${not empty errors.password}">
                        <span class="field-error"><c:out value="${errors.password}" /></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}/admin/users">取消</a>
            </div>
        </form>
    </section>
</main>
</body>
</html>
