{{define "title"}}Add IP Address - 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"><h6 class="mb-0"><i class="bi bi-geo-alt me-2"></i>Your Current IP</h6></div>
                <div class="card-body text-center">
                    <div class="fw-bold font-monospace fs-5 mb-2" id="current-ip"><span class="spinner-border spinner-border-sm me-2"></span>Detecting...</div>
                    <button type="button" class="btn btn-outline-primary btn-sm" onclick="useCurrentIP()"><i class="bi bi-arrow-up me-1"></i>Use This IP</button>
                </div>
            </div>
        </div>
    </div>
    <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-shield-plus me-2"></i>Add IP Address to Whitelist</h4></div>
                <div class="card-body">
                    <form method="POST">
                        <div class="mb-3">
                            <label for="ip_address" class="form-label">IP Address</label>
                            <input type="text" class="form-control font-monospace" id="ip_address" name="ip_address" required
                                   pattern="^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
                                   placeholder="192.168.1.100" value="{{.prefill_ip}}">
                            <div class="form-text">IPv4 address that will be allowed to send emails without authentication</div>
                        </div>
                        <div class="mb-4">
                            <label for="domain_id" class="form-label">Authorized Domain</label>
                            <select class="form-select" id="domain_id" name="domain_id" required>
                                <option value="">Select a domain...</option>
                                {{range .domains}}<option value="{{.ID}}">{{.DomainName}}</option>{{end}}
                            </select>
                            <div class="form-text">This IP will only be able to send emails for the selected domain</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.</div>
                            </div>
                        </div>
                        <div class="alert alert-warning">
                            <h6 class="alert-heading"><i class="bi bi-exclamation-triangle me-2"></i>Security Note</h6>
                            <ul class="mb-0">
                                <li>Only whitelist trusted IP addresses</li>
                                <li>This IP can send emails without username/password authentication</li>
                                <li>The IP is restricted to the selected domain only</li>
                                <li>Use static IP addresses for reliable access</li>
                            </ul>
                        </div>
                        <div class="d-flex justify-content-between">
                            <a href="/pymta-manager/ips" class="btn btn-secondary"><i class="bi bi-arrow-left me-2"></i>Back to IP List</a>
                            <button type="submit" class="btn btn-success"><i class="bi bi-shield-plus me-2"></i>Add to Whitelist</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{{end}}

{{define "extra_js"}}
<script>
    async function detectCurrentIP() {
        try {
            const response = await fetch('https://ifconfig.me/all.json');
            const data = await response.json();
            document.getElementById('current-ip').innerHTML = `<span class="text-primary">${data.ip_addr}</span>`;
        } catch (er) {
            try {
                const response = await fetch('https://httpbin.org/ip');
                const data = await response.json();
                document.getElementById('current-ip').innerHTML = `<span class="text-primary">${data.origin}</span>`;
            } catch (error) {
                document.getElementById('current-ip').innerHTML = '<span class="text-muted">Unable to detect</span>';
            }
        }
    }
    function useCurrentIP() {
        const currentIPElement = document.getElementById('current-ip');
        const ip = currentIPElement.textContent.trim();
        if (ip && ip !== 'Detecting...' && ip !== 'Unable to detect') {
            document.getElementById('ip_address').value = ip;
            document.getElementById('domain_id').focus();
        } else {
            showToast('Unable to detect current IP address', 'danger');
        }
    }
    document.getElementById('ip_address').addEventListener('input', function(e) {
        const ip = e.target.value;
        const ipPattern = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
        if (ip && !ipPattern.test(ip)) { e.target.setCustomValidity('Please enter a valid IPv4 address'); } else { e.target.setCustomValidity(''); }
    });
    detectCurrentIP();
</script>
{{end}}
