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

<?php if (is_array($block->getChildBlock('diagrams')->getTabsIds())) : ?>
<script>
require([
    'Magento_Ui/js/modal/alert',
    'prototype'
], function(alert){

window.changeDiagramsPeriod = function(periodObj) {
    periodParam = periodObj.value ? 'period/' + periodObj.value + '/' : '';
    <?php foreach ($block->getChildBlock('diagrams')->getTabsIds() as $tabId) : ?>
    ajaxBlockParam = 'block/tab_<?= $block->escapeJs($tabId) ?>/';
    ajaxBlockUrl = '<?= $block->escapeJs($block->getUrl('adminhtml/*/ajaxBlock', ['_current' => true, 'block' => '', 'period' => ''])) ?>' + ajaxBlockParam + periodParam;
    new Ajax.Request(ajaxBlockUrl, {
        parameters: {isAjax: 'true', form_key: FORM_KEY},
        onSuccess: function(transport) {
            tabContentElementId = '<?= $block->escapeJs($block->getChildBlock('diagrams')->getId()) ?>_<?= $block->escapeJs($tabId) ?>_content';
            try {
                if (transport.responseText.isJSON()) {
                    var response = transport.responseText.evalJSON()
                    if (response.error) {
                        alert({
                            content: response.message
                        });
                    }
                    if(response.ajaxExpired && response.ajaxRedirect) {
                        setLocation(response.ajaxRedirect);
                    }
                } else {
                    $(tabContentElementId).update(transport.responseText);
                }
            }
            catch (e) {
                $(tabContentElementId).update(transport.responseText);
            }
        }
    });
    <?php endforeach; ?>
    ajaxBlockUrl = '<?= $block->escapeJs($block->getUrl('adminhtml/*/ajaxBlock', ['_current' => true, 'block' => 'totals', 'period' => ''])) ?>' + periodParam;
    new Ajax.Request(ajaxBlockUrl, {
        parameters: {isAjax: 'true', form_key: FORM_KEY},
        onSuccess: function(transport) {
            tabContentElementId = 'dashboard_diagram_totals';
            try {
                if (transport.responseText.isJSON()) {
                    var response = transport.responseText.evalJSON();
                    if (response.error) {
                        alert({
                            content: response.message
                        });
                    }
                    if(response.ajaxExpired && response.ajaxRedirect) {
                        setLocation(response.ajaxRedirect);
                    }
                } else {
                    $(tabContentElementId).replace(transport.responseText);
                }
            }
            catch (e) {
                $(tabContentElementId).replace(transport.responseText);
            }
        }
    });
}

});
</script>
<?php endif; ?>
<div class="dashboard-container row">
    <div class="dashboard-main col-m-8 col-m-push-4">
        <div class="dashboard-diagram-container">
            <?= $block->getChildHtml('diagrams') ?>
            <?php if (is_array($block->getChildBlock('diagrams')->getTabsIds())) : ?>
                <div id="diagram_tab_content" class="dashboard-diagram-tab-content"></div>
            <?php endif; ?>
        </div>
        <?= $block->getChildHtml('totals') ?>
        <div class="dashboard-store-stats">
            <?= $block->getChildHtml('grids') ?>
            <div id="grid_tab_content" class="dashboard-store-stats-content"></div>
            <?= $block->getChildHtml('refresh_statistics') ?>
        </div>
    </div>
    <div class="dashboard-secondary col-m-4 col-m-pull-8">
        <?= $block->getChildHtml('sales') ?>
        <div class="dashboard-item">
            <div class="dashboard-item-title"><?= $block->escapeHtml(__('Last Orders')) ?></div>
            <?= $block->getChildHtml('lastOrders') ?>
        </div>
        <div class="dashboard-item">
            <div class="dashboard-item-title"><?= $block->escapeHtml(__('Last Search Terms')) ?></div>
            <?= $block->getChildHtml('lastSearches') ?>
        </div>
        <div class="dashboard-item">
            <div class="dashboard-item-title"><?= $block->escapeHtml(__('Top Search Terms')) ?></div>
            <?= $block->getChildHtml('topSearches') ?>
        </div>
    </div>
</div>
