// Start save settings JS
function ai_app_onsite_save_email_settings() {

    // Clear all previous error messages
    document.querySelectorAll('.fields-error').forEach(function (error) {
        error.textContent = '';
    });

    // Get form fields
    var email_enabled = document.getElementById("email_enabled").checked;
    var email_address = document.getElementById("email-input").value;

    // Collect error messages
    var errors = [];
    if (!email_enabled) {
        errors.push('Please check the checkbox first.');
    }

    // Validate email input if email is enabled
    if (email_enabled && (!email_address || !validateEmail(email_address))) {
        errors.push('Please enter a valid email address.');
    }

    // If there are errors, display them and return
    if (errors.length > 0) {
        var messageContainer = document.getElementById('Email-settings-msg-container');
        // Clear previous messages
        messageContainer.innerHTML = '';
        errors.forEach(function (error) {
            messageContainer.innerHTML += '<span style="color: red;">' + error + '</span><br>';
        });
        return;
    }

    // Show loader
    var loader = document.getElementById('loader');
    loader.style.display = 'flex';

    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            // Hide loader
            loader.style.display = 'none';

            // Show success or error message
            var messageContainer = document.getElementById('Email-settings-msg-container');
            setTimeout(function () {
                if (xhr.status === 200) {
                    var response = JSON.parse(xhr.responseText);
                    if (response.success) {
                        messageContainer.innerHTML = '<span style="color: green;">' + response.data + '</span>';
                    } else {
                        messageContainer.innerHTML = '<span style="color: red;">Error: ' + response.error + '</span>';
                    }
                } else {
                    messageContainer.innerHTML = '<span style="color: red;">Error: Failed to save data</span>';
                }

                // Hide message after 3 seconds
                setTimeout(function () {
                    messageContainer.innerHTML = '';
                }, 3000);
            }, 2000);
        }
    };

    xhr.open('POST', myPluginAjax.ajaxurl, true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

    // Include nonce field value
    var nonce = document.getElementById("ai_app_onsite_email_settings_nonce_field").value;

    // Customize data to send to the server
    var data = 'action=ai_app_onsite_save_email_setting&email_enabled=' + email_enabled +
        '&email_address=' + encodeURIComponent(email_address) +
        '&ai_app_onsite_email_settings_nonce_field=' + nonce;

    xhr.send(data);
}

// Email validation function
function validateEmail(email) {
    var re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return re.test(email);
}



//=======================End save plugin settings js===========================

//Start get plugin setting from db js
function ai_app_onsite_get_email_settings() {

}
ai_app_onsite_get_email_settings();
