{{define "title"}}Edit Domain{{end}}

{{define "content"}}
<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 offset-md-2">
            <div class="card">
                <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
                    <h4 class="mb-0"><i class="bi bi-pencil me-2"></i>Edit Domain</h4>
                    <a href="/pymta-manager/domains" class="btn btn-light btn-sm"><i class="bi bi-arrow-left me-1"></i>Back to Domains</a>
                </div>
                <div class="card-body">
                    <form method="POST" class="needs-validation" novalidate>
                        <div class="mb-3">
                            <label for="domain_name" class="form-label"><i class="bi bi-globe me-1"></i>Domain Name</label>
                            <input type="text" class="form-control" id="domain_name" name="domain_name" value="{{.domain.DomainName}}" placeholder="example.com"
                                   pattern="^[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?)*$" required>
                            <div class="invalid-feedback">Please provide a valid domain name.</div>
                            <div class="form-text"><i class="bi bi-info-circle me-1"></i>Enter a fully qualified domain name (e.g., example.com)</div>
                        </div>

                        <div class="mb-3 form-check">
                            <input type="checkbox" class="form-check-input" id="mfa_exempt" name="mfa_exempt" {{if .domain.MFAExempt}}checked{{end}}>
                            <label class="form-check-label" for="mfa_exempt">Exempt this domain's mailboxes from mandatory MFA</label>
                            <div class="form-text">Only relevant if "Enforce MFA for Mailboxes" is turned on in Server Settings.</div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="alert alert-info">
                                    <h6><i class="bi bi-info-circle me-1"></i>Current Status</h6>
                                    <p class="mb-1"><strong>Status:</strong>
                                        {{if .domain.IsActive}}<span class="badge bg-success">Active</span>{{else}}<span class="badge bg-danger">Inactive</span>{{end}}
                                    </p>
                                    <p class="mb-0"><strong>Created:</strong> {{strftime "%Y-%m-%d %H:%M:%S" .domain.CreatedAt}}</p>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="alert alert-warning">
                                    <h6><i class="bi bi-exclamation-triangle me-1"></i>Note</h6>
                                    <p class="mb-0">Changing the domain name will affect all associated users, IP addresses, and DKIM keys. Make sure to update your DNS records accordingly.</p>
                                </div>
                            </div>
                        </div>

                        <div class="d-flex justify-content-between">
                            <a href="/pymta-manager/domains" class="btn btn-secondary"><i class="bi bi-x me-1"></i>Cancel</a>
                            <button type="submit" class="btn btn-primary"><i class="bi bi-save me-1"></i>Update Domain</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
(function() {
    'use strict';
    window.addEventListener('load', function() {
        var forms = document.getElementsByClassName('needs-validation');
        Array.prototype.filter.call(forms, function(form) {
            form.addEventListener('submit', function(event) {
                if (form.checkValidity() === false) { event.preventDefault(); event.stopPropagation(); }
                form.classList.add('was-validated');
            }, false);
        });
    }, false);
})();
document.getElementById('domain_name').addEventListener('input', function(e) {
    const value = e.target.value.toLowerCase();
    e.target.value = value;
    const domainRegex = /^[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?)*$/;
    if (value && !domainRegex.test(value)) { e.target.setCustomValidity('Invalid domain format'); } else { e.target.setCustomValidity(''); }
});
</script>
{{end}}
