<?php
/**
 * Copyright 2014 Adobe
 * All Rights Reserved.
 */

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

$numColumns = count($block->getColumns());
?>
<?php if ($block->getCollection()): ?>
    <div class="dashboard-item-content">
        <?php if ($block->getCollection()->getSize() > 0): ?>
            <table class="admin__table-primary dashboard-data"
                   id="<?= $block->escapeHtmlAttr($block->getId()) ?>_table">
                <?php
                /* This part is commented to remove all <col> tags from the code. */
                /* foreach ($block->getColumns() as $_column): ?>
                <col <?= $_column->getHtmlProperty() ?> />
                <?php endforeach; */ ?>
                <?php if ($block->getHeadersVisibility() || $block->getFilterVisibility()): ?>
                    <thead>
                    <?php if ($block->getHeadersVisibility()): ?>
                        <tr>
                            <?php foreach ($block->getColumns() as $_column): ?>
                                <?= $_column->getHeaderHtml() ?>
                            <?php endforeach; ?>
                        </tr>
                    <?php endif; ?>
                    </thead>
                <?php endif; ?>
                <?php if (!$block->getIsCollapsed()): ?>
                    <tbody>
                    <?php foreach ($block->getCollection() as $_index => $_item): ?>
                        <tr title="<?= $block->escapeHtmlAttr($block->getRowUrl($_item)) ?>">
                            <?php $i = 0; foreach ($block->getColumns() as $_column): ?>
                                <td class="<?= $block->escapeHtmlAttr($_column->getCssProperty());
                                ?> <?= /* @noEscape */ ++$i == $numColumns ? 'last' : '';
?>"><?= /* @noEscape */ (($_html = $_column->getRowField($_item)) != '' ?
                                        $_html : '&nbsp;') ?></td>
                            <?php endforeach; ?>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                <?php endif; ?>
            </table>
        <?php else: ?>
            <div class="<?= $block->escapeHtmlAttr($block->getEmptyTextClass());
            ?>"><?= $block->escapeHtml($block->getEmptyText()) ?></div>
        <?php endif; ?>
    </div>
    <?php if ($block->canDisplayContainer()): ?>
        <?php $scriptString = 'var deps = [];' . PHP_EOL;
        if ($block->getDependencyJsObject()) {
            $scriptString .= 'deps.push(\'uiRegistry\');' . PHP_EOL;
        }

        if (strpos($block->getRowClickCallback(), 'order.') !== false) {
            $scriptString .= 'deps.push(\'Magento_Sales/order/create/form\');' . PHP_EOL;
        }

        $scriptString .= 'deps.push(\'mage/adminhtml/grid\');' . PHP_EOL;

        $scriptString .= 'require(deps, function('. ($block->getDependencyJsObject() ? 'registry' : '') .'){' .
            PHP_EOL .
        '//TODO: getJsObjectName and getRowClickCallback has unexpected behavior. Should be removed' . PHP_EOL;

        if ($block->getDependencyJsObject()) {
            $scriptString .= 'registry.get(\'' . $block->escapeJs($block->getDependencyJsObject()) .
                '\', function ('. $block->escapeJs($block->getDependencyJsObject()) . ') {' . PHP_EOL;
        }

        $scriptString .= $block->escapeJs($block->getJsObjectName()) . ' = new varienGrid(\'' .
            $block->escapeJs($block->getId()) . '\', \'' . $block->escapeJs($block->getGridUrl()) . '\', \'' .
            $block->escapeJs($block->getVarNamePage()) .'\', \'' .
            $block->escapeJs($block->getVarNameSort()) . '\', \'' .
            $block->escapeJs($block->getVarNameDir()) . '\', \'' .
            $block->escapeJs($block->getVarNameFilter()) .'\');' . PHP_EOL;

        $scriptString .= $block->escapeJs($block->getJsObjectName()) .'.useAjax = \'' .
            $block->escapeJs($block->getUseAjax()) . '\';' . PHP_EOL;
        if ($block->getRowClickCallback()) {
            $scriptString .= $block->escapeJs($block->getJsObjectName()) . '.rowClickCallback = ' .
                /* @noEscape */ $block->getRowClickCallback() . ';' . PHP_EOL;
        }

        if ($block->getCheckboxCheckCallback()) {
            $scriptString .= $block->escapeJs($block->getJsObjectName()) . '.checkboxCheckCallback = ' .
                /* @noEscape */ $block->getCheckboxCheckCallback() . ';' . PHP_EOL;
        }

        if ($block->getRowInitCallback()) {
            $scriptString .= $block->escapeJs($block->getJsObjectName()) . '.initRowCallback = ' .
                /* @noEscape */ $block->getRowInitCallback() . ';' . PHP_EOL;
            $scriptString .= $block->escapeJs($block->getJsObjectName()) . '.rows.each(function(row){' .
                /* @noEscape */ $block->getRowInitCallback() . '(' . $block->escapeJs($block->getJsObjectName()) .
                ', row)});' . PHP_EOL;
        }

        if ($block->getMassactionBlock()->isAvailable()) {
            $scriptString .= /* @noEscape */ $block->getMassactionBlock()->getJavaScript();
        }

        if ($block->getDependencyJsObject()) {
            $scriptString .=  '});' . PHP_EOL;
        }

        $scriptString .= '});' . PHP_EOL;

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