document.addEventListener('DOMContentLoaded', function () {
    console.log('DOMContentLoaded event triggered');

    const installButtons = document.querySelectorAll('.install-btn');
    const uninstallButtons = document.querySelectorAll('.uninstall-btn');

    console.log('Install buttons:', installButtons);
    console.log('Uninstall buttons:', uninstallButtons);

    // Handle Install button click
    installButtons.forEach(function (button) {
        console.log('Adding install listener to button', button);
        button.addEventListener('click', function (event) {
            event.preventDefault();  // Prevent default form submission or page reload
            console.log('Install button clicked');
            const iconSetId = this.getAttribute('data-icon-set-id');
            console.log('Icon Set ID:', iconSetId);

            const formData = new FormData();
            formData.append('action', 'install_icon_set');
            formData.append('icon_set_id', iconSetId);

            const xhr = new XMLHttpRequest();
            xhr.open('POST', mytheme_ajax.ajax_url, true);

            xhr.onload = function () {
                if (xhr.status === 200) {
                    const response = JSON.parse(xhr.responseText);
                    console.log(response);
                    if (response.success) {
                        alert('Icon set installed successfully!');
                    } else {
                        alert('Failed to install icon set: ' + response.data.message);
                    }
                } else {
                    alert('There was an error with the request.');
                }
            };

            xhr.send(formData);
        });
    });

    // Handle Uninstall button click
    uninstallButtons.forEach(function (button) {
        console.log('Adding uninstall listener to button', button);
        button.addEventListener('click', function (event) {
            event.preventDefault();  // Prevent default form submission or page reload
            console.log('Uninstall button clicked');
            const iconSetId = this.getAttribute('data-icon-set-id');
            console.log('Icon Set ID for uninstall:', iconSetId);

            const formData = new FormData();
            formData.append('action', 'uninstall_icon_set');
            formData.append('icon_set_id', iconSetId);

            const xhr = new XMLHttpRequest();
            xhr.open('POST', mytheme_ajax.ajax_url, true);

            xhr.onload = function () {
                if (xhr.status === 200) {
                    const response = JSON.parse(xhr.responseText);
                    console.log(response);
                    if (response.success) {
                        alert('Icon set uninstalled successfully!');
                    } else {
                        alert('Failed to uninstall icon set: ' + response.data.message);
                    }
                } else {
                    alert('There was an error with the request.');
                }
            };

            xhr.send(formData);
        });
    });
});
