{% extends "base.html" %}

{% block title %}User Management - Admin{% endblock %}

{% block content %}
<div class="bbs-header">
    <h1><i class="bi bi-people"></i> User Management</h1>
</div>

<div class="d-flex justify-content-between mb-3">
    <form method="get" class="d-flex gap-2">
        <input type="text" name="search" value="{{ search }}" placeholder="Search username or email..." class="form-control" style="width:300px">
        <button type="submit" class="btn btn-primary"><i class="bi bi-search"></i></button>
    </form>
    <a href="{{ url_for('admin.add_user') }}" class="btn btn-primary"><i class="bi bi-person-plus"></i> Add User</a>
</div>

<form method="post" action="{{ url_for('admin.bulk_users') }}">
    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
    <div class="card">
        <div class="card-body p-0">
            <table class="table mb-0">
                <thead>
                    <tr>
                        <th><input type="checkbox" id="select-all"></th>
                        <th>Username</th>
                        <th>Email</th>
                        <th>Status</th>
                        <th>Joined</th>
                        <th>Last Login</th>
                        <th>Logins</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for user in users %}
                    <tr>
                        <td><input type="checkbox" name="user_ids" value="{{ user.id }}"></td>
                        <td>
                            {{ user.username }}
                            {% if user.is_admin %}<span class="badge bg-warning text-dark">Admin</span>{% endif %}
                            {% if not user.is_active %}<span class="badge bg-danger">Banned</span>{% endif %}
                        </td>
                        <td>{{ user.email }}</td>
                        <td>
                            {% if user.is_active %}
                                <i class="bi bi-circle-fill text-success"></i> Active
                            {% else %}
                                <i class="bi bi-circle-fill text-danger"></i> Banned
                            {% endif %}
                        </td>
                        <td>{{ user.created_at.strftime('%Y-%m-%d') }}</td>
                        <td>{{ user.last_login.strftime('%Y-%m-%d') if user.last_login else 'Never' }}</td>
                        <td>{{ user.login_count }}</td>
                        <td>
                            <a href="{{ url_for('admin.edit_user', user_id=user.id) }}" class="btn btn-sm btn-primary" title="Edit"><i class="bi bi-pencil"></i></a>
                            <a href="{{ url_for('admin.manage_user', user_id=user.id) }}" class="btn btn-sm btn-info" title="Manage (access / time / ratio / notes)"><i class="bi bi-person-gear"></i></a>
                            <a href="{{ url_for('admin.user_notes', user_id=user.id) }}" class="btn btn-sm btn-outline-warning" title="Sysop notes"><i class="bi bi-journal-text"></i></a>
                            <form method="post" action="{{ url_for('admin.toggle_ban', user_id=user.id) }}" class="d-inline">
                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                                <button type="submit" class="btn btn-sm btn-primary" title="{{ 'Unban' if not user.is_active else 'Ban' }}">
                                    <i class="bi bi-{{ 'check-circle' if not user.is_active else 'slash-circle' }}"></i>
                                </button>
                            </form>
                            {% if user.id != current_user.id %}
                            <a href="{{ url_for('admin.delete_user', user_id=user.id) }}" class="btn btn-sm btn-danger"><i class="bi bi-trash"></i></a>
                            {% endif %}
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <div class="d-flex justify-content-between mt-3">
        <div class="d-flex gap-2">
            <select name="action" class="form-select" style="width:auto">
                <option value="">Bulk Action...</option>
                <option value="ban">Ban Selected</option>
                <option value="unban">Unban Selected</option>
                <option value="delete">Delete Selected</option>
            </select>
            <button type="submit" class="btn btn-primary">Apply</button>
        </div>

        <!-- Pagination -->
        <nav>
            <ul class="pagination mb-0">
                {% if pagination.has_prev %}
                <li class="page-item">
                    <a class="page-link" href="{{ url_for('admin.users', page=pagination.prev_num, search=search) }}">Previous</a>
                </li>
                {% endif %}
                <li class="page-item disabled"><span class="page-link">{{ pagination.page }} / {{ pagination.pages }}</span></li>
                {% if pagination.has_next %}
                <li class="page-item">
                    <a class="page-link" href="{{ url_for('admin.users', page=pagination.next_num, search=search) }}">Next</a>
                </li>
                {% endif %}
            </ul>
        </nav>
    </div>
</form>

<script>
document.getElementById('select-all').addEventListener('change', function() {
    document.querySelectorAll('input[name="user_ids"]').forEach(cb => cb.checked = this.checked);
});
</script>
{% endblock %}
