{% extends "admin/base.html" %}
{% block title %}Groups{% endblock %}
{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-collection-fill me-2"></i>Groups</h2>
    <a href="{{ url_for('site_admin.group_new') }}" class="btn btn-success">
        <i class="bi bi-plus-lg me-1"></i>New Group
    </a>
</div>

<div class="card border-secondary">
    <div class="card-body p-0">
        <table class="table table-hover mb-0">
            <thead>
                <tr>
                    <th>ID</th><th>Name</th><th>Description</th><th>Members</th><th>DB</th><th>Created</th><th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
            {% for g in groups %}
            <tr>
                <td class="text-muted small">{{ g.id }}</td>
                <td class="fw-semibold">{{ g.name }}</td>
                <td class="text-muted small">{{ g.description or '—' }}</td>
                <td>{{ g.member_count }}</td>
                <td>
                    {% if g.has_db %}
                    <span class="badge bg-success"><i class="bi bi-database-fill-check me-1"></i>Configured</span>
                    {% else %}
                    <span class="badge bg-secondary">No DB</span>
                    {% endif %}
                </td>
                <td class="text-muted small">{{ g.created_at | fmt_dt }}</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 data">
                            <i class="bi bi-eye"></i>
                        </button>
                    </form>
                    <a href="{{ url_for('site_admin.group_members', group_id=g.id) }}" class="btn btn-sm btn-outline-secondary" title="Members">
                        <i class="bi bi-people-fill"></i>
                    </a>
                    <a href="{{ url_for('site_admin.group_edit', group_id=g.id) }}" class="btn btn-sm btn-outline-warning" title="Edit">
                        <i class="bi bi-pencil"></i>
                    </a>
                    <form method="post" action="{{ url_for('site_admin.group_delete', group_id=g.id) }}" class="d-inline"
                          onsubmit="return confirm('Delete group {{ g.name }}?')">
                        <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
                        <button type="submit" class="btn btn-sm btn-outline-danger" title="Delete">
                            <i class="bi bi-trash3"></i>
                        </button>
                    </form>
                </td>
            </tr>
            {% else %}
            <tr><td colspan="7" class="text-muted text-center py-4">No groups yet.</td></tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
</div>
{% endblock %}
