<?php
/**
 * @version     1.3.3
 * @package     sellacious
 *
 * @copyright   Copyright (C) 2012-2017 Bhartiy Web Technologies. All rights reserved.
 * @license     SPL Sellacious Private License; see http://www.sellacious.com/spl.html
 * @author      Izhar Aazmi <info@bhartiy.com> - http://www.bhartiy.com
 */
// no direct access
defined('_JEXEC') or die;

/** @var  $this SellaciousViewDashboard */
JHtml::_('jquery.framework');
JHtml::_('behavior.framework');
JHtml::_('bootstrap.tooltip');
JHtml::_('behavior.keepalive');

$doc = JFactory::getDocument();
$doc->addScriptVersion('templates/sellacious/js/plugin/flot/jquery.flot.cust.js');
$doc->addScriptVersion('templates/sellacious/js/plugin/flot/jquery.flot.resize.min.js');
$doc->addScriptVersion('templates/sellacious/js/plugin/flot/jquery.flot.fillbetween.min.js');
$doc->addScriptVersion('templates/sellacious/js/plugin/flot/jquery.flot.orderBar.min.js');
$doc->addScriptVersion('templates/sellacious/js/plugin/flot/jquery.flot.pie.min.js');
$doc->addScriptVersion('templates/sellacious/js/plugin/flot/jquery.flot.tooltip.js');
$doc->addScriptVersion('templates/sellacious/js/plugin/moment/moment.js');
$doc->addScriptVersion('templates/sellacious/js/plugin/moment/moment-timezone-with-data.min.js');
$doc->addScriptVersion('templates/sellacious/js/plugin/ion-slider/ion.rangeSlider.js');

JHtml::_('stylesheet', 'com_sellacious/view.dashboard.graph.css', null, true);

/** @var  JDate  $start */
/** @var  JDate  $end */
$start = $this->helper->core->fixDate('now', 'UTC', null)->setTime(0, 0, 0)->modify('-29 day');
$end   = $this->helper->core->fixDate('now', 'UTC', null)->setTime(23, 59, 59);
$min   = $start->toUnix() + $start->getOffsetFromGmt();
$max   = $end->toUnix() + $end->getOffsetFromGmt();

$chartC = array();
$chartA = array();
$chartV = array();

$stats = array_slice($this->orderStats, 1);

foreach ($stats as $row)
{
	$chartC[] = array($row->ts * 1000, (int) $row->count, $row->date);
	$chartA[] = array($row->ts * 1000, (float) $row->value, $row->date);
}

if ($this->helper->access->check('statistics.visitor'))
{
	$stats = $this->helper->report->getPageViewStats(30, 'now', false);

	foreach ($stats as $row)
	{
		$chartV[] = array($row->ts * 1000, (int) $row->count, $row->date);
	}
}
?>
<script>
	jQuery(function ($) {
		$(document).ready(function () {
			var dashboardC = new SellaciousDashboard;
			dashboardC.init({
					min: <?php echo $min; ?>,
					max: <?php echo $max; ?>,
					range: 'm'
				},
				'#stats-chart0',
				'#range-slider0',
				<?php echo json_encode($chartC); ?>
			);

			var dashboardA = new SellaciousDashboard;
			dashboardA.init({
					min: <?php echo $min; ?>,
					max: <?php echo $max; ?>,
					range: 'm'
				},
				'#stats-chart1',
				'#range-slider1',
				<?php echo json_encode($chartA); ?>
			);

			<?php if (count($chartV)): ?>
			var dashboardV = new SellaciousDashboard;
			dashboardV.init({
					min: <?php echo $min; ?>,
					max: <?php echo $max; ?>,
					range: 'm'
				},
				'#stats-chart2',
				'#range-slider2',
				<?php echo json_encode($chartV); ?>
			);
			<?php endif; ?>
		});
	});
</script>
<section id="widget-grid">
	<!-- row -->
	<div class="row">
		<article class="col-sm-12">
			<!-- new widget -->
			<div class="jarviswidget">

				<header>
					<span class="widget-icon"> <i class="fa fa-bar-chart txt-color-darken"></i> </span>
					<h2>Statistics</h2>
					<ul class="nav nav-tabs pull-right in" id="myTab">
						<li class="active">
							<a data-toggle="tab" href="#s0"><i class="fa fa-clock-o"></i> <span
									class="hidden-mobile hidden-tablet">Daily Orders Count</span></a>
						</li>
						<li>
							<a data-toggle="tab" href="#s1"><i class="fa fa-money"></i> <span
									class="hidden-mobile hidden-tablet">Daily Orders Revenue</span></a>
						</li>
						<?php if (count($chartV)): ?>
						<li>
							<a data-toggle="tab" href="#s2"><i class="fa fa-globe"></i> <span
									class="hidden-mobile hidden-tablet">Daily Page Views</span></a>
						</li>
						<?php endif; ?>
					</ul>
				</header>

				<!-- widget div-->
				<div class="widget-body">
					<!-- content -->
					<div id="myTabContent" class="tab-content">
						<div class="tab-pane fade in no-padding-bottom active" id="s0">
							<div class="chart-area w100p">
								<input id="range-slider0" type="hidden" title="">
								<div id="stats-chart0" class="chart-large txt-color-blue"><!-- Graph / chart would appear here --></div>
								<div id="axis-label-y0" class="axis-label-y">Orders Count</div>
							</div>
						</div>

						<div class="tab-pane fade in no-padding-bottom" id="s1">
							<div class="chart-area w100p">
								<input id="range-slider1" type="hidden" title="">
								<div id="stats-chart1" class="chart-large txt-color-blue"><!-- Graph / chart would appear here --></div>
								<div id="axis-label-y1" class="axis-label-y">Orders Revenue
									(<?php echo $this->helper->currency->forUser(null, 'code_3'); ?>)</div>
							</div>
						</div>

						<?php if (count($chartV)): ?>
						<div class="tab-pane fade in no-padding-bottom" id="s2">
							<div class="chart-area w100p">
								<input id="range-slider2" type="hidden" title="">
								<div id="stats-chart2" class="chart-large txt-color-blue"><!-- Graph / chart would be here --></div>
								<div id="axis-label-y2" class="axis-label-y">Page Views</div>
							</div>
						</div>
						<?php endif; ?>
					</div>
					<!-- end content -->
				</div>
				<!-- end widget div -->
			</div>
			<!-- end widget -->
		</article>
	</div>
	<!-- end row -->
</section>

