$ = jQuery;
(function ($) {
    'use strict';
    new DataTable("#user_orders");
    new DataTable("#user_invoices_dashboard", {
        info: false,
        paging: false,
        bFilter: false
    });
    new DataTable("#user_orders_dashboard", {
        info: false,
        paging: false,
        bFilter: false
    });
    $(".add_new_address").click(function () {
        $(".old_addresses").hide();
        $(".new_billing_address_form").show();
    });
    $('.submit_cart').on('click', function (e) {
        e.preventDefault();
        $("#payment-form").submit();
    });

    $("#payment-form").on('submit', function (e) {
        e.preventDefault();

        // Show spinner and disable button
        const btn = $('.submit_cart');
        const spinner = $('#bankSpinner');
        btn.prop('disabled', true);
        spinner.removeClass('d-none');

        var formData = $(this).serialize();
        var nonce = dspp_nonces.dspp_bits_payment_processor;
        var ajaxUrl = dspp_nonces.ajax_url;

        $.ajax({
            type: 'post',
            url: ajaxUrl,
            data: {
                action: 'dspp_bits_payment_processor',
                nonce: nonce,
                formData: formData
            },
            success: function (response) {
                if (response.success) {
                    location.href = response.url;
                } else {
                    alert('We are unable to process your order right now! Please try again later.');
                    btn.prop('disabled', false);
                    spinner.addClass('d-none');
                }
            },
            error: function () {
                alert('Something went wrong.');
                btn.prop('disabled', false);
                spinner.addClass('d-none');
            }
        });
    });
    $('#continueToPaymentLink').on('click', function (e) {
        const spinner = $('#linkSpinner');
        const btn = $(this);

        // Optional: prevent multiple clicks
        btn.addClass('disabled');
        spinner.removeClass('d-none');
    });

})(jQuery);
function dspp_submitForm() {
    document.getElementById('update-user-profile-image').submit();
}