jQuery(document).ready(function($) {
    $('#saa-test-api').on('click', function() {
        var $status = $('#saa-api-status');
        $status.removeClass('success error').text('Testing...');

        $.post(saaAdmin.ajax_url, {
            action: 'saa_test_api',
            nonce: saaAdmin.nonce
        })
        .done(function(response) {
            if (response.success) {
                $status.addClass('success').removeClass('error').text(response.data);
            } else {
                $status.addClass('error').removeClass('success').text(response.data);
            }
        })
        .fail(function(xhr, textStatus, errorThrown) {
            $status.addClass('error').removeClass('success')
                .text('AJAX error: ' + textStatus + ' ' + errorThrown);
        });
    });
});