<?php
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
?>

<?php
$_htmlId = $block->getHtmlId() ? $block->getHtmlId() : '_' . uniqid();
?>

<div class="design_theme_ua_regexp" id="grid<?= $block->escapeHtmlAttr($_htmlId) ?>">
    <div class="admin__control-table-wrapper">
        <table class="admin__control-table" id="<?= $block->escapeHtmlAttr($block->getElement()->getId()) ?>">
            <thead>
            <tr>
                <?php foreach ($block->getColumns() as $columnName => $column): ?>
                    <th><?= $block->escapeHtml($column['label']) ?></th>
                <?php endforeach; ?>
            </tr>
            </thead>

            <tbody id="addRow<?= $block->escapeHtmlAttr($_htmlId) ?>"></tbody>
        </table>
    </div>
    <input type="hidden" name="<?= $block->escapeHtmlAttr($block->getElement()->getName()) ?>[__empty]" value="" />

    <?php $scriptString = <<<script
        require([
            'mage/template',
            'prototype'
        ], function (mageTemplate) {
        // create row creator
        window.arrayRow{$block->escapeJs($_htmlId)} = {

            // define row prototypeJS template
            template: mageTemplate(
                    '<tr id="<%- _id %>">'
script;
    foreach ($block->getColumns() as $columnName => $column):
        $scriptString .= <<<script

                        + '<td>'
                        + '{$block->escapeJs($block->renderCellTemplate($columnName))}'
                        + '<\/td>'
script;
    endforeach;

    $scriptString .= <<<script
            ),

            add: function(rowData, insertAfterId) {
                // generate default template data
                var templateValues;

                // Prepare template values
                if (rowData) {
                    templateValues = rowData;
                } else {
                    var d = new Date();
                    templateValues = {
script;
    foreach ($block->getColumns() as $columnName => $column):
        $scriptString .= <<<script

                            {$block->escapeJs($columnName)}: '',
                                'option_extra_attrs': {},
script;
    endforeach;
    $scriptString .= <<<script

                        _id: '_' + d.getTime() + '_' + d.getMilliseconds()
                };
            }

            // Insert new row after specified row or at the bottom
            if (insertAfterId) {
                Element.insert($(insertAfterId), {after: this.template(templateValues)});
            } else {
                Element.insert($('addRow{$block->escapeJs($_htmlId)}'), {bottom: this.template(templateValues)});
            }

            // Fill controls with data
            if (rowData) {
                var rowInputElementNames = Object.keys(rowData.column_values);
                for (var i = 0; i < rowInputElementNames.length; i++) {
                    if ($(rowInputElementNames[i])) {
                        $(rowInputElementNames[i]).setValue(rowData.column_values[rowInputElementNames[i]]);
                    }
                }
            }

            // Add event for {addAfterBtn} button

script;
    $scriptString .= <<<script

            },

          
        }

script;

    foreach ($block->getArrayRows() as $_rowId => $_row) {
        $scriptString .= /** @noEscape */ ' arrayRow' .$block->escapeJs($_htmlId) .
            '.add(' . /** @noEscape */ $_row->toJson() . ");\n";
    }
    $scriptString .= <<<script

        // Toggle the grid availability, if element is disabled (depending on scope)
script;
    if ($block->getElement()->getDisabled()):
        $scriptString .= <<<script

        toggleValueElements({checked: true}, $('grid{$block->escapeJs($_htmlId)}').parentNode);
script;
    endif;
    $scriptString .= <<<script

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