{% extends "base.html" %}
{% block content %}
<div class="container mt-4">
    <h2 class="mb-3">用户管理</h2>

    {% with messages = get_flashed_messages() %}
        {% if messages %}
            <div class="alert alert-info">
                {% for message in messages %}
                    {{ message }}
                {% endfor %}
            </div>
        {% endif %}
    {% endwith %}

    <div class="table-responsive">
        <table class="table table-bordered align-middle">
            <thead class="table-light">
                <tr>
                    <th>手机号</th>
                    <th>姓名</th>
                    <th>所属支局</th>
                    <th>角色</th>
                    <th style="width: 160px;">操作</th>
                </tr>
            </thead>
            <tbody>
                {% for user in users %}
                <tr>
                    <form method="POST" action="{{ url_for('update_user', phone=user.phone) }}">
                        <td>{{ user.phone }}</td>
                        <td>{{ user.name }}</td>
                        <td>
                            <select name="branch" class="form-select">
                                <option value="" {% if not user.branch %}selected{% endif %}>未设置</option>
                                {% for branch in branch_options %}
                                    <option value="{{ branch }}" {% if user.branch == branch %}selected{% endif %}>{{ branch }}</option>
                                {% endfor %}
                            </select>
                        </td>
                        <td>
                            <select name="role" class="form-select" required>
                                {% for role in role_options %}
                                    <option value="{{ role }}" {% if user.role == role %}selected{% endif %}>{{ role }}</option>
                                {% endfor %}
                            </select>
                        </td>
                        <td class="text-center">
                            <button type="submit" class="btn btn-primary btn-sm">保存</button>
                        </td>
                    </form>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>
{% endblock %}

