jQuery(document).ready(function () {
    // Function to update URL parameter
    function iafwzdesk_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
    }

    // Define all Settings sub-tabs
    const settingsTab = {
        section: "#iafwzdesk-settings-section",
        header: "#iafwzdesk-settings-header",
        menuValue: null
    };

    const formFiltersTab = {
        section: "#iafwzdesk-form-filters-section",
        header: "#iafwzdesk-form-filters-header",
        menuValue: 'form-filters'
    };

    const licenseTab = {
        section: "#iafwzdesk-license-section",
        header: "#iafwzdesk-license-header",
        menuValue: 'license'
    };

    const allSettingsTabs = [settingsTab, formFiltersTab, licenseTab];

    // Function to activate one sub-tab and deactivate the rest
    function iafwzdesk_toggleSections(activeTab) {
        allSettingsTabs.forEach(function (tab) {
            jQuery(tab.section).hide();
            jQuery(tab.header).removeClass("iafwzdesk-settings-heading-active");
        });
        jQuery(activeTab.section).show();
        jQuery(activeTab.header).addClass("iafwzdesk-settings-heading-active");
        iafwzdesk_updateURLParam('menu', activeTab.menuValue);
    }

    // Initialize based on URL menu parameter
    const menuParam = new URLSearchParams(window.location.search).get('menu');
    if (menuParam === 'license') {
        iafwzdesk_toggleSections(licenseTab);
    } else if (menuParam === 'form-filters') {
        iafwzdesk_toggleSections(formFiltersTab);
    } else {
        iafwzdesk_toggleSections(settingsTab);
    }

    // Click handlers for each sub-tab header
    jQuery(settingsTab.header).click(function () {
        iafwzdesk_toggleSections(settingsTab);
    });

    jQuery(formFiltersTab.header).click(function () {
        iafwzdesk_toggleSections(formFiltersTab);
    });

    jQuery(licenseTab.header).click(function () {
        iafwzdesk_toggleSections(licenseTab);
    });

    //-----------------Review Area
    function iafwzdesk_SendReviewAction(actionType, nonce) {
        jQuery.post(ajaxurl, {
            action: 'iafwzdesk_review_action',
            action_type: actionType,
            nonce: nonce
        }, function(response) {
            if (response.success) {
                jQuery('#iafwzdesk-review-notice').fadeOut();
            } else {
                alert('Error: ' + response.data.message);
            }
        }).fail(function() {
            alert('Request failed. Please try again.');
        });
    }

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

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

function iafwzdesk_showAccount()
{
    jQuery("#iafwzdesk-auth-popupOverlay").toggleClass("show");
}
