{{define "title"}}Aliases - Email Server{{end}}
{{define "page_title"}}Aliases{{end}}

{{define "content"}}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-signpost-split me-2"></i>Aliases <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>An alias lets this mailbox receive mail at another address. Enable "send as" to also let it send mail using that address. The login address is always <code>{{.mailbox.Email}}</code> — aliases never change that.
</div>

<div class="card mb-4">
    <div class="card-header"><h5 class="mb-0"><i class="bi bi-plus-circle me-2"></i>Add Alias</h5></div>
    <div class="card-body">
        <form method="POST" action="/pymta-manager/mailboxes/{{.mailbox.ID}}/aliases/add">
            <div class="mb-3">
                <div class="input-group">
                    <input type="text" class="form-control" id="local_part" name="local_part" required placeholder="alias"
                           pattern="[a-zA-Z0-9._%+-]+" title="Letters, numbers, and . _ % + - only">
                    <span class="input-group-text">@</span>
                    <select class="form-select" id="domain_id" name="domain_id" required style="max-width: 260px;">
                        <option value="">Select a domain...</option>
                        {{range .domains}}<option value="{{.ID}}">{{.DomainName}}</option>{{end}}
                    </select>
                </div>
            </div>
            <div class="mb-3">
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" id="can_send_as" name="can_send_as">
                    <label class="form-check-label" for="can_send_as"><strong>Allow sending as this address</strong></label>
                    <div class="form-text">If enabled, this mailbox can use MAIL FROM with this alias once authenticated with its app password.</div>
                </div>
            </div>
            <button type="submit" class="btn btn-success"><i class="bi bi-signpost-split me-2"></i>Add Alias</button>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-header"><h5 class="mb-0"><i class="bi bi-list-ul me-2"></i>Existing Aliases</h5></div>
    <div class="card-body p-0">
        {{if .aliases}}
            <div class="table-responsive">
                <table class="table table-dark table-hover mb-0">
                    <thead><tr><th>Address</th><th>Permissions</th><th>Created</th><th>Actions</th></tr></thead>
                    <tbody>
                        {{range .aliases}}
                        <tr>
                            <td>{{.Email}}</td>
                            <td>
                                {{if .CanSendAs}}<span class="badge bg-warning text-dark"><i class="bi bi-send me-1"></i>Receive &amp; Send</span>
                                {{else}}<span class="badge bg-secondary"><i class="bi bi-inbox me-1"></i>Receive Only</span>{{end}}
                            </td>
                            <td><small class="text-muted">{{strftime "%Y-%m-%d %H:%M" .CreatedAt}}</small></td>
                            <td>
                                <form method="post" action="/pymta-manager/mailboxes/{{$.mailbox.ID}}/aliases/{{.ID}}/remove" class="d-inline">
                                    <button type="submit" class="btn btn-outline-danger btn-sm" title="Remove" data-confirm="Remove alias {{.Email}}?"><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-signpost-split text-muted" style="font-size: 4rem;"></i>
                <h4 class="text-muted mt-3">No aliases yet</h4>
                <p class="text-muted">Add one above to let this mailbox receive mail at another address.</p>
            </div>
        {{end}}
    </div>
</div>
{{end}}
