jQuery(document).ready(function($) {
    // Handle form submissions with AJAX
    $('.aysa-ajax-form').on('submit', function(e) {
        e.preventDefault();
        
        var $form = $(this);
        var $submitBtn = $form.find('button[type="submit"]');
        var $spinner = $form.find('.spinner');
        
        // Show spinner
        $spinner.addClass('is-active');
        
        // Disable submit button
        $submitBtn.prop('disabled', true);
        
        $.ajax({
            url: aysa_ajax_object.ajax_url,
            type: 'POST',
            data: $form.serialize(),
            dataType: 'json',
            success: function(response) {
                if (response.success) {
                    // Handle success
                    if (response.data.message) {
                        alert(response.data.message);
                    }
                    
                    // Redirect if needed
                    if (response.data.redirect) {
                        window.location.href = response.data.redirect;
                    }
                    
                    // Reset form if needed
                    if (response.data.reset_form) {
                        $form[0].reset();
                    }
                } else {
                    // Handle error
                    if (response.data.message) {
                        alert(response.data.message);
                    }
                }
            },
            error: function(xhr, status, error) {
                alert('An error occurred: ' + error);
            },
            complete: function() {
                // Hide spinner
                $spinner.removeClass('is-active');
                
                // Re-enable submit button
                $submitBtn.prop('disabled', false);
            }
        });
    });
    
    // Handle action buttons with AJAX
    $(document).on('click', '.aysa-ajax-action', function(e) {
        e.preventDefault();
        
        var $btn = $(this);
        var $spinner = $btn.siblings('.spinner');
        
        if (!$spinner.length) {
            // If spinner doesn't exist next to the button, look in parent container
            $spinner = $btn.closest('.aysa-action-container').find('.spinner');
        }
        
        // Show spinner
        $spinner.addClass('is-active');
        
        // Disable button
        $btn.prop('disabled', true);
        
        $.ajax({
            url: aysa_ajax_object.ajax_url,
            type: 'POST',
            data: {
                action: $btn.data('action'),
                nonce: aysa_ajax_object.nonce,
                id: $btn.data('id'),
                context: $btn.data('context')
            },
            dataType: 'json',
            success: function(response) {
                if (response.success) {
                    // Handle success
                    if (response.data.message) {
                        alert(response.data.message);
                    }
                    
                    // Update UI if needed
                    if (response.data.html) {
                        $($btn.data('target')).html(response.data.html);
                    }
                    
                    // Refresh page if needed
                    if (response.data.refresh) {
                        location.reload();
                    }
                } else {
                    // Handle error
                    if (response.data.message) {
                        alert(response.data.message);
                    }
                }
            },
            error: function(xhr, status, error) {
                alert('An error occurred: ' + error);
            },
            complete: function() {
                // Hide spinner
                $spinner.removeClass('is-active');
                
                // Re-enable button
                $btn.prop('disabled', false);
            }
        });
    });
}); 