<?php
/**
 * OpenMage
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>

<?php if (is_array($this->getChild('diagrams')->getTabsIds())) : ?>
<script type="text/javascript">
//<![CDATA[
function changeDiagramsPeriod(periodObj) {
    periodParam = periodObj.value ? 'period/' + periodObj.value + '/' : '';
<?php foreach ($this->getChild('diagrams')->getTabsIds() as $tabId): ?>
    ajaxBlockParam = 'block/tab_<?php echo $tabId ?>/';
    ajaxBlockUrl = '<?php echo $this->getUrl('*/*/ajaxBlock', ['_current' => true, 'block' => '', 'period' => '']) ?>' + ajaxBlockParam + periodParam;
    new Ajax.Request(ajaxBlockUrl, {
        parameters: {isAjax: 'true', form_key: FORM_KEY},
        onSuccess: function(transport) {
            tabContentElementId = '<?php echo $this->getChild('diagrams')->getId() ?>_<?php echo $tabId ?>_content';
            try {
                if (transport.responseText.isJSON()) {
                    var response = transport.responseText.evalJSON()
                    if (response.error) {
                        alert(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 $this->getUrl('*/*/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(response.message);
                    }
                    if(response.ajaxExpired && response.ajaxRedirect) {
                        setLocation(response.ajaxRedirect);
                    }
                } else {
                    $(tabContentElementId).replace(transport.responseText);
                }
            }
            catch (e) {
                $(tabContentElementId).replace(transport.responseText);
            }
        }
    });
}

function toggleCal(id) {
    $('dashboard_'+id+'_cal_div').toggle();
    $('dashboard_'+id+'_range_div').toggle();
}
//]]>
</script>
<?php endif ?>
<div class="content-header">
    <table cellspacing="0">
        <tr>
            <td><h3 class="head-dashboard"><?php echo $this->__('Dashboard') ?></h3></td>
        </tr>
    </table>
</div>
<div class="dashboard-container">
    <?php echo $this->getChildHtml('store_switcher') ?>
    <table cellspacing="25" width="100%">
        <tr>
            <td><?php echo $this->getChildHtml('sales') ?>
                <div class="entry-edit">
                    <div class="entry-edit-head"><h4><?php echo $this->__('Last 5 Orders') ?></h4></div>
                    <fieldset class="np"><?php echo $this->getChildHtml('lastOrders'); ?></fieldset>
                </div>
                <div class="entry-edit">
                    <div class="entry-edit-head"><h4><?php echo $this->__('Last 5 Search Terms') ?></h4></div>
                    <fieldset class="np"><?php echo $this->getChildHtml('lastSearches'); ?></fieldset>
                </div>
                <div class="entry-edit">
                    <div class="entry-edit-head"><h4><?php echo $this->__('Top 5 Search Terms') ?></h4></div>
                    <fieldset class="np"><?php echo $this->getChildHtml('topSearches'); ?></fieldset>
                </div>
            </td>
            <td>
                <div class="entry-edit" style="border:1px solid #ccc;">
                    <?php echo $this->getChildHtml('diagrams') ?>
                    <?php if (is_array($this->getChild('diagrams')->getTabsIds())) : ?>
                        <div id="diagram_tab_content"></div>
                    <?php endif ?>
                    <div style="margin:20px;">
                        <?php echo $this->getChildHtml('totals') ?>
                    </div>
                    <div style="margin:20px;">
                        <?php echo $this->getChildHtml('grids') ?>
                        <div id="grid_tab_content"></div>
                    </div>
                </div>
            </td>
        </tr>
    </table>
</div>
