jQuery(document).ready(function ($) {
    $('.pdfinvx-download-pdf').on('click', function (e) {
        e.preventDefault();
        const $button = $(this);
        const url = $button.attr('href');
        const originalText = $button.text();

        $button
            .addClass('disabled')
            .text(pdfInvoiceXFrontend.i18n.generating)
            .prepend('<span class="spinner is-active"></span>');

        // Create a hidden iframe to trigger the download
        const iframe = $('<iframe/>', {
            style: 'display: none;',
            src: url
        }).appendTo('body');

        // Remove iframe after a short delay
        setTimeout(function () {
            iframe.remove();
            $button
                .removeClass('disabled')
                .text(originalText)
                .find('.spinner')
                .remove();
        }, 2000);
    });
});