<?php /** @var Adyen\Payment\Model\Config\Adminhtml\WebhookTest $block */ ?>
<script>
    require([
            'jquery'
        ],
        function(jQuery)
        {
            let progressSpan = jQuery('#webhook_progress');
            jQuery('#adyen_webhook_test').click(function () {

                new Ajax.Request('<?= $block->escapeJs($block->getAjaxUrl()) ?>', {
                    parameters: {},
                    type: "POST",
                    loaderArea:     false,
                    asynchronous:   true,
                    onCreate: function() {
                        progressSpan.find('.test-configured').hide();
                        progressSpan.find('.test-processing').show();
                        jQuery('#adyen_webhook_test_message').text('');
                        jQuery("#adyen_webhook_test").prop('disabled', true);
                    },
                    onSuccess: function (response) {
                        //add html response
                        progressSpan.find('.test-processing').hide();
                        let resultText = '';
                        if (response.responseJSON === null) {
                            resultText = "No response";
                        } else {
                            resultText = response.responseJSON.statusCode;
                            if(resultText==='Success'){
                                progressSpan.find('.test-configured').show();
                            }
                        }
                        jQuery('#adyen_webhook_test_message').text(resultText);
                    },
                    onFailure: function(response)
                    {
                        progressSpan.find('.test-processing').hide();
                        jQuery('#adyen_webhook_test_message').text("Failed").show();
                    },
                    onComplete: function()
                    {
                        jQuery("#adyen_webhook_test").prop('disabled', false);
                    }
                });
            });

        });
</script>
<?php if ($block->isWebhookIdConfigured()) : ?>
    <?php echo $block->getButtonHtml() ?>
    <span class="adyen_webhook_test_config" id="webhook_progress">
        <img class="test-processing" hidden="hidden" alt="Configuring" style="margin:0 5px" src="<?= $block->escapeUrl($block->getViewFileUrl('images/process_spinner.gif')) ?>"/>
        <img class="test-configured" hidden="hidden" alt="Configured" style="margin:-3px 5px" src="<?= $block->escapeUrl($block->getViewFileUrl('images/rule_component_apply.gif')) ?>"/>
        <span id="adyen_webhook_test_message"></span>
    </span>
    <p class="note">
        <span><?= $block->escapeHtml(__('Sends sample notifications to test if the webhook is set up correctly.')); ?></span>
    </p>
<?php endif; ?>
