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

// @codingStandardsIgnoreFile

?>

<?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_<?php /* @escapeNotVerified */ echo $tabId ?>/';
    ajaxBlockUrl = '<?php echo $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 = '<?php /* @escapeNotVerified */ echo $block->getChildBlock('diagrams')->getId() ?>_<?php /* @escapeNotVerified */ echo $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 = '<?php echo $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">
            <?php echo $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>
        <?php echo $block->getChildHtml('totals') ?>
        <div class="dashboard-store-stats">
            <?php echo $block->getChildHtml('grids') ?>
            <div id="grid_tab_content" class="dashboard-store-stats-content"></div>
            <?php echo $block->getChildHtml('refresh_statistics') ?>
        </div>
    </div>
    <div class="dashboard-secondary col-m-4 col-m-pull-8">
        <?php echo $block->getChildHtml('sales') ?>
        <div class="dashboard-item">
            <div class="dashboard-item-title"><?php /* @escapeNotVerified */ echo __('Last Orders') ?></div>
            <?php echo $block->getChildHtml('lastOrders'); ?>
        </div>
        <div class="dashboard-item">
            <div class="dashboard-item-title"><?php /* @escapeNotVerified */ echo __('Last Search Terms') ?></div>
            <?php echo $block->getChildHtml('lastSearches'); ?>
        </div>
        <div class="dashboard-item">
            <div class="dashboard-item-title"><?php /* @escapeNotVerified */ echo __('Top Search Terms') ?></div>
            <?php echo $block->getChildHtml('topSearches'); ?>
        </div>
    </div>
</div>
