
jQuery(document).ready(function($) {
    
    // Capture Payment button click handler
    $('.convesiopay-capture-btn').on('click', function() {
        const $button = $(this);
        const $cancelButton = $('.convesiopay-cancel-btn');
        const $loadingIndicator = $('.convesiopay-loading');
        
        // Get data from button attributes
        const orderId = $button.data('order-id');
        const paymentId = $button.data('payment-id');
        const amount = $button.data('amount');
        const currency = $button.data('currency');
        
        
        // Disable buttons and show loading
        $button.prop('disabled', true);
        $cancelButton.prop('disabled', true);
        $loadingIndicator.show();
        
        // Make AJAX request to capture payment
        $.ajax({
            url: convesiopayManualCapture.ajaxUrl,
            type: 'POST',
            data: {
                action: 'peachpay_convesiopay_capture_payment',
                nonce: convesiopayManualCapture.nonce,
                order_id: orderId,
                payment_id: paymentId,
                amount: amount,
                currency: currency
            },
            success: function(response) {
                
                if (response.success) {
                    // Show success message
                    const message = response.data.message || 'Payment captured successfully';
                    showMessage(message, 'success');
                    
                    // Hide the capture/cancel buttons since payment is now captured
                    $('.convesiopay-capture-btn, .convesiopay-cancel-btn').fadeOut(300);
                    
                    // Refresh the page after 2 seconds to show updated order status
                    setTimeout(function() {
                        location.reload();
                    }, 2000);
                    
                } else {
                    // Show error message
                    const errorMessage = response.data.message || 'Failed to capture payment';
                    showMessage(errorMessage, 'error');
                    
                    // Re-enable buttons
                    $button.prop('disabled', false);
                    $cancelButton.prop('disabled', false);
                }
            },
            error: function(xhr, status, error) {
                console.error('ConvesioPay Manual Capture: AJAX error', error);
                showMessage('Network error occurred while capturing payment', 'error');
                
                // Re-enable buttons
                $button.prop('disabled', false);
                $cancelButton.prop('disabled', false);
            },
            complete: function() {
                // Hide loading indicator
                $loadingIndicator.hide();
            }
        });
    });
    
    // Cancel Payment button click handler
    $('.convesiopay-cancel-btn').on('click', function() {
        const $button = $(this);
        const $captureButton = $('.convesiopay-capture-btn');
        const $loadingIndicator = $('.convesiopay-loading');
        
        // Confirm cancellation
        if (!confirm('Are you sure you want to cancel this payment? This action cannot be undone.')) {
            return;
        }
        
        // Get data from button attributes
        const orderId = $button.data('order-id');
        const paymentId = $button.data('payment-id');
        
        
        // Disable buttons and show loading
        $button.prop('disabled', true);
        $captureButton.prop('disabled', true);
        $loadingIndicator.show();
        
        // Make AJAX request to cancel payment
        $.ajax({
            url: convesiopayManualCapture.ajaxUrl,
            type: 'POST',
            data: {
                action: 'peachpay_convesiopay_cancel_payment',
                nonce: convesiopayManualCapture.nonce,
                order_id: orderId,
                payment_id: paymentId
            },
            success: function(response) {
                
                if (response.success) {
                    // Show success message
                    const message = response.data.message || 'Payment cancelled successfully';
                    showMessage(message, 'success');
                    
                    // Hide the capture/cancel buttons since payment is now cancelled
                    $('.convesiopay-capture-btn, .convesiopay-cancel-btn').fadeOut(300);
                    
                    // Refresh the page after 2 seconds to show updated order status
                    setTimeout(function() {
                        location.reload();
                    }, 2000);
                    
                } else {
                    // Show error message
                    const errorMessage = response.data.message || 'Failed to cancel payment';
                    showMessage(errorMessage, 'error');
                    
                    // Re-enable buttons
                    $button.prop('disabled', false);
                    $captureButton.prop('disabled', false);
                }
            },
            error: function(xhr, status, error) {
                console.error('ConvesioPay Manual Capture: AJAX error', error);
                showMessage('Network error occurred while cancelling payment', 'error');
                
                // Re-enable buttons
                $button.prop('disabled', false);
                $captureButton.prop('disabled', false);
            },
            complete: function() {
                // Hide loading indicator
                $loadingIndicator.hide();
            }
        });
    });
    
    // Helper function to show messages
    function showMessage(message, type) {
        // Remove any existing messages
        $('.convesiopay-message').remove();
        
        // Create message element
        const messageClass = type === 'success' ? 'notice notice-success' : 'notice notice-error';
        const messageHtml = '<div class="convesiopay-message ' + messageClass + ' is-dismissible" style="margin: 10px 0;">' +
                           '<p><strong>' + message + '</strong></p>' +
                           '<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>' +
                           '</div>';
        
        // Insert message after the capture/cancel buttons
        $('.convesiopay-cancel-btn').after(messageHtml);
        
        // Handle dismiss button
        $('.notice-dismiss').on('click', function() {
            $(this).parent().fadeOut();
        });
        
        // Auto-hide success messages after 5 seconds
        if (type === 'success') {
            setTimeout(function() {
                $('.convesiopay-message').fadeOut();
            }, 5000);
        }
    }
});