<?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').removeClassName('admin__field-error').addClassName('note');
                        validationMessage.setStyle({color:'green'});
                    } else {
                        validationMessage.removeClassName('hidden').addClassName('admin__field-error');
                    }
                } catch (e) {
                    validationMessage.removeClassName('hidden').addClassName('admin__field-error');
                }
                validationMessage.update(result);
            }
        });
    }

    window.validateVat = validateVat;
//]]>

});
</script>
<div class="actions actions-validate-vat">
    <p class="admin__field-error hidden" id="validation_result" style="margin-bottom:10px;"></p>
    <button onclick="javascript:validateVat(); return false;" class="action-validate-vat" type="button" id="<?= /* @noEscape */ $block->getHtmlId() ?>">
        <span><?= $block->escapeHtml($block->getButtonLabel()) ?></span>
    </button>
</div>

