{{define "title"}}Filter Rules - Email Server{{end}}
{{define "page_title"}}Filter Rules{{end}}

{{define "content"}}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-funnel me-2"></i>Filter Rules <small class="text-muted fs-6">{{.mailbox.Email}}</small></h2>
    <a href="/pymta-manager/mailboxes" class="btn btn-secondary"><i class="bi bi-arrow-left me-2"></i>Back to Mailboxes</a>
</div>

<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. "Move to folder" delivers into a separate IMAP folder instead of INBOX — your mail client will show it once mail has actually landed there.
</div>

<div class="card mb-4">
    <div class="card-header"><h5 class="mb-0"><i class="bi bi-plus-circle me-2"></i>Add Rule</h5></div>
    <div class="card-body">
        <form method="POST" action="/pymta-manager/mailboxes/{{.mailbox.ID}}/rules/add">
            <div class="row g-2 align-items-end mb-3">
                <div class="col-auto">
                    <label class="form-label">Priority</label>
                    <input type="number" class="form-control" name="priority" value="0" style="width: 90px;">
                </div>
                <div class="col-auto">
                    <label class="form-label">Match</label>
                    <select class="form-select" name="match_type">
                        <option value="all">ALL of the following (AND)</option>
                        <option value="any">ANY of the following (OR)</option>
                    </select>
                </div>
            </div>

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

            <div class="row g-2 align-items-end">
                <div class="col-auto">
                    <label class="form-label">Then</label>
                    <select class="form-select" name="action" id="rule_action">
                        <option value="move_to_folder">Move to folder</option>
                        <option value="mark_as_spam">Mark as Junk</option>
                        <option value="delete">Delete</option>
                        <option value="mark_read">Mark as read</option>
                    </select>
                </div>
                <div class="col-auto">
                    <input type="text" class="form-control" name="action_value" id="rule_action_value" placeholder="folder name">
                </div>
                <div class="col-auto">
                    <button type="submit" class="btn btn-success"><i class="bi bi-funnel me-2"></i>Add Rule</button>
                </div>
            </div>
        </form>

        <template id="condition_row_template">
            <div class="row g-2 align-items-end mb-2 condition-row">
                <div class="col-auto">
                    <label class="form-label">If</label>
                    <select class="form-select" name="condition_field">
                        <option value="from">From</option>
                        <option value="to">To</option>
                        <option value="subject">Subject</option>
                    </select>
                </div>
                <div class="col-auto">
                    <select class="form-select" 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-auto">
                    <input type="text" class="form-control" name="condition_value" placeholder="value" required>
                </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>
    </div>
</div>

<div class="card">
    <div class="card-header"><h5 class="mb-0"><i class="bi bi-list-ul me-2"></i>Existing Rules</h5></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>Condition</th><th>Action</th><th>Status</th><th>Actions</th></tr></thead>
                    <tbody>
                        {{range .rules}}
                        <tr>
                            <td>{{.Priority}}</td>
                            <td><code>{{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}}Mark as read{{end}}
                            </td>
                            <td>{{if .IsActive}}<span class="badge bg-success">Active</span>{{else}}<span class="badge bg-secondary">Inactive</span>{{end}}</td>
                            <td>
                                <form method="post" action="/pymta-manager/mailboxes/{{$.mailbox.ID}}/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: 4rem;"></i>
                <h4 class="text-muted mt-3">No rules yet</h4>
                <p class="text-muted">Add one above to automatically sort or act on incoming mail.</p>
            </div>
        {{end}}
    </div>
</div>
{{end}}

{{define "extra_js"}}
<script>
document.getElementById('rule_action').addEventListener('change', function(e) {
    const valueInput = document.getElementById('rule_action_value');
    valueInput.style.display = e.target.value === 'move_to_folder' ? '' : 'none';
});

function addConditionRow() {
    const tpl = document.getElementById('condition_row_template');
    const container = document.getElementById('conditions_container');
    const clone = document.importNode(tpl.content, true);
    clone.querySelector('.remove-condition').addEventListener('click', function() {
        if (container.children.length > 1) {
            this.closest('.condition-row').remove();
        }
    });
    container.appendChild(clone);
}
document.getElementById('add_condition').addEventListener('click', addConditionRow);
addConditionRow();
</script>
{{end}}
