{% extends "base.html" %}

{% block content %}
    <h1>Manage Users</h1>
    <form method="POST" action="{{ url_for('admin.seed_demo_accounts') }}" class="mb-3">
        <button type="submit" class="btn btn-sm btn-outline-primary">Create/Ensure Demo Accounts</button>
    </form>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>ID</th>
                <th>Username</th>
                <th>Email</th>
                <th>Role</th>
                <th>Status</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            {% for user in users %}
            <tr>
                <td>{{ user.id }}</td>
                <td>{{ user.username }}</td>
                <td>{{ user.email }}</td>
                <td>{{ user.role.capitalize() }}</td>
                <td>
                    {% if user.is_active %}
                        <span class="badge badge-success">Active</span>
                    {% else %}
                        <span class="badge badge-danger">Suspended</span>
                    {% endif %}
                </td>
                <td>
                    {% if user.id != current_user.id %}
                        {% if user.is_active %}
                            <form method="POST" action="{{ url_for('admin.suspend_user', user_id=user.id) }}" class="d-inline">
                                <button type="submit" class="btn btn-sm btn-warning">Suspend</button>
                            </form>
                        {% else %}
                            <form method="POST" action="{{ url_for('admin.activate_user', user_id=user.id) }}" class="d-inline">
                                <button type="submit" class="btn btn-sm btn-success">Activate</button>
                            </form>
                        {% endif %}
                        <form method="POST" action="{{ url_for('admin.delete_user', user_id=user.id) }}" class="d-inline ml-1">
                            <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this user?');">Delete</button>
                        </form>
                        <form method="POST" action="{{ url_for('admin.change_user_password', user_id=user.id) }}" class="d-inline ml-1">
                            <div class="input-group input-group-sm" style="width: 260px;">
                                <input type="password" name="new_password" placeholder="New password" class="form-control" required>
                                <div class="input-group-append">
                                    <button type="submit" class="btn btn-sm btn-secondary">Change</button>
                                </div>
                            </div>
                        </form>
                        {% if user.role == 'consultant' %}
                        <form method="POST" action="{{ url_for('admin.grant_credits', user_id=user.id) }}" class="d-inline ml-1">
                            <div class="input-group input-group-sm" style="width: 360px;">
                                <select name="assessment_id" class="form-control" required>
                                    {% for a in assessments %}
                                        <option value="{{ a.id }}">{{ a.name }}</option>
                                    {% endfor %}
                                </select>
                                <input type="number" name="quantity" min="1" value="2" class="form-control" style="max-width:80px" required>
                                <div class="input-group-append">
                                    <button type="submit" class="btn btn-sm btn-primary">Grant</button>
                                </div>
                            </div>
                        </form>
                        {% endif %}
                    {% else %}
                        <span class="text-muted">Cannot manage self</span>
                    {% endif %}
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
{% endblock %}
