{% extends "admin/base.html" %}
{% block title %}Dashboard{% endblock %}
{% block content %}
<h2 class="mb-4"><i class="bi bi-shield-fill-check text-danger me-2"></i>Site Admin Dashboard</h2>

<div class="row g-3 mb-4">
    <div class="col-md-3">
        <div class="card border-0 bg-secondary bg-opacity-25">
            <div class="card-body text-center">
                <div class="fs-2 fw-bold text-danger">{{ stats.group_count }}</div>
                <div class="text-muted">Groups</div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-0 bg-secondary bg-opacity-25">
            <div class="card-body text-center">
                <div class="fs-2 fw-bold text-warning">{{ stats.user_count }}</div>
                <div class="text-muted">Users</div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-0 bg-secondary bg-opacity-25">
            <div class="card-body text-center">
                <div class="fs-2 fw-bold text-success">{{ stats.db_configured }}</div>
                <div class="text-muted">DBs configured</div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card border-0 bg-secondary bg-opacity-25">
            <div class="card-body text-center">
                <div class="fs-2 fw-bold text-info">{{ stats.admin_count }}</div>
                <div class="text-muted">Site Admins</div>
            </div>
        </div>
    </div>
</div>

<div class="row g-3">
    <div class="col-md-6">
        <div class="card border-secondary">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span><i class="bi bi-collection-fill me-2"></i>Groups</span>
                <a href="{{ url_for('site_admin.group_new') }}" class="btn btn-sm btn-success">
                    <i class="bi bi-plus-lg"></i> New
                </a>
            </div>
            <div class="card-body p-0">
                <table class="table table-hover mb-0">
                    <thead><tr><th>Name</th><th>Members</th><th>DB</th><th></th></tr></thead>
                    <tbody>
                    {% for g in groups %}
                    <tr>
                        <td>{{ g.name }}</td>
                        <td>{{ g.member_count }}</td>
                        <td>
                            {% if g.has_db %}
                            <span class="badge bg-success">Configured</span>
                            {% else %}
                            <span class="badge bg-secondary">None</span>
                            {% endif %}
                        </td>
                        <td class="text-end">
                            <form method="post" action="{{ url_for('site_admin.view_group', group_id=g.id) }}" class="d-inline">
                                <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
                                <button type="submit" class="btn btn-sm btn-outline-info" title="Browse">
                                    <i class="bi bi-eye"></i>
                                </button>
                            </form>
                            <a href="{{ url_for('site_admin.group_edit', group_id=g.id) }}" class="btn btn-sm btn-outline-secondary" title="Edit">
                                <i class="bi bi-pencil"></i>
                            </a>
                        </td>
                    </tr>
                    {% else %}
                    <tr><td colspan="4" class="text-muted text-center py-3">No groups yet</td></tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
            <div class="card-footer text-end">
                <a href="{{ url_for('site_admin.groups') }}" class="text-muted small">All groups →</a>
            </div>
        </div>
    </div>

    <div class="col-md-6">
        <div class="card border-secondary">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span><i class="bi bi-people-fill me-2"></i>Users</span>
                <a href="{{ url_for('site_admin.user_new') }}" class="btn btn-sm btn-success">
                    <i class="bi bi-plus-lg"></i> New
                </a>
            </div>
            <div class="card-body p-0">
                <table class="table table-hover mb-0">
                    <thead><tr><th>User</th><th>Groups</th><th>Admin</th><th></th></tr></thead>
                    <tbody>
                    {% for u in users %}
                    <tr>
                        <td>{{ u.username }}</td>
                        <td>{{ u.group_count }}</td>
                        <td>{% if u.is_site_admin %}<span class="badge bg-danger"><i class="bi bi-shield-fill"></i></span>{% endif %}</td>
                        <td class="text-end">
                            <a href="{{ url_for('site_admin.user_edit', user_id=u.id) }}" class="btn btn-sm btn-outline-secondary" title="Edit">
                                <i class="bi bi-pencil"></i>
                            </a>
                        </td>
                    </tr>
                    {% else %}
                    <tr><td colspan="4" class="text-muted text-center py-3">No users yet</td></tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
            <div class="card-footer text-end">
                <a href="{{ url_for('site_admin.users') }}" class="text-muted small">All users →</a>
            </div>
        </div>
    </div>
</div>
{% endblock %}
