<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/** @var \Magento\Customer\Block\Adminhtml\System\Config\Validatevat $block */
?>
<script>
require(['prototype'], function(){

//<![CDATA[
    function validateVat() {
        var validationMessage = $('validation_result');

        params = {
            country: $('<?= $block->escapeJs($block->getMerchantCountryField()) ?>').value,
            vat: $('<?= $block->escapeJs($block->getMerchantVatNumberField()) ?>').value
        };

        new Ajax.Request('<?= $block->escapeJs($block->escapeUrl($block->getAjaxUrl())) ?>', {
            parameters: params,
            onSuccess: function(response) {
                var result = '<?= $block->escapeJs($block->escapeHtml(__('Error during VAT Number verification.'))) ?>';
                try {
                    if (response.responseText.isJSON()) {
                        response = response.responseText.evalJSON();
                        result = response.message;
                    }
                    if (response.valid == 1) {
                        validationMessage.removeClassName('hidden').addClassName('success')
                    } else {
                        validationMessage.removeClassName('hidden').addClassName('error')
                    }
                } catch (e) {
                    validationMessage.removeClassName('hidden').addClassName('error')
                }
                validationMessage.update(result);
            }
        });
    }

    window.validateVat = validateVat;
//]]>

});
</script>
<div class="actions actions-validate-vat">
    <div id="validation_result" class="message-validation hidden"></div>
    <button onclick="javascript:validateVat(); return false;" class="action-validate-vat" type="button" id="<?= $block->escapeHtmlAttr($block->getHtmlId()) ?>">
        <span><?= $block->escapeHtml($block->getButtonLabel()) ?></span>
    </button>
</div>
