{{define "title"}}Add Sender - Email Server{{end}}

{{define "content"}}
<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 mx-auto">
            <div class="card">
                <div class="card-header"><h4 class="mb-0"><i class="bi bi-person-plus me-2"></i>Add New Sender</h4></div>
                <div class="card-body">
                    <form method="POST">
                        <div class="mb-3">
                            <label for="local_part" class="form-label">Email Address</label>
                            <div class="input-group">
                                <input type="text" class="form-control" id="local_part" name="local_part" required placeholder="user"
                                       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 class="form-text">The sender always belongs to the domain selected here — this can't be typed as free text, so it can't drift from the domain it's assigned to.</div>
                        </div>
                        <div class="mb-3">
                            <label for="password" class="form-label">Password</label>
                            <input type="password" class="form-control" id="password" name="password" required minlength="6">
                            <div class="form-text">Minimum 6 characters</div>
                        </div>
                        <div class="mb-4">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="can_send_as_domain" name="can_send_as_domain">
                                <label class="form-check-label" for="can_send_as_domain"><strong>Domain Sender</strong></label>
                                <div class="form-text">If checked, sender can send emails as any address in their domain. Otherwise, sender can only send as their own email address.</div>
                            </div>
                        </div>
                        <div class="mb-3">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="store_message_content" name="store_message_content">
                                <label class="form-check-label" for="store_message_content"><strong>Store Full Message Content</strong></label>
                                <div class="form-text">If enabled, the full message body and attachments will be stored and viewable in logs. Otherwise, only headers and subject are stored.</div>
                            </div>
                        </div>
                        <div class="alert alert-info">
                            <h6 class="alert-heading"><i class="bi bi-info-circle me-2"></i>Permission Levels</h6>
                            <ul class="mb-0">
                                <li><strong>Regular Sender:</strong> Can only send emails from their own email address</li>
                                <li><strong>Domain Sender:</strong> Can send emails from any address in their domain</li>
                            </ul>
                        </div>
                        <div class="d-flex justify-content-between">
                            <a href="/pymta-manager/senders" class="btn btn-secondary"><i class="bi bi-arrow-left me-2"></i>Back to Senders</a>
                            <button type="submit" class="btn btn-success"><i class="bi bi-person-plus me-2"></i>Add Sender</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{{end}}

{{define "extra_js"}}
<script>
    document.getElementById('can_send_as_domain').addEventListener('change', function(e) {
        const isChecked = e.target.checked;
        const domainSelect = document.getElementById('domain_id');
        const selectedDomain = domainSelect.options[domainSelect.selectedIndex]?.text || 'domain.com';
        const helpText = e.target.closest('.form-check').querySelector('.form-text');
        if (isChecked) {
            helpText.innerHTML = `User can send as any address in ${selectedDomain}`;
        } else {
            helpText.innerHTML = 'User can only send as their own email address.';
        }
    });
    document.getElementById('domain_id').addEventListener('change', function(e) {
        const checkbox = document.getElementById('can_send_as_domain');
        if (checkbox.checked) { checkbox.dispatchEvent(new Event('change')); }
    });
</script>
{{end}}
