<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<?php
/**
 * @var $block \Magento\Backend\Block\Widget\Grid\Serializer
 * @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
 */
?>
<?php
// phpcs:ignore
$_id = 'id_' . md5(microtime());
?>
<?php $formId = $block->getFormId()?>
<?php if (!empty($formId)): ?>
    <?php $scriptString = <<<script
    require([
        'prototype',
        'mage/adminhtml/grid'
    ], function(){
        Event.observe(window, "load", function(){
            var serializeInput  = document.createElement('input');
            serializeInput.type = 'hidden';
            serializeInput.name = '{$block->escapeJs($block->getInputElementName())}';
            serializeInput.id   = '{$_id}';
            try {
                document.getElementById('{$block->escapeJs($formId)}').appendChild(serializeInput);
                new serializerController('{$_id}', {$block->getDataAsJSON()}, {$block->getColumnInputNames(true)},
                 {$block->escapeJs($block->getGridBlock()->getJsObjectName())},
                 '{$block->escapeJs($block->getReloadParamName())}');
            } catch(e) {
                //Error add serializer
            }
        });
    });
script;
    ?>
    <?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
<?php else:?>
<input type="hidden" name="<?= $block->escapeHtmlAttr($block->getInputElementName()) ?>"  value=""
       id="<?= /* @noEscape */ $_id ?>" />
    <?php $scriptString = <<<script
    require([
        'mage/adminhtml/grid'
    ], function(){
        new serializerController('{$_id}', {$block->getDataAsJSON()}, {$block->getColumnInputNames(true)},
         {$block->escapeJs($block->getGridBlock()->getJsObjectName())},
         '{$block->escapeJs($block->getReloadParamName())}');
    });
script;
    ?>
    <?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
<?php endif;?>
