jQuery(document).ready(function($) {
    // Handle AJAX operations for data table actions
    $(document).on('click', '.aysa-table-action', function(e) {
        e.preventDefault();
        
        var $button = $(this);
        var $spinner = $button.find('.spinner');
        var $row = $button.closest('tr');
        
        // Show spinner
        $spinner.addClass('is-active');
        
        // Disable button
        $button.prop('disabled', true);
        
        // Collect data from button attributes
        var action = $button.data('action');
        var itemId = $button.data('id');
        var itemType = $button.data('type');
        
        $.ajax({
            url: aysa_ajax_object.ajax_url,
            type: 'POST',
            data: {
                action: action,
                id: itemId,
                type: itemType,
                nonce: aysa_ajax_object.nonce
            },
            success: function(response) {
                if (response.success) {
                    // Handle success actions
                    if (response.data.message) {
                        alert(response.data.message);
                    }
                    
                    // Remove row if delete action
                    if (action.includes('delete') && response.data.removed) {
                        $row.fadeOut(300, function() {
                            $(this).remove();
                        });
                    }
                    
                    // Update row content if provided
                    if (response.data.html) {
                        $row.html(response.data.html);
                    }
                    
                    // Update button text/state if needed
                    if (response.data.button_text) {
                        $button.text(response.data.button_text);
                    }
                    
                    // Refresh page if requested
                    if (response.data.refresh) {
                        location.reload();
                    }
                } else {
                    // Handle error
                    alert(response.data.message || 'Error performing action');
                }
            },
            error: function() {
                alert('An error occurred while processing your request');
            },
            complete: function() {
                // Hide spinner
                $spinner.removeClass('is-active');
                
                // Re-enable button
                $button.prop('disabled', false);
            }
        });
    });
    
    // Handle bulk actions
    $('#aysa-bulk-action-form').on('submit', function(e) {
        e.preventDefault();
        
        var $form = $(this);
        var $submitBtn = $form.find('button[type="submit"]');
        var $spinner = $form.find('.spinner');
        var action = $form.find('select[name="bulk_action"]').val();
        
        if (!action || action === '-1') {
            alert('Please select an action');
            return;
        }
        
        var selectedItems = [];
        $form.find('input[name="bulk_select[]"]:checked').each(function() {
            selectedItems.push($(this).val());
        });
        
        if (selectedItems.length === 0) {
            alert('Please select at least one item');
            return;
        }
        
        // Confirm destructive actions
        if (action.includes('delete') && !confirm('Are you sure you want to delete the selected items?')) {
            return;
        }
        
        // Show spinner
        $spinner.addClass('is-active');
        
        // Disable submit button
        $submitBtn.prop('disabled', true);
        
        $.ajax({
            url: aysa_ajax_object.ajax_url,
            type: 'POST',
            data: {
                action: 'aysa_bulk_action',
                bulk_action: action,
                items: selectedItems,
                nonce: aysa_ajax_object.nonce
            },
            success: function(response) {
                if (response.success) {
                    if (response.data.message) {
                        alert(response.data.message);
                    }
                    
                    // Refresh the page to show changes
                    if (response.data.refresh !== false) {
                        location.reload();
                    }
                } else {
                    alert(response.data.message || 'Error performing bulk action');
                }
            },
            error: function() {
                alert('An error occurred while processing bulk action');
            },
            complete: function() {
                // Hide spinner
                $spinner.removeClass('is-active');
                
                // Re-enable submit button
                $submitBtn.prop('disabled', false);
            }
        });
    });
}); 