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

use Magento\Catalog\Ui\DataProvider\Product\ProductCollection;

?>
<?php
/**
 * Template for \Magento\Backend\Block\Widget\Grid
 *
 *  getId()
 *  getCollection()
 *  getColumns()
 *  getPagerVisibility()
 *  getVarNamePage()
 */

/**
 * @var \Magento\Backend\Block\Widget\Grid\Extended $block
 * @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
 * @var \Magento\Backend\ViewModel\LimitTotalNumberOfProductsInGrid $viewModel
 */
$numColumns = count($block->getColumns());
$viewModel = $block->getViewModel();

?>
<?php if ($block->getCollection()): ?>
    <?php if ($block->canDisplayContainer()): ?>

    <div id="<?= $block->escapeHtml($block->getId()) ?>" data-grid-id="<?= $block->escapeHtml($block->getId()) ?>">
    <?php else: ?>
        <?= $block->getLayout()->getMessagesBlock()->getGroupedHtml() ?>
    <?php endif; ?>
    <?php $massActionAvailable = $block->getMassactionBlock() && $block->getMassactionBlock()->isAvailable() ?>
    <?php if ($block->getPagerVisibility() || $block->getExportTypes() || $block->getFilterVisibility() ||
        $massActionAvailable): ?>
        <div class="admin__data-grid-header admin__data-grid-toolbar">
            <div class="admin__data-grid-header-row">
                <?php if ($massActionAvailable): ?>
                    <?= $block->getMainButtonsHtml() ?
                        '<div class="admin__filter-actions">' . $block->getMainButtonsHtml() . '</div>' : '' ?>
                <?php endif; ?>
            <?php if ($block->getExportTypes()): ?>
                <div class="admin__data-grid-export">
                    <label
                        class="admin__control-support-text"
                        for="<?= $block->escapeHtml($block->getId()) ?>_export">
                        <?= $block->escapeHtml(__('Export to:')) ?>
                    </label>
                    <select name="<?= $block->escapeHtml($block->getId()) ?>_export"
                            id="<?= $block->escapeHtml($block->getId()) ?>_export"
                            class="admin__control-select">
                        <?php foreach ($block->getExportTypes() as $_type): ?>
                            <option value="<?= $block->escapeHtmlAttr($_type->getUrl()) ?>">
                                <?= $block->escapeHtml($_type->getLabel()) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <?= $block->getExportButtonHtml() ?>
                </div>
            <?php endif; ?>
            </div>

            <div class="admin__data-grid-header-row <?= $massActionAvailable ? '_massaction' : '' ?>">
                <?php if ($massActionAvailable): ?>
                    <?= $block->getMassactionBlockHtml() ?>
                <?php else: ?>
                    <?= $block->getMainButtonsHtml() ?
                        '<div class="admin__filter-actions">' . $block->getMainButtonsHtml() . '</div>' : '' ?>
                <?php endif; ?>
                <?php $countRecords = $block->getCollection()->getSize(); ?>
                <div class="admin__control-support-text">
                    <?php if (!$block->getCollection() instanceof ProductCollection
                        || !$viewModel->limitTotalNumberOfProducts()
                        || $countRecords < $viewModel->getRecordsLimit()): ?>
                        <span id="<?= $block->escapeHtml($block->getHtmlId()) ?>-total-count"
                            <?= /* @noEscape */ $block->getUiId('total-count') ?>>
                            <?= /* @noEscape */ $countRecords ?>
                        </span>
                        <?= $block->escapeHtml(__('records found')) ?>
                    <?php endif; ?>
                    <span id="<?= $block->escapeHtml($block->getHtmlId()) ?>_massaction-count"
                          class="mass-select-info _empty">
                        <strong data-role="counter">0</strong>
                        <span><?= $block->escapeHtml(__('selected')) ?></span>
                    </span>
                </div>

            <?php if ($block->getPagerVisibility()): ?>
                <div class="admin__data-grid-pager-wrap">
                    <select name="<?= $block->escapeHtmlAttr($block->getVarNameLimit()) ?>"
                            id="<?= $block->escapeHtml($block->getHtmlId()) ?>_page-limit"
                            onchange="<?= /* @noEscape */ $block->getJsObjectName() ?>.loadByElement(this)"
                            class="admin__control-select">
                        <option value="20"<?php if ($block->getCollection()->getPageSize() == 20): ?>
                            selected="selected"<?php endif; ?>>20
                        </option>
                        <option value="30"<?php if ($block->getCollection()->getPageSize() == 30): ?>
                            selected="selected"<?php endif; ?>>30
                        </option>
                        <option value="50"<?php if ($block->getCollection()->getPageSize() == 50): ?>
                            selected="selected"<?php endif; ?>>50
                        </option>
                        <option value="100"<?php if ($block->getCollection()->getPageSize() == 100): ?>
                            selected="selected"<?php endif; ?>>100
                        </option>
                        <option value="200"<?php if ($block->getCollection()->getPageSize() == 200): ?>
                            selected="selected"<?php endif; ?>>200
                        </option>
                    </select>
                    <label for="<?= $block->escapeHtml($block->getHtmlId()) ?>_page-limit"
                        class="admin__control-support-text"><?= $block->escapeHtml(__('per page')) ?></label>

                    <div class="admin__data-grid-pager">
                        <?php $_curPage = $block->getCollection()->getCurPage() ?>
                        <?php $_lastPage = $block->getCollection()->getLastPageNumber() ?>
                        <?php if ($_curPage > 1): ?>
                            <button class="action-previous" type="button">
                                <span><?= $block->escapeHtml(__('Previous page')) ?></span>
                            </button>
                            <?= /* @noEscape */ $secureRenderer->renderEventListenerAsTag(
                                'onclick',
                                /* @noEscape */ $block->getJsObjectName() . '.setPage(\'' .
                                /* @noEscape */ ($_curPage - 1) . '\');event.preventDefault();',
                                'div#' . $block->escapeJs($block->getId()) .
                                ' .admin__data-grid-pager button.action-previous:not(.disabled)'
                            ) ?>
                        <?php else: ?>
                            <button type="button" class="action-previous disabled">
                                <span><?= $block->escapeHtml(__('Previous page')) ?></span>
                            </button>
                        <?php endif; ?>
                        <?php if (!$block->getCollection() instanceof ProductCollection
                            || !$viewModel->limitTotalNumberOfProducts()
                            || $countRecords < $viewModel->getRecordsLimit()): ?>
                            <input type="text"
                                   id="<?= $block->escapeHtml($block->getHtmlId()) ?>_page-current"
                                   name="<?= $block->escapeHtmlAttr($block->getVarNamePage()) ?>"
                                   value="<?= $block->escapeHtmlAttr($_curPage) ?>"
                                   class="admin__control-text"
                                   <?= /* @noEscape */ $block->getUiId('current-page') ?> />
                            <?= /* @noEscape */ $secureRenderer->renderEventListenerAsTag(
                                'onkeypress',
                                /* @noEscape */ $block->getJsObjectName() . '.inputPage(event, \'' .
                                /* @noEscape */ $_lastPage . '\')',
                                '#' . $block->escapeHtml($block->getHtmlId()) . '_page-current'
                            ) ?>
                            <label class="admin__control-support-text"
                                   for="<?= $block->escapeHtml($block->getHtmlId()) ?>_page-current">
                                <?= /* @noEscape */ __('of %1', '<span>' .
                                    $block->getCollection()->getLastPageNumber() . '</span>') ?>
                            </label>
                        <?php endif; ?>
                        <?php if ($_curPage < $_lastPage): ?>
                            <button type="button"
                                    title="<?= $block->escapeHtmlAttr(__('Next page')) ?>"
                                    class="action-next">
                                <span><?= $block->escapeHtml(__('Next page')) ?></span>
                            </button>
                            <?= /* @noEscape */ $secureRenderer->renderEventListenerAsTag(
                                'onclick',
                                /* @noEscape */ $block->getJsObjectName() . '.setPage(\'' .
                                /* @noEscape */ ($_curPage + 1) . '\');event.preventDefault();',
                                'div#' . $block->escapeJs($block->getId()) .
                                ' .admin__data-grid-pager button.action-next:not(.disabled)'
                            ) ?>
                        <?php else: ?>
                            <button type="button" class="action-next disabled">
                                <span><?= $block->escapeHtml(__('Next page')) ?></span>
                            </button>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endif ?>
            </div>
        </div>
    <?php endif; ?>

    <div class="admin__data-grid-wrap admin__data-grid-wrap-static">
        <table class="data-grid" id="<?= $block->escapeHtml($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): ?>
                            <?php if ($_column->getHeaderHtml() == '&nbsp;'): ?>
                                <th class="data-grid-th" data-column="<?= $block->escapeHtmlAttr($_column->getId()) ?>"
                                    <?= $_column->getHeaderHtmlProperty() ?>>&nbsp;</th>
                            <?php else: ?>
                                <?= $_column->getHeaderHtml() ?>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </tr>
                <?php endif; ?>
                <?php if ($block->getFilterVisibility()): ?>
                    <tr class="data-grid-filters" data-role="filter-form">
                        <?php $i = 0;
                        foreach ($block->getColumns() as $_column): ?>
                            <td data-column="<?= $block->escapeHtmlAttr($_column->getId()) ?>"
                                <?= $_column->getHeaderHtmlProperty() ?>>
                                <?= $_column->getFilterHtml() ?>
                            </td>
                        <?php endforeach; ?>
                    </tr>
                <?php endif ?>
                </thead>
            <?php endif; ?>
            <?php if ($block->getCountTotals()): ?>
                <tfoot>
                <tr class="totals">
                    <?php foreach ($block->getColumns() as $_column): ?>
                        <th class="<?= $block->escapeHtmlAttr($_column->getCssProperty()) ?>">
                            <?= /* @noEscape */ ($_column->hasTotalsLabel()) ?
                                $block->escapeHtml($_column->getTotalsLabel()) :
                                $_column->getRowField($_column->getGrid()->getTotals()) ?>
                        </th>
                    <?php endforeach; ?>
                </tr>
                </tfoot>
            <?php endif; ?>

            <tbody>
            <?php if (($block->getCollection()->getSize() > 0) && (!$block->getIsCollapsed())): ?>
                <?php foreach ($block->getCollection() as $_index => $_item): ?>
                    <tr title="<?= $block->escapeHtmlAttr($block->getRowUrl($_item)) ?>"
                        <?php if ($_class = $block->getRowClass($_item)): ?>
                        class="<?= $block->escapeHtmlAttr($_class) ?>"
                        <?php endif; ?>>
                        <?php
                        $i = 0;
                        foreach ($block->getColumns() as $_column):
                            if ($block->shouldRenderCell($_item, $_column)):
                                $_rowspan = $block->getRowspan($_item, $_column);
                                ?>
                            <td <?= /* @noEscape */ ($_rowspan ? 'rowspan="' . $_rowspan . '" ' : '') ?>
                                class="<?= $block->escapeHtmlAttr($_column->getCssProperty()) ?>
                                        <?= /* @noEscape */ $_column->getId() == 'massaction' ?
                                        'data-grid-checkbox-cell': '' ?>">
                                <?= /* @noEscape */ (($_html = $_column->getRowField($_item)) != '' ?
                                $_html : '&nbsp;') ?>
                                </td><?php
                                if ($block->shouldRenderEmptyCell($_item, $_column)):
                                    ?>
                                    <td colspan="<?= $block->escapeHtmlAttr($block->getEmptyCellColspan($_item)) ?>"
                                        class="last"><?= $block->escapeHtml($block->getEmptyCellLabel()) ?></td><?php
                                endif;
                            endif;
                        endforeach; ?>
                    </tr>
                    <?php if ($_multipleRows = $block->getMultipleRows($_item)): ?>
                        <?php foreach ($_multipleRows as $_i): ?>
                            <tr>
                                <?php $i = 0;
                                foreach ($block->getMultipleRowColumns($_i) as $_column): ?>
                                    <td class="<?= $block->escapeHtmlAttr($_column->getCssProperty()) ?>
                                        <?= /* @noEscape */ $_column->getId() == 'massaction' ?
                                        'data-grid-checkbox-cell': '' ?>">
                                        <?= /* @noEscape */ (($_html = $_column->getRowField($_i)) != '' ?
                                            $_html : '&nbsp;') ?>
                                    </td>
                                <?php endforeach; ?>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>

                    <?php if ($block->shouldRenderSubTotal($_item)): ?>
                        <tr class="subtotals">
                            <?php $i = 0;
                            foreach ($block->getSubTotalColumns() as $_column): ?>
                                <td class="<?= $block->escapeHtmlAttr($_column->getCssProperty()) ?>
                                <?= /* @noEscape */ $_column->getId() == 'massaction' ?
                                    'data-grid-checkbox-cell': '' ?>">
                                    <?= /* @noEscape */ $_column->hasSubtotalsLabel() ?
                                        $block->escapeHtml($_column->getSubtotalsLabel()) :
                                        $_column->getRowField($block->getSubTotalItem($_item)) ?>
                                </td>
                            <?php endforeach; ?>
                        </tr>
                    <?php endif; ?>
                <?php endforeach; ?>
            <?php elseif ($block->getEmptyText()): ?>
                <tr class="data-grid-tr-no-data">
                    <td class="<?= $block->escapeHtmlAttr($block->getEmptyTextClass()) ?>"
                        colspan="<?= $block->escapeHtmlAttr($numColumns) ?>"><?=
                        $block->escapeHtml($block->getEmptyText()) ?></td>
                </tr>
            <?php endif; ?>
            </tbody>
        </table>

    </div>
        <?php if ($block->canDisplayContainer()): ?>
