const config = Object.assign({}, window.appLocalizer);

export function getNonce() {
	return config.nonce;
}


export function getCore_file_directories() {
	return config.get_core_file_directories;
}
export function getInstalled_plugins() {
	return config.get_installed_plugins;
}
export function getInstalled_themes() {
	return config.get_installed_themes;
}
export function getExclusion_option() {
	return config.get_exclusion_option;
}


export function getDashboardData() {
	return config.dashboard_data;
}

export function isPro() {
	return config.isPro;
}
export function systemInfo() {
	return config.system_info;
}
export function getActivitylogs() {
	return config.get_all_activity_logs;
}
export function admin_list() {
	return config.admin_list;
}

export function formflow_status() {
	return config.formflow_status;
}
export function isWooactive() {
	return config.isWooactive;
}
export function cf7_status() {
	return config.cf7_status;
}

export function ran_setup_wizard() {
	return config.ran_setup_wizard;
}

export function debugMode() {
	return config.debugMode;
}

export function maintenannoticeMode() {
	return config.maintenannoticeMode;
}
export function globalhook() {
	return config.globalhook;
}
export function get_current_user_id() {
	return config.get_current_user_id;
}

export function getvisitorData() {
	return config.visitor_data;
}

export function getNoticeData() {
	return config.notice_settings;
}


/**
 * Site is Under Maintenance form response 
 */

document.addEventListener('DOMContentLoaded', function () {
    const form = document.getElementById('maintenance-query-form');

    if (!form) {
        return;
    }

    const messagesContainer = document.getElementById('form-messages');
    const submitButton = form.querySelector('input[type="submit"]');

    form.addEventListener('submit', function (e) {
        e.preventDefault(); // Prevent the default form submission

        // Clear previous messages
        messagesContainer.innerHTML = '';

        // Prepare to validate form fields
        let valid = true; // Flag to check if the form is valid

        // Check for required fields
        const emailField = document.getElementById('user_email');
        const messageField = document.getElementById('user_message');

        // Reset field styles
        emailField.style.border = '';
        messageField.style.border = '';

        // Validate email
        if (!emailField.value.trim()) {
            valid = false;
            emailField.style.border = '2px solid red'; // Highlight border in red
        }

        // Validate message
        if (!messageField.value.trim()) {
            valid = false;
            messageField.style.border = '2px solid red'; // Highlight border in red
        }

        // If form is not valid, show message and exit
        if (!valid) {
            messagesContainer.innerHTML = `<div class="message error">Please fill in all required fields.</div>`;
            return; // Exit the function
        }

        // Disable the submit button and change its text
        submitButton.disabled = true;
        submitButton.value = 'Sending...';

        // Prepare form data
        const formData = new FormData(form);

        // Convert FormData to a plain object
        const data = {};
        formData.forEach((value, key) => {
            data[key] = value;
        });

        // Prepare the URL for the REST API request
        const url = `${appLocalizer.wpntsUrl}/wpnts/v1/maintenance_mode`;

        // Use the Fetch API for the POST request
        fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-WP-NONCE': appLocalizer.nonce
            },
            body: JSON.stringify(data)
        })
        .then(response => response.json())
        .then(resData => {
            // Re-enable the submit button
            submitButton.disabled = false;
            submitButton.value = 'Submit Query'; // Reset button text

            if (resData.success) {
                // Display success message
                messagesContainer.innerHTML = `<div class="message success">${resData.message}</div>`;
                // Reset the form
                form.reset();
                // Reset field styles
                emailField.style.border = '';
                messageField.style.border = '';
            } else {
                // Display error message for failed responses
                messagesContainer.innerHTML = `<div class="message error">${resData.message || 'An error occurred.'}</div>`;
            }

            // Remove messages after 4 seconds
            setTimeout(() => {
                messagesContainer.innerHTML = '';
            }, 4000);
        })
        .catch(err => {
            console.error('Error:', err);
            messagesContainer.innerHTML = `<div class="message error">An unexpected error occurred. Please try again later.</div>`;
            // Re-enable the submit button
            submitButton.disabled = false;
            submitButton.value = 'Submit Query'; // Reset button text

            // Remove messages after 4 seconds
            setTimeout(() => {
                messagesContainer.innerHTML = '';
            }, 4000);
        });
    });
});





