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

// @codingStandardsIgnoreFile

?>
<?php
/** @var $block \Magento\Reports\Block\Adminhtml\Grid */
$numColumns = sizeof($block->getColumns());
?>
<?php if ($block->getCollection()): ?>
    <?php if ($block->canDisplayContainer()): ?>
    <div id="<?= /* @escapeNotVerified */ $block->getId() ?>">
        <?php else: ?>
            <?= $block->getLayout()->getMessagesBlock()->getGroupedHtml() ?>
        <?php endif; ?>
        <?php if ($block->getStoreSwitcherVisibility() || $block->getDateFilterVisibility()): ?>
            <div class="admin__data-grid-header admin__data-grid-toolbar">
                <div class="admin__data-grid-header-row">
                    <?php if ($block->getDateFilterVisibility()): ?>
                        <div class="admin__filter-actions" data-role="filter-form" id="<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_range') ?>">
                            <span class="field-row">
                                <label for="<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_from') ?>"
                                       class="admin__control-support-text">
                                    <span><?= /* @escapeNotVerified */ __('From') ?>:</span>
                                </label>
                                <input class="input-text no-changes required-entry admin__control-text"
                                       type="text"
                                       id="<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_from') ?>"
                                       name="report_from"
                                       value="<?= $block->escapeHtml($block->getFilter('report_from')) ?>">
                                <span id="<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_from_advice') ?>"></span>
                            </span>

                            <span class="field-row">
                                <label for="<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_to') ?>"
                                       class="admin__control-support-text">
                                    <span><?= /* @escapeNotVerified */ __('To') ?>:</span>
                                </label>
                                <input class="input-text no-changes required-entry admin__control-text"
                                       type="text"
                                       id="<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_to') ?>"
                                       name="report_to"
                                       value="<?= $block->escapeHtml($block->getFilter('report_to')) ?>"/>
                                <span id="<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_to_advice') ?>"></span>
                            </span>

                            <span class="field-row admin__control-filter">
                                 <label for="<?= /* @escapeNotVerified */ $block->getSuffixId('report_period') ?>"
                                        class="admin__control-support-text">
                                     <span><?= /* @escapeNotVerified */ __('Show By') ?>:</span>
                                 </label>
                                <select name="report_period" id="<?= /* @escapeNotVerified */ $block->getSuffixId('report_period') ?>" class="admin__control-select">
                                    <?php foreach ($block->getPeriods() as $_value => $_label): ?>
                                        <option value="<?= /* @escapeNotVerified */ $_value ?>" <?php if ($block->getFilter('report_period') == $_value): ?> selected<?php endif; ?>><?= /* @escapeNotVerified */ $_label ?></option>
                                    <?php endforeach; ?>
                                </select>
                                <?= $block->getRefreshButtonHtml() ?>
                            </span>
                            <script>
                                require([
                                    "jquery",
                                    "mage/calendar"
                                ], function($){

                                    $("#<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_range') ?>").dateRange({
                                        dateFormat:"<?= /* @escapeNotVerified */ $block->getDateFormat() ?>",
                                        buttonText:"<?= /* @escapeNotVerified */ __('Select Date') ?>",
                                        from:{
                                            id:"<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_from') ?>"
                                        },
                                        to:{
                                            id:"<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_to') ?>"
                                        }
                                    });
                                });
                            </script>
                        </div>
                    <?php endif; ?>
                    <?php if ($block->getChildBlock('grid.export')): ?>
                        <?= $block->getChildHtml('grid.export') ?>
                    <?php endif; ?>
                </div>
            </div>
        <?php endif; ?>
        <div class="admin__data-grid-wrap admin__data-grid-wrap-static">
            <table class="data-grid" id="<?= /* @escapeNotVerified */ $block->getId() ?>_table">
                <?= $block->getChildHtml('grid.columnSet') ?>
            </table>
        </div>
    </div>
    <?php if ($block->canDisplayContainer()): ?>
        <script>
            require([
                "jquery",
                "validation",
                "mage/mage",
                "mage/adminhtml/grid"
            ], function(jQuery){

                //<![CDATA[
                <?= /* @escapeNotVerified */ $block->getJsObjectName() ?> = new varienGrid('<?= /* @escapeNotVerified */ $block->getId() ?>', '<?= /* @escapeNotVerified */ $block->getGridUrl() ?>', '<?= /* @escapeNotVerified */ $block->getVarNamePage() ?>', '<?= /* @escapeNotVerified */ $block->getVarNameSort() ?>', '<?= /* @escapeNotVerified */ $block->getVarNameDir() ?>', '<?= /* @escapeNotVerified */ $block->getVarNameFilter() ?>');
                <?= /* @escapeNotVerified */ $block->getJsObjectName() ?>.useAjax = '<?php if ($block->getUseAjax()): /* @escapeNotVerified */ echo $block->getUseAjax(); endif; ?>';
                <?php if ($block->getDateFilterVisibility()): ?>
                <?= /* @escapeNotVerified */ $block->getJsObjectName() ?>.doFilterCallback = validateFilterDate;
                var period_date_from = $('<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_from') ?>');
                var period_date_to = $('<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_to') ?>');
                period_date_from.adviceContainer = $('<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_from_advice') ?>');
                period_date_to.adviceContainer = $('<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_to_advice') ?>');

                var validateFilterDate = function() {
                    if (period_date_from && period_date_to) {
                        var valid = true;
                        jQuery(period_date_from).add(period_date_to).each(function() {
                            valid = Validation.validate(this) && valid;
                        });
                        return valid;
                    }
                    else {
                        return true;
                    }
                }
                <?php endif;?>
                <?php if ($block->getStoreSwitcherVisibility()): ?>
                /* Overwrite function from switcher.phtml widget*/
                switchStore = function(obj) {
                    if (obj.options[obj.selectedIndex].getAttribute('website') == 'true') {
                        var selectionType = 'website';
                    } else if (obj.options[obj.selectedIndex].getAttribute('group') == 'true') {
                        var selectionType = 'group';
                    } else {
                        var selectionType = 'store';
                    }
                    var storeParam = obj.value ? selectionType + '/' + obj.value + '/' : '';
                    if (obj.switchParams) {
                        storeParam += obj.switchParams;
                    }
                    var formParam = new Array('<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_from') ?>', '<?= /* @escapeNotVerified */ $block->getSuffixId('period_date_to') ?>', '<?= /* @escapeNotVerified */ $block->getSuffixId('report_period') ?>');
                    var paramURL = '';
                    var switchURL = '<?= /* @escapeNotVerified */ $block->getAbsoluteGridUrl(['_current' => false]) ?>'.replace(/(store|group|website)\/\d+\//, '');

                    for (var i = 0; i < formParam.length; i++) {
                        if ($(formParam[i]).value && $(formParam[i]).name) {
                            paramURL += $(formParam[i]).name + '=' + escape($(formParam[i]).value) + '&';
                        }
                    }
                    setLocation(switchURL + storeParam + '?' + paramURL);
                }
                <?php endif; ?>
                //]]>

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