/**
 * Order Bump JavaScript
 * Handles order bump functionality in checkout
 */
jQuery(document).ready(function($) {
    const { __ } = (window.wp && window.wp.i18n) ? window.wp.i18n : { __: (s) => s };
    // Handle order bump button click
    $(document).on('click', '.ckfylt25-order-bump-btn', function(e) {
        e.preventDefault();
        
        var $button = $(this);
        var $container = $button.closest('.ckfylt25-order-bump-container');
        var productId = $button.data('product-id');
        
        // Disable button and show loading state
        $button.prop('disabled', true);
        $container.addClass('adding-to-cart');
        
        // Make AJAX request to add product to cart
        $.ajax({
            url: ckfylt25_ajax.ajax_url,
            type: 'POST',
            data: {
                action: 'ckfylt25_add_order_bump_to_cart',
                product_id: productId,
                nonce: ckfylt25_ajax.nonce
            },
            success: function(response) {
                if (response.success) {
                    // Update button to show success
                    $button.removeClass('ckfylt25-order-bump-btn').addClass('button-success');
                    $button.text('✓ ' + __('Added to Order', 'checkoutify-lite'));
                    
                    // Trigger checkout update
                    $(document.body).trigger('update_checkout');
                    
                    // Hide the order bump after a delay
                    setTimeout(function() {
                        $container.fadeOut(300, function() {
                            $(this).remove();
                        });
                    }, 2000);
                } else {
                    // Show error message
                    alert(__('Error:', 'checkoutify-lite') + ' ' + (response.data || __('Failed to add product to cart', 'checkoutify-lite')));
                    
                    // Re-enable button
                    $button.prop('disabled', false);
                    $container.removeClass('adding-to-cart');
                }
            },
            error: function() {
                // Show error message
                alert(__('Error:', 'checkoutify-lite') + ' ' + __('Failed to add product to cart', 'checkoutify-lite'));
                
                // Re-enable button
                $button.prop('disabled', false);
                $container.removeClass('adding-to-cart');
            }
        });
    });
    
    // Handle close button click
    $(document).on('click', '.ckfylt25-order-bump-container button[onclick*="display=\'none\'"]', function(e) {
        e.preventDefault();
        $(this).closest('.ckfylt25-order-bump-container').fadeOut(300);
    });
});