/**
 * =============================================================================
 * @package         Blue Raven (Free Version)
 * @Plugin URI      https://enterrahost.com/plugins/blue-raven
 * 
 * @Author          EnterraHost
 * @Author URI      https://enterrahost.com
 * @License         GPL-2.0-or-later
 *  
 * @Copyright       (c) 2025 EnterraHost
 * @Contents        MAIN User JS 
 *
 * =============================================================================
 * 
 * || INDEX:
 * ||   - LICENSE FORM
 * ||   
 * =============================================================================
 */

jQuery(function ($) {

/*
============================================================
    LICENSE FORM
============================================================
*/

    $(document).on('submit', '#license-form', function (e) {
        e.preventDefault();

        const form = this; 
        const formData = new FormData(form);
        formData.append('action', 'blue_raven_validate_license');
        formData.append('nonce', blueRavenData.nonce); 
        const responseEl = document.getElementById('license-response');
    
        fetch(blueRavenData.urls.licensingValidationUrl, {
            method: "POST",
            body: formData
        })
        .then(response => {
            if (!response.ok) throw new Error("HTTP status " + response.status);
            return response.json();
        })
        .then(data => {
            if (data.success) {
                $('.enterra-menu-user-icon').click();
            } else {
                responseEl.innerText = data.data || 'Something went wrong';
            }
        })
        .catch(err => {
            responseEl.innerText = "Something went wrong.";
        });
    });
});
