$(document).ready(function () {
    $('#btnDownloadBackup').on('click', function () {
        const btn = $(this);
        const originalHtml = btn.html();

        Swal.fire({
            title: 'مکمل ویب سائٹ بیک اپ',
            text: 'بیک اپ فائل تیار کی جا رہی ہے، براہ کرم انتظار کریں...',
            icon: 'info',
            allowOutsideClick: false,
            showConfirmButton: false,
            didOpen: () => {
                Swal.showLoading();
            }
        });

        // Use window.location to trigger the download directly
        // This is safe even with large files as it doesn't block the UI thread differently
        // than an AJAX request that would then need to handle blob/download.
        // However, we can't easily detect "completion" of the download trigger
        // if headers are sent.

        // Wait a small bit to let the Swal show
        setTimeout(() => {
            window.location.href = 'APIs/full_web_backup.php';

            // Note: Since browsers don't provide a way to detect when a download started/finished
            // purely via window.location, we'll give it some time then close the swal
            // or we could use an iframe approach to handle it more gracefully.
            // For now, we'll keep the swal for 10 seconds or until the user manually 
            // closes it if we had a close button, but let's just close it after a reasonable delay
            // assuming the server starts sending bits.

            setTimeout(() => {
                Swal.close();
                Swal.fire({
                    title: 'کامیابی',
                    text: 'ڈاؤن لوڈ شروع ہو چکا ہے یا کچھ ہی دیر میں شروع ہو جائے گا۔',
                    icon: 'success',
                    timer: 3000
                });
            }, 5000);
        }, 500);
    });
});
