/**
 * Handles all AJAX interactions for the Payments admin page.
 * Relies on the 'em_payment_ajax_object' localized from PHP.
 * @param {object} jQuery - jQuery
 */
jQuery(function (jQuery) {

    // The static wrapper for the entire payments page
    var pageWrapper = jQuery('#em-payments-page-wrapper');
    var modalContainer = jQuery('#modal-payment-manager');

    // If the main wrapper for this page doesn't exist, do nothing.
    if (!pageWrapper.length) {
        return;
    }

    // --- EVENT LISTENERS using DELEGATION ---

    // Listener for the "View" button on any payment row
    pageWrapper.on('click', '.payment-view-btn', function () {
        var payment_id = jQuery(this).data('id');

        jQuery.post(em_payment_ajax_object.ajax_url, {
            action: 'em_get_payment_view',
            nonce: em_payment_ajax_object.view_nonce,
            id: payment_id
        }).done(function (response) {
            if (response.success) {
                modalContainer.html(response.data.html).show();
            } else {
                alert('Error: ' + (response.data.message || 'Could not retrieve payment details.'));
            }
        });
    });

    // Listener for the "Delete" button on any payment row
    pageWrapper.on('click', '.payment-delete-btn', function () {
        if (!confirm('Are you sure you want to delete this payment record? This action cannot be undone.')) {
            return;
        }

        var payment_id = jQuery(this).data('id');
        var row = jQuery('#record-' + payment_id); // Assumes your <tr> has id="record-123"

        jQuery.post(em_payment_ajax_object.ajax_url, {
            action: 'em_delete_payment',
            nonce: em_payment_ajax_object.delete_nonce,
            id: payment_id
        }).done(function (response) {
            if (response.success) {
                row.fadeOut(500, function () {
                    jQuery(this).remove();
                });
            } else {
                alert('Error: ' + (response.data.message || 'Could not delete payment.'));
            }
        });
    });


    // --- BULK ACTION LISTENERS ---

    // Listener for the main "Select All" checkbox
    pageWrapper.on('click', '#select-all-checkbox', function () {
        pageWrapper.find('.table-item-checkbox').prop('checked', jQuery(this).is(':checked'));
    });

    // Listener for the "Delete Selected" button
    pageWrapper.on('click', '#bulk-delete-button', function () {
        var selected_ids = [];
        pageWrapper.find('.table-item-checkbox:checked').each(function () {
            selected_ids.push(jQuery(this).val());
        });

        if (selected_ids.length === 0) {
            alert('Please select at least one payment to delete.');
            return;
        }

        if (!confirm('Are you sure you want to delete all selected payments?')) {
            return;
        }

        var nonce = em_payment_ajax_object.delete_all_nonce;

        jQuery.post(em_payment_ajax_object.ajax_url, {
            action: 'em_delete_all_payments', // Correct action for payments
            nonce: nonce,
            ids: selected_ids
        }).done(function (response) {
            if (response.success) {
                jQuery.each(selected_ids, function (index, id) {
                    jQuery('#record-' + id).fadeOut(500, function () { jQuery(this).remove(); });
                });
            } else {
                alert('Error: ' + (response.data.message || 'Could not delete payments.'));
            }
        });
    });


    // Generic listener to close any modal on the page
    jQuery('body').on('click', '#modal-payment-manager .close, #modal-payment-manager .close-modal', function () {
        modalContainer.hide().html('');
    });

});