{{define "title"}}Dashboard - Email Server Management{{end}}
{{define "page_title"}}Dashboard{{end}}

{{define "content"}}
<div class="row">
    <div class="col-lg-3 col-md-6 mb-4">
        <a href="/pymta-manager/domains" class="text-decoration-none">
        <div class="card border-primary">
            <div class="card-body">
                <div class="d-flex align-items-center">
                    <div class="flex-grow-1">
                        <h5 class="card-title text-primary mb-1"><i class="bi bi-globe me-2"></i>Domains</h5>
                        <h3 class="mb-0">{{.domain_count}}</h3>
                        <small class="text-muted">Active domains</small>
                    </div>
                    <div class="fs-2 text-primary opacity-50"><i class="bi bi-globe"></i></div>
                </div>
            </div>
        </div>
        </a>
    </div>

    <div class="col-lg-3 col-md-6 mb-4">
        <a href="/pymta-manager/senders" class="text-decoration-none">
        <div class="card border-success">
            <div class="card-body">
                <div class="d-flex align-items-center">
                    <div class="flex-grow-1">
                        <h5 class="card-title text-success mb-1"><i class="bi bi-people me-2"></i>Senders</h5>
                        <h3 class="mb-0">{{.sender_count}}</h3>
                        <small class="text-muted">Authenticated senders</small>
                    </div>
                    <div class="fs-2 text-success opacity-50"><i class="bi bi-people"></i></div>
                </div>
            </div>
        </div>
        </a>
    </div>

    <div class="col-lg-3 col-md-6 mb-4">
        <a href="/pymta-manager/dkim" class="text-decoration-none">
        <div class="card border-warning">
            <div class="card-body">
                <div class="d-flex align-items-center">
                    <div class="flex-grow-1">
                        <h5 class="card-title text-warning mb-1"><i class="bi bi-shield-check me-2"></i>DKIM Keys</h5>
                        <h3 class="mb-0">{{.dkim_count}}</h3>
                        <small class="text-muted">Active DKIM keys</small>
                    </div>
                    <div class="fs-2 text-warning opacity-50"><i class="bi bi-shield-check"></i></div>
                </div>
            </div>
        </div>
        </a>
    </div>

    <div class="col-lg-3 col-md-6 mb-4">
        <a href="/pymta-manager/mailboxes" class="text-decoration-none">
        <div class="card {{if gt .mailboxes_near_quota 0}}border-danger{{else}}border-primary{{end}}">
            <div class="card-body">
                <div class="d-flex align-items-center">
                    <div class="flex-grow-1">
                        <h5 class="card-title {{if gt .mailboxes_near_quota 0}}text-danger{{else}}text-primary{{end}} mb-1"><i class="bi bi-inbox me-2"></i>Mailboxes</h5>
                        <h3 class="mb-0">{{.mailbox_count}}</h3>
                        {{if gt .mailboxes_near_quota 0}}
                            <small class="text-danger"><i class="bi bi-exclamation-triangle me-1"></i>{{.mailboxes_near_quota}} near quota</small>
                        {{else}}
                            <small class="text-muted">Active mailboxes</small>
                        {{end}}
                    </div>
                    <div class="fs-2 {{if gt .mailboxes_near_quota 0}}text-danger{{else}}text-primary{{end}} opacity-50"><i class="bi bi-inbox"></i></div>
                </div>
            </div>
        </div>
        </a>
    </div>

    <div class="col-lg-3 col-md-6 mb-4">
        <div class="card border-info">
            <div class="card-body">
                <div class="d-flex align-items-center">
                    <div class="flex-grow-1">
                        <h5 class="card-title text-info mb-1"><i class="bi bi-activity me-2"></i>Status</h5>
                        <h6 class="{{if eq .health.Status "healthy"}}text-success{{else}}text-warning{{end}} mb-0">
                            <i class="bi bi-circle-fill me-1" style="font-size: 0.5rem;"></i>
                            {{title .health.Status}}
                        </h6>
                        <small class="text-muted">
                            {{if and (eq .health.Services.smtp_server "running") (eq .health.Services.database "ok")}}
                                All services running
                            {{else}}
                                {{if eq .health.Services.smtp_server "stopped"}}SMTP Server stopped{{end}}
                                {{if eq .health.Services.database "error"}}Database error{{end}}
                            {{end}}
                        </small>
                    </div>
                    <div class="fs-2 text-info opacity-50"><i class="bi bi-activity"></i></div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-lg-8 mb-4">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="bi bi-envelope me-2"></i>Recent Email Activity</h5>
                <a href="/pymta-manager/logs?type=emails" class="btn btn-outline-light btn-sm">View All</a>
            </div>
            <div class="card-body p-0">
                {{if .recent_emails}}
                    <div class="table-responsive">
                        <table class="table table-dark table-hover mb-0">
                            <thead><tr><th>Time</th><th>From</th><th>Recipients</th><th>Status</th><th>DKIM</th></tr></thead>
                            <tbody>
                                {{range .recent_emails}}
                                <tr>
                                    <td><small class="text-muted">{{formatDatetime .CreatedAt}}</small></td>
                                    <td><span class="text-truncate d-inline-block" style="max-width: 150px;" title="{{.MailFrom}}">{{.MailFrom}}</span></td>
                                    <td>
                                        <div style="max-width: 200px; font-size: 0.85rem;">
                                            {{if .ToAddress}}<div class="text-truncate"><span class="text-info fw-bold" style="font-size: 0.75rem;">To:</span> {{.ToAddress}}</div>{{end}}
                                            {{if .CcAddresses}}<div class="text-truncate"><span class="text-warning fw-bold" style="font-size: 0.75rem;">CC:</span> {{.CcAddresses}}</div>{{end}}
                                            {{if .BccAddresses}}<div class="text-truncate"><span class="text-secondary fw-bold" style="font-size: 0.75rem;">BCC:</span> {{.BccAddresses}}</div>{{end}}
                                        </div>
                                    </td>
                                    <td>
                                        {{if eq .Status "relayed"}}
                                            <span class="badge bg-success"><i class="bi bi-check-circle me-1"></i>Sent</span>
                                        {{else if eq .Status "partial"}}
                                            <span class="badge bg-warning text-dark"><i class="bi bi-exclamation-triangle me-1"></i>Partial Fail</span>
                                        {{else}}
                                            <span class="badge bg-danger"><i class="bi bi-x-circle me-1"></i>Failed</span>
                                        {{end}}
                                    </td>
                                    <td>
                                        {{if .DKIMSigned}}<span class="text-success"><i class="bi bi-shield-check" title="DKIM Signed"></i></span>
                                        {{else}}<span class="text-muted"><i class="bi bi-shield-x" title="Not DKIM Signed"></i></span>{{end}}
                                    </td>
                                </tr>
                                {{end}}
                            </tbody>
                        </table>
                    </div>
                {{else}}
                    <div class="text-center py-4"><i class="bi bi-envelope text-muted fs-1"></i><p class="text-muted mt-2">No email activity yet</p></div>
                {{end}}
            </div>
        </div>
    </div>

    <div class="col-lg-4 mb-4">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="bi bi-shield-lock me-2"></i>Recent Auth Activity</h5>
                <a href="/pymta-manager/logs?type=auth" class="btn btn-outline-light btn-sm">View All</a>
            </div>
            <div class="card-body p-0">
                {{if .recent_auths}}
                    <div class="list-group list-group-flush">
                        {{range .recent_auths}}
                        <div class="list-group-item list-group-item-dark d-flex justify-content-between align-items-start">
                            <div class="ms-2 me-auto">
                                <div class="fw-bold">
                                    {{if .Success}}<i class="bi bi-check-circle text-success me-1"></i>{{else}}<i class="bi bi-x-circle text-danger me-1"></i>{{end}}
                                    {{title .AuthType}}
                                </div>
                                <small class="text-muted">{{.Identifier}}</small><br>
                                <small class="text-muted">{{formatDatetime .CreatedAt}}</small>
                            </div>
                            <small class="text-muted">{{.IPAddress}}</small>
                        </div>
                        {{end}}
                    </div>
                {{else}}
                    <div class="text-center py-4"><i class="bi bi-shield-lock text-muted fs-1"></i><p class="text-muted mt-2">No authentication activity yet</p></div>
                {{end}}
            </div>
        </div>
    </div>
