jQuery(document).ready(function($) {
    $('.custom-buy-now-button').on('click', function(e) {
        e.preventDefault();

        var productId = $(this).data('product-id');
        $.ajax({
            url: bwdcv_popup_ajax.ajax_url,
            type: 'POST',
            data: {
                action: 'bwdcv_add_to_cart_and_checkout',
                product_id: productId
            },
            success: function(response) {
                if (response.success) {
                    $.ajax({
                        url: bwdcv_popup_ajax.ajax_url,
                        type: 'POST',
                        data: {
                            action: 'bwdcv_load_checkout'
                        },
                        success: function(response) {
                            $('#checkout-popup, #popup-overlay').fadeIn();
                            if (response.success) {
                                $('#checkout-content').html(response.data);
                            } else {
                                $('#checkout-content').html('<p>Failed to load checkout form. Please try again.</p>');
                            }
                        },
                        error: function() {
                            $('#checkout-content').html('<p>Error loading checkout form. Please try again.</p>');
                        }
                    });
                } else {
                    alert(response.data.message);
                }
            },
            error: function() {
                alert('Error adding product to cart.');
            }
        });
    });
    $('#close-popup').on('click', function() {
        $('#checkout-popup, #popup-overlay').fadeOut();
    });
});


jQuery(document).ready(function($) {
    $('#place-order').on('click', function(e) {
        e.preventDefault();
        var checkoutData = $('#checkout-form').serialize(); 
        $.ajax({
            url: bwdcv_popup_ajax.ajax_url,
            type: 'POST',
            data: {
                action: 'bwdcv_process_checkout',
                checkout_data: checkoutData
            },
            success: function(response) {
                if (response.success) {
                    window.location.href = response.data.redirect_url;
                } else {
                    alert(response.data.message);
                }
            },
            error: function() {
                alert('There was an error processing your order.');
            }
        });
    });
});
jQuery('#place-order').on('click', function(e) {
    e.preventDefault();
    $.ajax({
        url: bwdcv_popup_ajax.ajax_url,
        type: 'POST',
        data: {
            action: 'woocommerce_checkout',
            billing_first_name: bwdcv_popup_ajax.display_name,
            billing_email: bwdcv_popup_ajax.user_email,
        },
        success: function(response) {
            if (response.result === 'success') {
                window.location.href = response.redirect;
            } else {
                alert('Error processing your order.');
            }
        }
    });
});




