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

/** @var \Magento\Backend\Block\Template $block */
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
?>

<?php $scriptString = <<<script
require([
    'Magento_Ui/js/modal/alert',
    'prototype'
], function(alert){

//<![CDATA[
    // Temporary Class will be replaced after refactoring of import/export functionality
    VarienExport = Class.create();
    VarienExport.prototype = {
        initialize: function() {
            /**
             * Previous selected entity for grid
             * @type {string}
             */
            this.previousGridEntity = '';
        },

        /**
         *  Handle value change in entity type selector
         */
        modifyFilterGrid: function() {
            if ($('entity') && \$F('entity') && \$F('entity') != 'catalog_product') {
                    \$$('col:first-child').each(function(el) {
                        el.show();
                    });
                    \$$('th.no-link:first-child').each(function(el) {
                        el.show();
                    });
                    \$$('td.a-center').each(function(el) {
                        el.show();
                    });
            }
        },

        /**
         * Post form data and process response via AJAX
         */
        getFilter: function() {
            if ($('entity') && \$F('entity')) {
                var url    = "{$block->escapeJs($block->getUrl('*/*/getFilter'))}";
                var entity = \$F('entity');
                if (entity != this.previousGridEntity) {
                    this.previousGridEntity = entity;
                    url += ((url.slice(-1) != '/') ? '/' : '') + 'entity/' + entity;
                    new Ajax.Request(url, {
                        method:      'post',
                        evalScripts: true,
                        onComplete:  function(transport) {
                            var responseText = transport.responseText.replace(/>\s+</g, '><');
                            $('export_filter_grid_container').update(responseText);
                            this.modifyFilterGrid();
                            $('export_filter_container').show();
                            $('messages').update();
                        }.bind(this)
                    });
                } else {
                    this.modifyFilterGrid();
                }
            } else {
                this.previousGridEntity = '';
                $('export_filter_container').hide();
            }
        }
    };

    /**
     * Continue export process after filter settings
     *
     * return void
     */
    getFile = function() {
        if ($('entity') && \$F('entity')) {
            var form      = $('export_filter_form');
            var oldAction = form.action;
            var url = oldAction + ((oldAction.slice(-1) != '/') ? '/' : '') + 'entity/' + \$F('entity')
                + '/file_format/' + \$F('file_format');
            if (\$F('fields_enclosure')) {
                url += '/fields_enclosure/' + \$F('fields_enclosure');
            }
            form.action = url;
            form.submit();
            form.action   = oldAction;
        } else {
            alert({
                content: '{$block->escapeHtml(__('Invalid data'))}'
            });
        }
    };

    varienExport = new VarienExport();
//]]>

});
script;
?>
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
