jQuery(document).ready(function ($) {
    // Attach click event to the dismiss button
    $(document).on('click', '.notice[data-notice="get-start"] button.notice-dismiss', function () {
        // Dismiss the notice via AJAX
        $.ajax({
            type: 'POST',
            url: ajaxurl,
            data: {
                action: 'cricket_club_league_dismissed_notice',
            },
            success: function () {
                // Remove the notice on success
                $('.notice[data-notice="get-start"]').remove();
            }
        });
    });
});

// Plugin – AI Content Writer plugin activation
document.addEventListener('DOMContentLoaded', function () {
    const cricket_club_league_button = document.getElementById('install-activate-button');
    const cricket_club_league_config = window.installPluginData || window.pluginInstallerData;

    if (!cricket_club_league_button || !cricket_club_league_config) return;

    cricket_club_league_button.addEventListener('click', function (e) {
        e.preventDefault();

        const cricket_club_league_redirectUrl = cricket_club_league_button.getAttribute('data-redirect') || cricket_club_league_config.redirectUrl;

        cricket_club_league_button.textContent = 'Setting up plugins & demo...';

        const cricket_club_league_installData = new FormData();
        cricket_club_league_installData.append('action', 'install_and_activate_wordclever_plugin');
        cricket_club_league_installData.append('nonce', cricket_club_league_config.nonce);

        fetch(cricket_club_league_config.ajaxurl, {
            method: 'POST',
            body: cricket_club_league_installData,
        })
        .then(res => res.json())
        .then(res => {
            if (res.success) {
                window.location.href = cricket_club_league_redirectUrl;
            } else {
                alert('Activation error: ' + (res.data?.message || 'Unknown error'));
                cricket_club_league_button.textContent = 'Try Again';
            }
        })
        .catch(error => {
            alert('Request failed: ' + error.message);
            cricket_club_league_button.textContent = 'Try Again';
        });
    });
});