</div>
        <?php endif; ?>
    <?php
    /** @var \Magento\Framework\Json\Helper\Data $jsonHelper */
    $jsonHelper = $block->getData('jsonHelper');
    if ($block->canDisplayContainer()):
        $scriptString = <<<script

    var deps = [];
script;
        if ($block->getDependencyJsObject()):
            $scriptString .= <<<script

    deps.push('uiRegistry');
script;
        endif;

        if ($block->getRowClickCallback() && strpos($block->getRowClickCallback(), 'order.') !== false):
            $scriptString .= <<<script

    deps.push('Magento_Sales/order/create/form')
script;
        endif;
        $scriptString .= <<<script

    deps.push('mage/adminhtml/grid');
script;
        if (is_array($block->getRequireJsDependencies())):
            foreach ($block->getRequireJsDependencies() as $dependency):
                $scriptString .= <<<script

            deps.push('{$block->escapeJs($dependency)}');
script;
            endforeach;
        endif;
        $dependencyJsObject = ($block->getDependencyJsObject() ? 'registry' : '');
        $scriptString .= <<<script

    require(deps, function({$dependencyJsObject}){

script;
        //TODO: getJsObjectName and getRowClickCallback has unexpected behavior. Should be removed
        $scriptString .= <<<script

        //<![CDATA[

script;
        if ($block->getDependencyJsObject()):
            $scriptString .= <<<script

        registry.get('{$block->escapeJs($block->getDependencyJsObject())}',
         function ({$block->escapeJs($block->getDependencyJsObject())}) {
script;
        endif;
        $encodedId = /* @noEscape */ $jsonHelper->jsonEncode($block->getId());
        $scriptString .= <<<script

    {$block->escapeJs($block->getJsObjectName())} = new varienGrid({$encodedId},
         '{$block->escapeJs($block->getGridUrl())}',
         '{$block->escapeJs($block->getVarNamePage())}',
         '{$block->escapeJs($block->getVarNameSort())}',
         '{$block->escapeJs($block->getVarNameDir())}',
         '{$block->escapeJs($block->getVarNameFilter())}'
         );

        {$block->escapeJs($block->getJsObjectName())}.useAjax = '{$block->escapeJs($block->getUseAjax())}';

script;
        if ($block->getRowClickCallback()):
            $rowClickCallback = /* @noEscape */ $block->getRowClickCallback();
            $scriptString .= <<<script

            {$block->escapeJs($block->getJsObjectName())}.rowClickCallback = {$rowClickCallback};
script;
        endif;
        if ($block->getCheckboxCheckCallback()):
            $checkboxCheckCallback = /* @noEscape */ $block->getCheckboxCheckCallback();
            $scriptString .= <<<script

            {$block->escapeJs($block->getJsObjectName())}.checkboxCheckCallback = {$checkboxCheckCallback};
script;
        endif;
        if ($block->getFilterKeyPressCallback()):
            $filterKeyPressCallback = /* @noEscape */ $block->getFilterKeyPressCallback();
            $scriptString .= <<<script

            {$block->escapeJs($block->getJsObjectName())}.filterKeyPressCallback = {$filterKeyPressCallback};
script;
        endif;
        if ($block->getRowInitCallback()):
            $rowInitCallback = /* @noEscape */ $block->getRowInitCallback();
            $scriptString .= <<<script

            {$block->escapeJs($block->getJsObjectName())}.initRowCallback = {$rowInitCallback};
            {$block->escapeJs($block->getJsObjectName())}.initGridRows();

script;
        endif;
        if ($block->getMassactionBlock() && $block->getMassactionBlock()->isAvailable()):
            $scriptString .= /* @noEscape */ $block->getMassactionBlock()->getJavaScript() . PHP_EOL;
        endif;
        $scriptString .= /* @noEscape */ $block->getAdditionalJavaScript() . PHP_EOL;

        if ($block->getDependencyJsObject()):
            $scriptString .= <<<script

        });

script;
        endif;
        $scriptString .= <<<script

    //]]>

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