jQuery(document).ready(function () {
    // Function to update URL parameter
    function iafwzc_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 iafwzc_toggleSections(activeTab, inactiveTab, menuValue) {
        jQuery(activeTab.section).show();
        jQuery(activeTab.header).addClass("iafwzc-settings-heading-active");
        jQuery(inactiveTab.section).hide();
        jQuery(inactiveTab.header).removeClass("iafwzc-settings-heading-active");
        iafwzc_updateURLParam('menu', menuValue); // Update the URL
    }

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

    const licenseTab = {
        section: "#iafwzc-license-section",
        header: "#iafwzc-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') {
        iafwzc_toggleSections(licenseTab, settingsTab, licenseTab.menuValue);
    } else {
        // Default to Settings
        iafwzc_toggleSections(settingsTab, licenseTab, settingsTab.menuValue);
    }

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

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

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

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

function iafwzc_showAccount()
{
    jQuery("#iafwzc-auth-popupOverlay").toggleClass("show");
}
