<?php
$numColumns = sizeof($block->getColumns());
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

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

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

            <div class="magetop__data-grid-header-row <?php echo $massActionAvailable ? '_massaction' : '';?>">
                <?php if ($massActionAvailable): ?>
                    <?php echo $block->getMassactionBlockHtml() ?>
                <?php else: ?>
                    <?php echo $block->getMainButtonsHtml() ? '<div class="magetop__filter-actions">' . $block->getMainButtonsHtml() . '</div>' : ''; ?>
                <?php endif; ?>
                <?php $countRecords = $block->getCollection()->getSize(); ?>
                <div class="magetop__control-support-text">
                        <span id="<?php echo $block->escapeHtml($block->getHtmlId()) ?>-total-count" <?php /* @escapeNotVerified */ echo $block->getUiId('total-count') ?>>
                            <?php /* @escapeNotVerified */ echo $countRecords ?>
                        </span>
                    <?php /* @escapeNotVerified */ echo __('records found') ?>
                    <span id="<?php echo $block->escapeHtml($block->getHtmlId()) ?>_massaction-count"
                          class="mass-select-info _empty"><strong data-role="counter">0</strong> <span><?php /* @escapeNotVerified */ echo __('selected') ?></span></span>
                </div>

            <?php if ($block->getPagerVisibility()): ?>
                <div class="magetop__data-grid-pager-wrap">
                    <select name="<?php /* @escapeNotVerified */ echo $block->getVarNameLimit() ?>"
                            id="<?php echo $block->escapeHTML($block->getHtmlId())?>_page-limit"
                            onchange="<?php /* @escapeNotVerified */ echo $block->getJsObjectName() ?>.loadByElement(this)"
                            class="magetop__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="<?php echo $block->escapeHTML($block->getHtmlId())?><?php echo $block->escapeHTML($block->getHtmlId())?>_page-limit"
                        class="magetop__control-support-text"><?php /* @escapeNotVerified */ echo __('per page') ?></label>

                    <div class="magetop__data-grid-pager">
                        <?php $_curPage = $block->getCollection()->getCurPage() ?>
                        <?php $_lastPage = $block->getCollection()->getLastPageNumber() ?>
                        <?php if ($_curPage > 1): ?>
                            <button class="action-previous"
                                    type="button"
                                    onclick="<?php /* @escapeNotVerified */ echo $block->getJsObjectName() ?>.setPage('<?php /* @escapeNotVerified */ echo($_curPage - 1) ?>');return false;">
                                <span><?php /* @escapeNotVerified */ echo __('Previous page') ?></span>
                            </button>
                        <?php else: ?>
                            <button type="button" class="action-previous disabled"><span><?php /* @escapeNotVerified */ echo __('Previous page') ?></span></button>
                        <?php endif; ?>
                        <input type="text"
                               id="<?php echo $block->escapeHTML($block->getHtmlId())?>_page-current"
                               name="<?php /* @escapeNotVerified */ echo $block->getVarNamePage() ?>"
                               value="<?php /* @escapeNotVerified */ echo $_curPage ?>"
                               class="magetop__control-text"
                               onkeypress="<?php /* @escapeNotVerified */ echo $block->getJsObjectName() ?>.inputPage(event, '<?php /* @escapeNotVerified */ echo $_lastPage ?>')" <?php /* @escapeNotVerified */ echo $block->getUiId('current-page') ?> />
                        <label class="magetop__control-support-text" for="<?php echo $block->escapeHTML($block->getHtmlId())?>_page-current">
                            <?php /* @escapeNotVerified */ echo __('of %1', '<span>' . $block->getCollection()->getLastPageNumber() . '</span>') ?>
                        </label>
                        <?php if ($_curPage < $_lastPage): ?>
                            <button type="button"
                                    title="<?php /* @escapeNotVerified */ echo __('Next page') ?>"
                                    class="action-next"
                                    onclick="<?php /* @escapeNotVerified */ echo $block->getJsObjectName() ?>.setPage('<?php /* @escapeNotVerified */ echo($_curPage + 1) ?>');return false;">
                                <span><?php /* @escapeNotVerified */ echo __('Next page') ?></span>
                            </button>
                        <?php else: ?>
                        <button type="button" class="action-next disabled"><span><?php /* @escapeNotVerified */ echo __('Next page') ?></span></button>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endif ?>
            </div>
        </div>
    <?php endif; ?>

    <div class="magetop__data-grid-wrap magetop__data-grid-wrap-static">
        <table class="data-grid" id="<?php echo $block->escapeHtml($block->getId()) ?>_table">
            <?php
            /* This part is commented to remove all <col> tags from the code. */
            /* foreach ($block->getColumns() as $_column): ?>
            <col <?php echo $_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="<?php /* @escapeNotVerified */ echo $_column->getId() ?>"
                                    <?php echo $_column->getHeaderHtmlProperty() ?>>&nbsp;</th>
                            <?php else: ?>
                                <?php echo $_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="<?php /* @escapeNotVerified */ echo $_column->getId() ?>" <?php echo $_column->getHeaderHtmlProperty() ?>>
                                <?php echo $_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="<?php /* @escapeNotVerified */ echo $_column->getCssProperty() ?>">
                            <?php /* @escapeNotVerified */ echo($_column->hasTotalsLabel()) ? $_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="<?php /* @escapeNotVerified */ echo $block->getRowUrl($_item) ?>"<?php if ($_class = $block->getRowClass($_item)): ?>
                        class="<?php /* @escapeNotVerified */ echo $_class; ?>"<?php endif; ?> ><?php
                        $i = 0;
                        foreach ($block->getColumns() as $_column):
                            if ($block->shouldRenderCell($_item, $_column)):
                                $_rowspan = $block->getRowspan($_item, $_column);
                                ?>
                            <td <?php echo($_rowspan ? 'rowspan="' . $_rowspan . '" ' : '') ?>
                                class="<?php /* @escapeNotVerified */ echo $_column->getCssProperty() ?>
                                        <?php /* @escapeNotVerified */ echo $_column->getId() == 'massaction' ? 'data-grid-checkbox-cell': ''?>">
                                <?php echo(($_html = $_column->getRowField($_item)) != '' ? $_html : '&nbsp;') ?>
                                </td><?php
                                if ($block->shouldRenderEmptyCell($_item, $_column)):
                                    ?>
                                    <td colspan="<?php /* @escapeNotVerified */ echo $block->getEmptyCellColspan($_item) ?>"
                                        class="last"><?php /* @escapeNotVerified */ echo $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="<?php /* @escapeNotVerified */ echo $_column->getCssProperty() ?>
                                        <?php /* @escapeNotVerified */ echo $_column->getId() == 'massaction' ? 'data-grid-checkbox-cell': ''?>">
                                        <?php echo(($_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="<?php /* @escapeNotVerified */ echo $_column->getCssProperty() ?>
                                           <?php /* @escapeNotVerified */ echo $_column->getId() == 'massaction' ? 'data-grid-checkbox-cell': ''?>">
                                    <?php /* @escapeNotVerified */ echo($_column->hasSubtotalsLabel() ? $_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="<?php /* @escapeNotVerified */ echo $block->getEmptyTextClass() ?>"
                        colspan="<?php /* @escapeNotVerified */ echo $numColumns ?>"><?php /* @escapeNotVerified */ echo $block->getEmptyText() ?></td>
                </tr>
            <?php endif; ?>
            </tbody>
        </table>

    </div>
    <?php if ($block->canDisplayContainer()): ?>
</div>
<script>
	var FORM_KEY = '<?php echo $objectManager->create("Magento\Framework\Data\Form\FormKey")->getFormKey() ?>';
    var deps = [];

    <?php if ($block->getDependencyJsObject()): ?>
    deps.push('uiRegistry');
    <?php endif; ?>

    <?php if (@strpos($block->getRowClickCallback(), 'order.') !== false): ?>
    deps.push('Magento_Sales/order/create/form')
    <?php endif; ?>

    deps.push('mage/adminhtml/grid');

    <?php if (is_array($block->getRequireJsDependencies())): ?>
        <?php foreach ($block->getRequireJsDependencies() as $dependency): ?>
            deps.push('<?php /* @escapeNotVerified */ echo $dependency; ?>');
        <?php endforeach; ?>
    <?php endif; ?>

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

        //<![CDATA[
    <?php if ($block->getDependencyJsObject()): ?>
        registry.get('<?php /* @escapeNotVerified */ echo $block->getDependencyJsObject() ?>', function (<?php /* @escapeNotVerified */ echo $block->getDependencyJsObject() ?>) {
    <?php endif; ?>

    <?php /* @escapeNotVerified */ echo $block->getJsObjectName() ?> = new varienGrid(<?php /* @noEscape */ echo $this->helper('Magento\Framework\Json\Helper\Data')->jsonEncode($block->getId()) ?>, '<?php /* @escapeNotVerified */ echo $block->getGridUrl() ?>', '<?php /* @escapeNotVerified */ echo $block->getVarNamePage() ?>', '<?php /* @escapeNotVerified */ echo $block->getVarNameSort() ?>', '<?php /* @escapeNotVerified */ echo $block->getVarNameDir() ?>', '<?php /* @escapeNotVerified */ echo $block->getVarNameFilter() ?>');
    <?php /* @escapeNotVerified */ echo $block->getJsObjectName() ?>.useAjax = '<?php /* @escapeNotVerified */ echo $block->getUseAjax() ?>';
    <?php if ($block->getRowClickCallback()): ?>
    <?php /* @escapeNotVerified */ echo $block->getJsObjectName() ?>.rowClickCallback = <?php /* @escapeNotVerified */ echo $block->getRowClickCallback() ?>;
    <?php endif; ?>
    <?php if ($block->getCheckboxCheckCallback()): ?>
    <?php /* @escapeNotVerified */ echo $block->getJsObjectName() ?>.checkboxCheckCallback = <?php /* @escapeNotVerified */ echo $block->getCheckboxCheckCallback() ?>;
    <?php endif; ?>
    <?php if ($block->getRowInitCallback()): ?>
    <?php /* @escapeNotVerified */ echo $block->getJsObjectName() ?>.initRowCallback = <?php /* @escapeNotVerified */ echo $block->getRowInitCallback() ?>;
    <?php /* @escapeNotVerified */ echo $block->getJsObjectName() ?>.initGridRows();
    <?php endif; ?>
    <?php if ($block->getMassactionBlock() && $block->getMassactionBlock()->isAvailable()): ?>
    <?php /* @escapeNotVerified */ echo $block->getMassactionBlock()->getJavaScript() ?>
    <?php endif ?>
    <?php /* @escapeNotVerified */ echo $block->getAdditionalJavaScript(); ?>

    <?php if ($block->getDependencyJsObject()): ?>
        });
    <?php endif; ?>
    //]]>

});
</script>
<?php endif; ?>
<?php endif ?>
