{{define "title"}}Manage Admins{{end}}
{{define "page_title"}}Manage Admins{{end}}

{{define "content"}}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-people-fill me-2"></i>Admins</h2>
    <a href="/pymta-manager/admins/add" class="btn btn-primary"><i class="bi bi-person-plus me-2"></i>Add Admin</a>
</div>

<div class="alert alert-info">
    <i class="bi bi-info-circle me-2"></i>
    Delegated admins can only see and manage the domains you assign them. They can also delegate further, but only for domains within their own assignment.
</div>

<div class="card">
    <div class="card-header"><h5 class="mb-0"><i class="bi bi-list-ul me-2"></i>All Admins You Manage</h5></div>
    <div class="card-body p-0">
        {{if .rows}}
            <div class="table-responsive">
                <table class="table table-dark table-hover mb-0">
                    <thead><tr><th>Username</th><th>Role</th><th>Domains</th><th>Created</th><th>Actions</th></tr></thead>
                    <tbody>
                        {{range .rows}}
                        {{$u := .user}}
                        <tr>
                            <td class="fw-bold">{{$u.Username}}</td>
                            <td>
                                {{if $u.IsGlobalAdmin}}<span class="badge bg-danger"><i class="bi bi-shield-fill-check me-1"></i>Global Admin</span>
                                {{else}}<span class="badge bg-secondary"><i class="bi bi-shield me-1"></i>Scoped Admin</span>{{end}}
                            </td>
                            <td>
                                {{if $u.IsGlobalAdmin}}<span class="text-muted">All domains</span>
                                {{else if .domain_names}}{{range .domain_names}}<span class="badge bg-info text-dark me-1">{{.}}</span>{{end}}
                                {{else}}<span class="text-muted">None assigned</span>{{end}}
                            </td>
                            <td><small class="text-muted">{{strftime "%Y-%m-%d %H:%M" $u.CreatedAt}}</small></td>
                            <td>
                                {{if eq $u.ID $.current_user_id}}
                                    <span class="text-muted small"><i class="bi bi-person-check me-1"></i>This is you</span>
                                {{else}}
                                <div class="btn-group btn-group-sm" role="group">
                                    {{if not $u.IsGlobalAdmin}}
                                    <a href="/pymta-manager/admins/{{$u.ID}}/edit" class="btn btn-outline-primary" title="Edit Domain Access"><i class="bi bi-pencil"></i></a>
                                    {{end}}
                                    <form method="post" action="/pymta-manager/admins/{{$u.ID}}/reset_mfa" class="d-inline">
                                        <button type="submit" class="btn btn-outline-warning" data-confirm="Reset MFA for {{$u.Username}}? They will need to re-enroll an authenticator app or passkey." title="Reset MFA"><i class="bi bi-shield-x"></i></button>
                                    </form>
                                    <form method="post" action="/pymta-manager/admins/{{$u.ID}}/remove" class="d-inline">
                                        <button type="submit" class="btn btn-outline-danger" data-confirm="Permanently remove admin {{$u.Username}}? This cannot be undone." title="Remove Admin"><i class="bi bi-trash"></i></button>
                                    </form>
                                </div>
                                {{end}}
                            </td>
                        </tr>
                        {{end}}
                    </tbody>
                </table>
            </div>
        {{else}}
            <div class="text-center py-5">
                <i class="bi bi-people text-muted" style="font-size: 4rem;"></i>
                <h4 class="text-muted mt-3">No delegated admins yet</h4>
                <p class="text-muted">Add an admin and assign them the domains they should manage</p>
                <a href="/pymta-manager/admins/add" class="btn btn-primary"><i class="bi bi-person-plus me-2"></i>Add Your First Admin</a>
            </div>
        {{end}}
    </div>
</div>
{{end}}
