{{define "webmail_blocklist.html"}}
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Settings - Webmail</title>
    <link href="/webmail/static/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="/webmail/static/vendor/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">
    <link href="/webmail/static/vendor/quill/quill.snow.css" rel="stylesheet">
    <style>
        body { background-color: #1a1a1a; color: #e0e0e0; }
        .table-dark { --bs-table-bg: #2d2d2d; --bs-table-border-color: #404040; }
    </style>
    {{template "webmail_settings_style" .}}
</head>
<body>
    {{template "csrf_script" .}}
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            <span class="navbar-brand mb-0 h1"><i class="bi bi-inbox-fill me-2"></i>Webmail <small class="text-muted">{{.mailbox.Email}}</small></span>
            <div class="navbar-nav flex-row gap-2 ms-auto">
                <a href="/webmail/mail/INBOX" class="btn btn-outline-light btn-sm"><i class="bi bi-envelope me-1"></i>Mail</a>
                <button type="button" onclick="openCompose('/webmail/mail/compose')" class="btn btn-outline-light btn-sm"><i class="bi bi-pencil-square me-1"></i>Compose</button>
                <form method="post" action="/webmail/logout" class="d-inline">
                    <button type="submit" class="btn btn-outline-light btn-sm"><i class="bi bi-box-arrow-right me-1"></i>Sign out</button>
                </form>
            </div>
        </div>
    </nav>

    <div class="toast-container position-fixed top-0 end-0 p-3" style="z-index: 1090;">
        {{range .flashes}}
        <div class="toast align-items-center text-bg-{{if eq .Category "error"}}danger{{else}}{{.Category}}{{end}} border-0" role="alert" aria-live="assertive" aria-atomic="true">
            <div class="d-flex">
                <div class="toast-body">
                    <i class="bi bi-{{if eq .Category "error"}}exclamation-triangle{{else if eq .Category "success"}}check-circle{{else}}info-circle{{end}} me-2"></i>
                    {{.Message}}
                </div>
                <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
            </div>
        </div>
        {{end}}
    </div>

    <div id="settingsPanelRoot">
    <div class="settings-card" id="settingsPanel">
        <div class="settings-card-header">
            <h5 class="mb-0"><i class="bi bi-sliders me-2"></i>Settings</h5>
            <button type="button" class="btn-close btn-close-white" onclick="closeSettings()" aria-label="Close"></button>
        </div>
        <div class="settings-card-body">
            {{template "webmail_settings_nav" .}}
            <div class="settings-body">
                <div class="alert alert-info">
                    <i class="bi bi-info-circle me-2"></i>An address or a whole domain (<code>@example.com</code>). <strong>Block</strong> quarantines matching mail straight to Junk without scoring it — the same action as right-click &gt; Mark as Junk. <strong>Whitelist</strong> skips spam scoring entirely, always landing in INBOX — use it for a trusted sender rspamd or the built-in filter keeps flagging.
                </div>

                <div class="card mb-4">
                    <div class="card-header"><h6 class="mb-0"><i class="bi bi-plus-circle me-2"></i>Add entry</h6></div>
                    <div class="card-body">
                        <form method="POST" action="/webmail/blocklist/add" class="row g-2 align-items-end">
                            <div class="col-auto">
                                <label class="form-label">Address or @domain.com</label>
                                <input type="text" class="form-control form-control-sm" name="pattern" placeholder="sender@example.com or @example.com" required style="min-width: 260px;">
                            </div>
                            <div class="col-auto">
                                <label class="form-label">List</label>
                                <select class="form-select form-select-sm" name="list_type">
                                    <option value="junk">Block (→ Junk)</option>
                                    <option value="allow">Whitelist (never spam)</option>
                                </select>
                            </div>
                            <div class="col-auto">
                                <button type="submit" class="btn btn-success btn-sm"><i class="bi bi-plus-lg me-1"></i>Add</button>
                            </div>
                        </form>
                    </div>
                </div>

                <div class="row g-4">
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header"><h6 class="mb-0"><i class="bi bi-slash-circle me-2"></i>Blocked <small class="text-muted fs-6">— goes to Junk</small></h6></div>
                            <div class="card-body p-0">
                                {{if .blocked}}
                                <ul class="list-group list-group-flush">
                                    {{range .blocked}}
                                    <li class="list-group-item list-group-item-dark d-flex justify-content-between align-items-center">
                                        <code>{{.Pattern}}</code>
                                        <form method="post" action="/webmail/blocklist/{{.ID}}/remove" class="d-inline">
                                            <button type="submit" class="btn btn-outline-danger btn-sm" title="Remove" data-confirm="Remove this entry?"><i class="bi bi-trash"></i></button>
                                        </form>
                                    </li>
                                    {{end}}
                                </ul>
                                {{else}}
                                <p class="text-muted p-3 mb-0">Nothing blocked yet.</p>
                                {{end}}
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header"><h6 class="mb-0"><i class="bi bi-check-circle me-2"></i>Whitelisted <small class="text-muted fs-6">— never spam</small></h6></div>
                            <div class="card-body p-0">
                                {{if .allowed}}
                                <ul class="list-group list-group-flush">
                                    {{range .allowed}}
                                    <li class="list-group-item list-group-item-dark d-flex justify-content-between align-items-center">
                                        <code>{{.Pattern}}</code>
                                        <form method="post" action="/webmail/blocklist/{{.ID}}/remove" class="d-inline">
                                            <button type="submit" class="btn btn-outline-danger btn-sm" title="Remove" data-confirm="Remove this entry?"><i class="bi bi-trash"></i></button>
                                        </form>
                                    </li>
                                    {{end}}
                                </ul>
                                {{else}}
                                <p class="text-muted p-3 mb-0">Nothing whitelisted yet.</p>
                                {{end}}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    </div>

    <div class="modal fade" id="confirmationModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title"><i class="bi bi-question-circle me-2"></i>Confirm Action</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body" id="confirmationModalBody">Are you sure you want to proceed?</div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="button" class="btn btn-danger" id="confirmationModalConfirm">Confirm</button>
                </div>
            </div>
        </div>
    </div>

    {{template "compose_widget" .}}

    <script src="/webmail/static/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="/webmail/static/vendor/quill/quill.js"></script>
    {{template "webmail_settings_script" .}}
</body>
</html>
{{end}}
