jQuery(document).ready(function () {
    // Function to update URL parameter
    function iafwzcai_updateURLParam(param, value) {
        const url = new URL(window.location.href);
        if (value) {
            url.searchParams.set(param, value); // Add or update parameter
        } else {
            url.searchParams.delete(param); // Remove parameter
        }
        window.history.pushState({}, '', url); // Update the URL without reloading
    }

    // Function to toggle sections and update the URL
    function iafwzcai_toggleSections(activeTab, inactiveTab, menuValue) {
        jQuery(activeTab.section).show();
        jQuery(activeTab.header).addClass("iafwzcai-settings-heading-active");
        jQuery(inactiveTab.section).hide();
        jQuery(inactiveTab.header).removeClass("iafwzcai-settings-heading-active");
        iafwzcai_updateURLParam('menu', menuValue); // Update the URL
    }

    // Define tabs
    const settingsTab = {
        section: "#iafwzcai-settings-section",
        header: "#iafwzcai-settings-header",
        menuValue: null // No parameter for settings
    };

    const licenseTab = {
        section: "#iafwzcai-license-section",
        header: "#iafwzcai-license-header",
        menuValue: 'license' // 'license' parameter for license tab
    };

    // Check the menu parameter in the URL
    const menuParam = new URLSearchParams(window.location.search).get('menu');
    if (menuParam === 'license') {
        iafwzcai_toggleSections(licenseTab, settingsTab, licenseTab.menuValue);
    } else {
        // Default to Settings
        iafwzcai_toggleSections(settingsTab, licenseTab, settingsTab.menuValue);
    }

    // Click handlers
    jQuery(settingsTab.header).click(function () {
        iafwzcai_toggleSections(settingsTab, licenseTab, settingsTab.menuValue);
    });

    jQuery(licenseTab.header).click(function () {
        iafwzcai_toggleSections(licenseTab, settingsTab, licenseTab.menuValue);
    });
    //-----------------Review Area
    function iafwzcai_SendReviewAction(actionType, nonce) {
        jQuery.post(ajaxurl, { 
            action: 'iafwzcai_review_action', 
            action_type: actionType,
            nonce: nonce
        }, function(response) {
            if (response.success) {
                jQuery('#iafwzcai-review-notice').fadeOut();
            } else {
                alert('Error: ' + response.data.message);
            }
        }).fail(function() {
            alert('Request failed. Please try again.');
        });
    }

    // Handle "Remind Me Later" click
    jQuery('#iafwzcai-review-later').on('click', function(e) {
        e.preventDefault();
        let nonce = jQuery(this).data('nonce');
        iafwzcai_SendReviewAction('later', nonce);
    });

    // Handle "Already Provided" click
    jQuery('#iafwzcai-review-dismiss').on('click', function(e) {
        e.preventDefault();
        let nonce = jQuery(this).data('nonce');
        iafwzcai_SendReviewAction('dismiss', nonce);
    });
});

function iafwzcai_showAccount()
{
    jQuery("#iafwzcai-auth-popupOverlay").toggleClass("show");
}
