{{define "title"}}Blacklist - Email Server{{end}}

{{define "content"}}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="bi bi-shield-x me-2"></i>SMTP/IMAP Abuse Blacklist</h2>
    </div>

    <div class="card mb-4">
        <div class="card-header"><h5 class="mb-0"><i class="bi bi-list me-2"></i>Blacklisted IP Addresses</h5></div>
        <div class="card-body">
            {{if .entries}}
                <div class="table-responsive">
                    <table class="table table-striped">
                        <thead><tr><th>IP Address</th><th>Reason</th><th>Offense #</th><th>Type</th><th>Blacklisted</th><th>Expires</th><th>Actions</th></tr></thead>
                        <tbody>
                            {{range .entries}}
                            <tr>
                                <td><div class="fw-bold font-monospace">{{.IPAddress}}</div></td>
                                <td><small class="text-muted">{{.Reason}}</small></td>
                                <td>{{.OffenseCount}}</td>
                                <td>{{if .Manual}}<span class="badge bg-secondary">Manual</span>{{else}}<span class="badge bg-warning text-dark">Auto</span>{{end}}</td>
                                <td><small class="text-muted">{{strftime "%Y-%m-%d %H:%M" .BlacklistedAt}}</small></td>
                                <td><small class="text-muted">{{strftime "%Y-%m-%d %H:%M" .ExpiresAt}}</small></td>
                                <td>
                                    <div class="btn-group" role="group">
                                        <form method="post" action="/pymta-manager/blacklist/{{.ID}}/whitelist" class="d-inline">
                                            <button type="submit" class="btn btn-outline-success btn-sm" title="Whitelist this IP" data-confirm="Remove {{.IPAddress}} from the blacklist and exempt it from abuse detection?"><i class="bi bi-shield-check"></i></button>
                                        </form>
                                        <form method="post" action="/pymta-manager/blacklist/{{.ID}}/remove" class="d-inline">
                                            <button type="submit" class="btn btn-outline-danger btn-sm" title="Remove entry" data-confirm="Remove the blacklist entry for {{.IPAddress}}?"><i class="bi bi-trash"></i></button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                            {{end}}
                        </tbody>
                    </table>
                </div>
            {{else}}
                <div class="text-center py-4">
                    <i class="bi bi-shield-check text-muted" style="font-size: 3rem;"></i>
                    <h5 class="text-muted mt-3">No IPs currently blacklisted</h5>
                </div>
            {{end}}
        </div>
    </div>

    <div class="card mb-4">
        <div class="card-header"><h6 class="mb-0"><i class="bi bi-plus-circle me-2"></i>Manually Blacklist an IP</h6></div>
        <div class="card-body">
            <form method="post" action="/pymta-manager/blacklist/add" class="row g-2 align-items-end">
                <div class="col-md-4">
                    <label class="form-label small">IP Address</label>
                    <input type="text" name="ip_address" class="form-control font-monospace" placeholder="203.0.113.7" required>
                </div>
                <div class="col-md-4">
                    <label class="form-label small">Reason</label>
                    <input type="text" name="reason" class="form-control" placeholder="Optional">
                </div>
                <div class="col-md-2">
                    <label class="form-label small">Hours</label>
                    <input type="number" name="hours" class="form-control" value="12" min="1" required>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-danger w-100"><i class="bi bi-shield-x me-1"></i>Blacklist</button>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-header"><h5 class="mb-0"><i class="bi bi-shield-check me-2"></i>Abuse-Detection Whitelist</h5></div>
        <div class="card-body">
            <div class="alert alert-info small">
                IPs here are exempt from automatic blacklisting for failed SMTP/IMAP auth. This is separate
                from the <a href="/pymta-manager/ips">relay whitelist</a>, which authorizes unauthenticated sending for a domain.
            </div>
            {{if .whitelist}}
                <div class="table-responsive mb-3">
                    <table class="table table-striped">
                        <thead><tr><th>IP Address</th><th>Note</th><th>Added</th><th>Actions</th></tr></thead>
                        <tbody>
                            {{range .whitelist}}
                            <tr>
                                <td><div class="fw-bold font-monospace">{{.IPAddress}}</div></td>
                                <td><small class="text-muted">{{.Note}}</small></td>
                                <td><small class="text-muted">{{strftime "%Y-%m-%d %H:%M" .CreatedAt}}</small></td>
                                <td>
                                    <form method="post" action="/pymta-manager/abuse-whitelist/{{.ID}}/remove" class="d-inline">
                                        <button type="submit" class="btn btn-outline-danger btn-sm" title="Remove" data-confirm="Remove {{.IPAddress}} from the abuse-detection whitelist?"><i class="bi bi-trash"></i></button>
                                    </form>
                                </td>
                            </tr>
                            {{end}}
                        </tbody>
                    </table>
                </div>
            {{end}}
            <form method="post" action="/pymta-manager/abuse-whitelist/add" class="row g-2 align-items-end">
                <div class="col-md-4">
                    <label class="form-label small">IP Address</label>
                    <input type="text" name="ip_address" class="form-control font-monospace" placeholder="203.0.113.7" required>
                </div>
                <div class="col-md-5">
                    <label class="form-label small">Note</label>
                    <input type="text" name="note" class="form-control" placeholder="Optional">
                </div>
                <div class="col-md-3">
                    <button type="submit" class="btn btn-primary w-100"><i class="bi bi-plus-circle me-1"></i>Add</button>
                </div>
            </form>
        </div>
    </div>
</div>
{{end}}
