(function($) {
    'use strict';

    $(document).ready(function() {
        // Handle import button click
        $('#pagevico-start-import').on('click', function(e) {
            e.preventDefault();
            
            // Get form data
            const formData = new FormData($('#pagevico-import-form')[0]);
            formData.append('action', 'pagevico_import_apvc');
            formData.append('nonce', pagevicoImport.nonce);
            
            // UI feedback
            $('#pagevico-start-import').prop('disabled', true);
            $('#import-spinner').removeClass('dnone');
            
            // Clear previous notices
            $('.import-notice').remove();
            
            // Send AJAX request
            $.ajax({
                url: pagevicoImport.ajaxUrl,
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    if (response.success) {
                        // Show success message
                        const notice = $('<div class="alert alert-success import-notice">' + response.data.message + '</div>');
                        $('#pagevico-import-form').before(notice);
                        
                        // Show info about reloading
                        const infoNotice = $('<div class="alert alert-info import-notice">' + pagevicoImport.importRunning + '</div>');
                        notice.after(infoNotice);
                        
                        // Reload page after 2 seconds to show progress
                        setTimeout(function() {
                            window.location.reload();
                        }, 2000);
                    } else {
                        // Show error
                        const notice = $('<div class="alert alert-danger import-notice">' + (response.data || pagevicoImport.importError) + '</div>');
                        $('#pagevico-import-form').before(notice);
                        
                        // Re-enable form
                        $('#pagevico-start-import').prop('disabled', false);
                        $('#import-spinner').addClass('dnone');
                    }
                },
                error: function(xhr, status, error) {
                    // Show more detailed error
                    console.error('AJAX Error:', error, xhr.responseText);
                    
                    // Show generic error message
                    const notice = $('<div class="alert alert-danger import-notice">' + pagevicoImport.importError + '</div>');
                    $('#pagevico-import-form').before(notice);
                    
                    // Re-enable form
                    $('#pagevico-start-import').prop('disabled', false);
                    $('#import-spinner').addClass('dnone');
                }
            });
        });

        // Auto-refresh for progress updates
        if ($('.alert-info:contains("Import is currently running")').length > 0) {
            setTimeout(function() {
                window.location.reload();
            }, 10000); // Refresh every 10 seconds while import is running
        }
    });
})(jQuery); 