{% extends "base.html" %}

{% block title %}编辑用户 - 图书管理系统{% endblock %}

{% block head %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/user-edit.css') }}">
{% endblock %}

{% block content %}
<div class="user-edit-container">
    <div class="page-header">
        <h1>编辑用户</h1>
        <div class="actions">
            <a href="{{ url_for('user.user_list') }}" class="btn btn-outline-secondary">
                <i class="fas fa-arrow-left"></i> 返回用户列表
            </a>
        </div>
    </div>

    <div class="card">
        <div class="card-body">
            {% with messages = get_flashed_messages(with_categories=true) %}
              {% if messages %}
                {% for category, message in messages %}
                  <div class="alert alert-{{ category }}">{{ message }}</div>
                {% endfor %}
              {% endif %}
            {% endwith %}

            <form method="POST" action="{{ url_for('user.user_edit', user_id=user.id) }}" id="userEditForm">
                <div class="form-row">
                    <!-- 用户基本信息 -->
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="username">用户名</label>
                            <input type="text" class="form-control" id="username" value="{{ user.username }}" readonly>
                            <small class="form-text text-muted">用户名不可修改</small>
                        </div>

                        <div class="form-group">
                            <label for="email">邮箱地址</label>
                            <input type="email" class="form-control" id="email" name="email" value="{{ user.email or '' }}">
                        </div>

                        <div class="form-group">
                            <label for="phone">手机号码</label>
                            <input type="text" class="form-control" id="phone" name="phone" value="{{ user.phone or '' }}">
                        </div>

                        <div class="form-group">
                            <label for="nickname">昵称</label>
                            <input type="text" class="form-control" id="nickname" name="nickname" value="{{ user.nickname or '' }}">
                        </div>
                    </div>

                    <!-- 用户权限和密码 -->
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="role_id">用户角色</label>
                            <select class="form-control" id="role_id" name="role_id">
                                {% for role in roles %}
                                <option value="{{ role.id }}" {% if role.id == user.role_id %}selected{% endif %}>
                                    {{ role.role_name }}
                                </option>
                                {% endfor %}
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="status">用户状态</label>
                            <select class="form-control" id="status" name="status">
                                <option value="1" {% if user.status == 1 %}selected{% endif %}>正常</option>
                                <option value="0" {% if user.status == 0 %}selected{% endif %}>禁用</option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="password">重置密码</label>
                            <input type="password" class="form-control" id="password" name="password">
                            <small class="form-text text-muted">留空表示不修改密码</small>
                        </div>

                        <div class="form-group" id="confirmPasswordGroup" style="display: none;">
                            <label for="confirm_password">确认密码</label>
                            <input type="password" class="form-control" id="confirm_password" name="confirm_password">
                        </div>
                    </div>
                </div>

                <!-- 附加信息 -->
                <div class="form-row">
                    <div class="col-md-12">
                        <div class="user-info-box">
                            <div class="info-item">
                                <span class="info-label">用户ID:</span>
                                <span class="info-value">{{ user.id }}</span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">注册时间:</span>
                                <span class="info-value">{{ user.created_at }}</span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">最后更新:</span>
                                <span class="info-value">{{ user.updated_at }}</span>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 提交按钮区域 -->
                <div class="form-actions">
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save"></i> 保存修改
                    </button>
                    <a href="{{ url_for('user.user_list') }}" class="btn btn-secondary">
                        取消
                    </a>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 操作成功提示模态框 -->
<div class="modal fade" id="successModal" tabindex="-1" role="dialog" aria-labelledby="successModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="successModalLabel">操作成功</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                用户信息已成功更新。
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                <a href="{{ url_for('user.user_list') }}" class="btn btn-primary">返回用户列表</a>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script src="{{ url_for('static', filename='js/user-edit.js') }}"></script>
{% endblock %}
