/**
 * Agenistrator - Progress Bar Handler.
 *
 * Handles progress bar cancel operations for admin pages.
 *
 * @package   Agenistrator
 * @since     1.0.0
 * @author    Alex Ruban <alex@maximum.guru>
 * @author    Maximum Software Inc <contact@maximum.software>
 * @license   GPL-3.0-only
 * @link      https://agenistrator.com/
 *
 * SPDX-FileCopyrightText: 2025-2026 Alex Ruban <alex@maximum.guru>
 * SPDX-FileCopyrightText: 2025-2026 Maximum Software Inc <contact@maximum.software>
 * SPDX-License-Identifier: GPL-3.0-only
 */
jQuery(document).ready(function() {
    'use strict';

    // Cancel operation button click handler (event delegation for multiple progress bars)
    jQuery(document).on('click', '.agenistrator-cancel-operation', function() {
        var $button = jQuery(this);
        var instanceId = $button.data('instance-id');
        var $progressContainer = $button.closest('.agenistrator-progress-bar-container');

        if (!instanceId) {
            return;
        }

        $button.prop('disabled', true);

        jQuery.ajax({
            url: ajaxurl,
            method: 'POST',
            data: {
                action: 'agenistrator_cancel_operation',
                instance_id: instanceId,
                nonce: agenistrator.nonce
            },
            success: function(response) {
                if (response.success) {
                    $progressContainer.slideUp(function() {
                        jQuery(this).remove();
                    });
                }
                $button.prop('disabled', false);
            },
            error: function() {
                $button.prop('disabled', false);
            }
        });
    });
});
