{{define "webmail_rules.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" .}}
    <style>
        .condition-row { background-color: #262626; }
        .chip-input { min-height: calc(1.75em + .5rem + 2px); background-color: #1a1a1a; border-color: #404040; cursor: text; }
        .chip-input input { background: transparent; color: #e0e0e0; }
        .chip-item { font-weight: normal; }
        .chip-item .bi-x { cursor: pointer; }
        .action-extra-fields > div { display: none; }
    </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>Rules run in priority order (lowest first) at delivery time; the first match wins.
                </div>

                <div class="card mb-4">
                    <div class="card-header"><h6 class="mb-0"><i class="bi bi-{{if .editing}}pencil{{else}}plus-circle{{end}} me-2"></i>{{if .editing}}Edit Rule{{else}}Add Rule{{end}}</h6></div>
                    <div class="card-body">
                        <form method="POST" action="/webmail/rules/save" id="ruleForm">
                            <input type="hidden" name="rule_id" value="{{if .editing}}{{.editing.ID}}{{end}}">

                            <div class="mb-3">
                                <label class="form-label">Rule name</label>
                                <input type="text" class="form-control form-control-sm" name="name" value="{{if .editing}}{{.editing.Name}}{{end}}" placeholder="e.g. Invoices to Accounting" style="max-width: 360px;">
                            </div>

                            <div class="row g-2 align-items-end mb-2">
                                <div class="col-auto">
                                    <label class="form-label">Priority</label>
                                    <input type="number" class="form-control form-control-sm" name="priority" value="{{if .editing}}{{.editing.Priority}}{{else}}0{{end}}" style="width: 90px;">
                                </div>
                                <div class="col-auto">
                                    <label class="form-label">Match</label>
                                    <select class="form-select form-select-sm" name="match_type" id="rule_match_type">
                                        <option value="all" {{if ne .editing_match_type "any"}}selected{{end}}>ALL of the following (AND)</option>
                                        <option value="any" {{if eq .editing_match_type "any"}}selected{{end}}>ANY of the following (OR)</option>
                                    </select>
                                </div>
                                {{if .editing}}
                                <div class="col-auto">
                                    <div class="form-check mt-4">
                                        <input class="form-check-input" type="checkbox" name="is_active" id="rule_is_active" value="1" {{if .editing.IsActive}}checked{{end}}>
                                        <label class="form-check-label" for="rule_is_active">Enabled</label>
                                    </div>
                                </div>
                                {{end}}
                            </div>

                            <div id="conditions_container"></div>
                            <button type="button" id="add_condition" class="btn btn-outline-light btn-sm mb-3"><i class="bi bi-plus-lg me-1"></i>Add condition</button>

                            <hr>

                            <div class="row g-2 align-items-end">
                                <div class="col-auto">
                                    <label class="form-label">Then</label>
                                    <select class="form-select form-select-sm" name="action" id="rule_action">
                                        <option value="move_to_folder" {{if or (not .editing) (eq .editing.Action "move_to_folder")}}selected{{end}}>Move to folder</option>
                                        <option value="mark_as_spam" {{if and .editing (eq .editing.Action "mark_as_spam")}}selected{{end}}>Mark as Junk</option>
                                        <option value="delete" {{if and .editing (eq .editing.Action "delete")}}selected{{end}}>Delete</option>
                                        <option value="mark_read" {{if and .editing (eq .editing.Action "mark_read")}}selected{{end}}>Mark as read</option>
                                        <option value="forward" {{if and .editing (eq .editing.Action "forward")}}selected{{end}}>Forward to...</option>
                                    </select>
                                </div>
                                <div class="col-auto action-extra-fields">
                                    <div class="action-field-move_to_folder">
                                        <label class="form-label">Folder name</label>
                                        <input type="text" class="form-control form-control-sm" name="action_value" id="rule_action_value_folder" placeholder="folder name" value="{{if and .editing (eq .editing.Action "move_to_folder")}}{{.editing.ActionValue}}{{end}}">
                                    </div>
                                    <div class="action-field-forward">
                                        <label class="form-label">Forward to</label>
                                        <input type="email" class="form-control form-control-sm" name="action_value" id="rule_action_value_forward" disabled placeholder="someone@example.com" value="{{if and .editing (eq .editing.Action "forward")}}{{.editing.ActionValue}}{{end}}">
                                        <div class="form-check mt-1">
                                            <input class="form-check-input" type="checkbox" name="keep_copy" id="rule_keep_copy" value="1" {{if or (not .editing) (ne .editing.Action "forward") .editing.ActionOptions.KeepCopy}}checked{{end}}>
                                            <label class="form-check-label small" for="rule_keep_copy">Also keep a copy in this mailbox</label>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-auto">
                                    <button type="submit" class="btn btn-success"><i class="bi bi-funnel me-2"></i>{{if .editing}}Update Rule{{else}}Add Rule{{end}}</button>
                                    {{if .editing}}<a href="/webmail/rules" class="btn btn-outline-secondary">Cancel</a>{{end}}
                                </div>
                            </div>
                        </form>

                        <template id="condition_row_template">
                            <div class="row g-2 align-items-start mb-2 condition-row border rounded p-2" style="border-color: #404040 !important;">
                                <div class="col-auto">
                                    <select class="form-select form-select-sm cond-field" name="condition_field" style="width: 150px;">
                                        <option value="from">From</option>
                                        <option value="to">To</option>
                                        <option value="subject">Subject</option>
                                        <option value="body">Body</option>
                                        <option value="has_attachment">Has attachment</option>
                                        <option value="recipient_type">I'm on the</option>
                                    </select>
                                </div>
                                <div class="col-auto cond-op-wrap" style="width: 140px;">
                                    <select class="form-select form-select-sm cond-op" name="condition_op">
                                        <option value="contains">contains</option>
                                        <option value="equals">equals</option>
                                        <option value="starts_with">starts with</option>
                                    </select>
                                </div>
                                <div class="col cond-value-area" style="min-width: 220px;">
                                    <div class="chip-input form-control form-control-sm d-flex flex-wrap gap-1"></div>
                                    <select class="form-select form-select-sm cond-fixed-select d-none"></select>
                                    <input type="hidden" class="cond-value-hidden" name="condition_value">
                                </div>
                                <div class="col-auto">
                                    <button type="button" class="btn btn-outline-danger btn-sm remove-condition" title="Remove condition"><i class="bi bi-x-lg"></i></button>
                                </div>
                            </div>
                        </template>
                        <script type="application/json" id="editingConditionsData">{{.editing_conditions_json}}</script>
                    </div>
                </div>

                <div class="card">
                    <div class="card-header"><h6 class="mb-0"><i class="bi bi-list-ul me-2"></i>Existing Rules</h6></div>
                    <div class="card-body p-0">
                        {{if .rules}}
                        <div class="table-responsive">
                            <table class="table table-dark table-hover mb-0">
                                <thead><tr><th>Priority</th><th>Name</th><th>Condition</th><th>Action</th><th>Status</th><th>Actions</th></tr></thead>
                                <tbody>
                                    {{range .rules}}
                                    <tr>
                                        <td>{{.Priority}}</td>
                                        <td>{{if .Name}}{{.Name}}{{else}}<span class="text-muted">(unnamed)</span>{{end}}</td>
                                        <td><code class="small">{{ruleSummary .}}</code></td>
                                        <td>
                                            {{if eq .Action "move_to_folder"}}Move to <strong>{{.ActionValue}}</strong>
                                            {{else if eq .Action "mark_as_spam"}}<span class="text-warning">Mark as Junk</span>
                                            {{else if eq .Action "delete"}}<span class="text-danger">Delete</span>
                                            {{else if eq .Action "forward"}}Forward to <strong>{{.ActionValue}}</strong>{{if not .ActionOptions.KeepCopy}} <span class="text-muted">(no local copy)</span>{{end}}
                                            {{else}}Mark as read{{end}}
                                        </td>
                                        <td>
                                            <form method="post" action="/webmail/rules/{{.ID}}/toggle" class="d-inline">
                                                <button type="submit" class="btn btn-sm {{if .IsActive}}btn-success{{else}}btn-secondary{{end}}" title="Click to {{if .IsActive}}disable{{else}}enable{{end}}">{{if .IsActive}}Active{{else}}Inactive{{end}}</button>
                                            </form>
                                        </td>
                                        <td class="d-flex gap-1">
                                            <a href="/webmail/rules?edit={{.ID}}" class="btn btn-outline-secondary btn-sm" title="Edit"><i class="bi bi-pencil"></i></a>
                                            <form method="post" action="/webmail/rules/{{.ID}}/remove" class="d-inline">
                                                <button type="submit" class="btn btn-outline-danger btn-sm" title="Remove" data-confirm="Remove this rule?"><i class="bi bi-trash"></i></button>
                                            </form>
                                        </td>
                                    </tr>
                                    {{end}}
                                </tbody>
                            </table>
                        </div>
                        {{else}}
                        <div class="text-center py-5">
                            <i class="bi bi-funnel text-muted" style="font-size: 3rem;"></i>
                            <h6 class="text-muted mt-3">No rules yet</h6>
                            <p class="text-muted small">Add one above to automatically sort or act on incoming mail.</p>
                        </div>
                        {{end}}
                    </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}}