</div>

{{if dget . "is_global_admin"}}
<div class="row">
    <div class="col-12 mb-4">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="bi bi-shield-x me-2"></i>Attack Activity</h5>
                <a href="/pymta-manager/blacklist" class="btn btn-outline-light btn-sm">View Blacklist</a>
            </div>
            <div class="card-body">
                <div class="row text-center">
                    <div class="col-6 col-md-2 mb-3 mb-md-0">
                        <div class="fs-3 {{if gt (dget . "active_blacklist_count") 0}}text-danger{{else}}text-secondary{{end}}">{{dget . "active_blacklist_count"}}</div>
                        <small class="text-muted">Active Blocks</small>
                    </div>
                    <div class="col-6 col-md-2 mb-3 mb-md-0">
                        <div class="fs-3 text-warning">{{dget . "failed_auth_24h"}}</div>
                        <small class="text-muted">Failed Auth (24h)</small>
                    </div>
                    <div class="col-6 col-md-2 mb-3 mb-md-0">
                        <div class="fs-3 text-warning">{{dget . "failed_auth_7d"}}</div>
                        <small class="text-muted">Failed Auth (7d)</small>
                    </div>
                    <div class="col-6 col-md-2 mb-3 mb-md-0">
                        <div class="fs-3 text-danger">{{dget . "blacklist_events_24h"}}</div>
                        <small class="text-muted">Blacklist Events (24h)</small>
                    </div>
                    <div class="col-6 col-md-2">
                        <div class="fs-3 text-danger">{{dget . "blacklist_events_7d"}}</div>
                        <small class="text-muted">Blacklist Events (7d)</small>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{{end}}

<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header"><h5 class="mb-0"><i class="bi bi-lightning me-2"></i>Quick Actions</h5></div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-3 mb-3"><div class="d-grid"><a href="/pymta-manager/domains/add" class="btn btn-outline-primary"><i class="bi bi-plus-circle me-2"></i>Add Domain</a></div></div>
                    <div class="col-md-3 mb-3"><div class="d-grid"><a href="/pymta-manager/senders/add" class="btn btn-outline-success"><i class="bi bi-person-plus me-2"></i>Add Sender</a></div></div>
                    <div class="col-md-3 mb-3"><div class="d-grid"><a href="/pymta-manager/ips/add" class="btn btn-outline-warning"><i class="bi bi-shield-plus me-2"></i>Whitelist IP</a></div></div>
                    {{if dget . "is_global_admin"}}
                    <div class="col-md-3 mb-3"><div class="d-grid"><a href="/pymta-manager/settings" class="btn btn-outline-info"><i class="bi bi-gear me-2"></i>Settings</a></div></div>
                    {{else}}
                    <div class="col-md-3 mb-3"><div class="d-grid"><a href="/pymta-manager/mailboxes/add" class="btn btn-outline-info"><i class="bi bi-mailbox me-2"></i>Add Mailbox</a></div></div>
                    {{end}}
                </div>
            </div>
        </div>
    </div>
</div>
{{end}}

{{define "extra_js"}}
<script>
    setTimeout(function() { location.reload(); }, 30000);
</script>
{{end}}
