<script src="<?php echo getConfig($connect, 'base_url');?>assets/plugins/chartjs/Chart.bundle.js"></script> 
<div class="block-header">
  <h2>DASHBOARD</h2>
</div>
<div class="row clearfix">
      <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
        <div class="info-box bg-pink hover-expand-effect">
          <a href="?page=transaksi"><div class="icon"> <i class="material-icons">playlist_add_check</i> </div></a>
          <div class="content">
            <div class="text">TRANSACTIONS</div>
			<?php
				(int)$j=mysqli_num_rows(mysqli_query($connect, "select ID from transaksi where ID_user='$_SESSION[id_user]' and hapus='0'"));
			?>
            <div class="number count-to" data-from="0" data-to="<?php echo $j;?>" data-speed="1" data-fresh-interval="20"></div>
          </div>
        </div>
      </div>
      <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
        <div class="info-box bg-cyan hover-expand-effect">
          <a href="?page=toko"><div class="icon"> <i class="material-icons">store_mall_directory</i> </div></a>
          <div class="content">
            <div class="text">STORES</div>
			<?php
				(int)$j=mysqli_num_rows(mysqli_query($connect, "select ID from toko where ID_user='$_SESSION[id_user]'"));
			?>
            <div class="number count-to" data-from="0" data-to="<?php echo $j;?>" data-speed="1" data-fresh-interval="20"></div>
          </div>
        </div>
      </div>
      <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
        <div class="info-box bg-light-green hover-expand-effect">
          <a href="?page=data-produk"><div class="icon"> <i class="material-icons">book</i> </div></a>
          <div class="content">
            <div class="text">PRODUCTS</div>
            <?php
				(int)$j=mysqli_num_rows(mysqli_query($connect, "select ID from produk where ID_user='$_SESSION[id_user]'"));
			?>
            <div class="number count-to" data-from="0" data-to="<?php echo $j;?>" data-speed="1" data-fresh-interval="20"></div>
          </div>
        </div>
      </div>
      <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
        <div class="info-box bg-orange hover-expand-effect">
          <a href="?page=kasir"><div class="icon"> <i class="material-icons">person_add</i> </div></a>
          <div class="content">
            <div class="text">OPERATOR</div>
            <?php
				(int)$j=mysqli_num_rows(mysqli_query($connect, "select ID from karyawan where ID_user='$_SESSION[id_user]'"));
			?>
            <div class="number count-to" data-from="0" data-to="<?php echo $j;?>" data-speed="1" data-fresh-interval="20"></div>
          </div>
        </div>
      </div>
    </div>
            
<div class="row">
	<div class="col-md-12">
		<div class="card">
          <div class="header text-center">
            <h2>FAV 5 PRODUCT FROM ALL STORES</h2>
          </div>
          <div class="body">
            <canvas id="horizontal_chart" height="50"></canvas>
          </div>
        </div>
		  <?php
			$qi=mysqli_query($connect, "select * from produk") or die(mysqli_error($connect));
			if(mysqli_num_rows($qi)){
				$q=mysqli_query($connect, "select sum(a.hit) as s_hits,a.* from produk a where a.ID_user='$_SESSION[id_user]' group by lower(a.nama_produk) order by sum(a.hit) desc limit 0,5") or die(mysqli_error($connect));
				$label=array();
				$data=array();
				$color=array();
				while($b=mysqli_fetch_array($q)){
					$label[]=$b['nama_produk'];
					$data[]=$b['s_hits'];
					$color[]=genColorCodeFromText($b['nama_produk']);
				}
				$datas=array('label'=>'Jumlah Penjualan','data'=>$data,'backgroundColor'=>$color);
		?>
		 <script>
		 	$(document).ready(function(e) {
				var data='[<?php echo json_encode($datas);?>]';
				new Chart(document.getElementById("horizontal_chart").getContext("2d"), getChartJs('horizontalBar',data,<?php echo json_encode($label);?>));
			});
	    </script>
		<?php }else{$st=-1;?>
			<script>
		 	$(function(e) {
				$('canvas').closest('.body').html('<i>Belum ada data</i>');
			});
			</script>
		<?php }?>
	</div>
</div>

<div class="row">
	<div class="col-md-8">
		<div class="card">
          <div class="header text-center">
            <h2>STORE SALES CHART ALL THE YEAR</h2>
          </div>
          <div class="body">
            <canvas id="bar_chart" height="100"></canvas>
          </div>
        </div>
		  <?php
			$data='';
			$y=date('Y');
			$bl=12;//date('m');
			$qi=mysqli_query($connect, "select * from toko where ID_user='$_SESSION[id_user]' order by ID asc") or die(mysqli_error($connect));
			if(mysqli_num_rows($qi)){
			$dbul=array();
			while($bi=mysqli_fetch_array($qi)){
				$vdata = '[';
				for($i=1;$i<=$bl;$i++){
					if($i<10){$bulan='0'.$i;}else{$bulan=$i;}
					$q=mysqli_query($connect, "select sum(grandtotal) from transaksi where MONTH(waktu)='$bulan' and YEAR(waktu)='$y' and ID_toko='$bi[ID]' and status='1'  and hapus='0'") or die(mysqli_error($connect));
					while($b=mysqli_fetch_array($q)){
						$vdata.=(int)$b[0].",";
					}	
				}
				$vdata = substr($vdata,0,-1);
				$vdata.="]";
				$dbul[]=$vdata;
				$data.='{"label":"'.$bi['nama_toko'].'","data":'.$vdata.',"backgroundColor": "'.genColorCodeFromText($bi['nama_toko']).'"},';
			}
			$data=substr($data,0,-1);
		?>
		 <script>
		 	$(document).ready(function(e) {
				var data='[<?php echo $data;?>]';
				new Chart(document.getElementById("bar_chart").getContext("2d"), getChartJs('bar',data));
			});
	    </script>
		<?php }else{$st=-1;?>
			<script>
		 	$(function(e) {
				$('canvas').closest('.body').html('<i>Belum ada data</i>');
			});
			</script>
		<?php }?>
	</div>
	<div class="col-md-4">
		<div class="card">
          <div class="header text-center">
            <h2>TRANSACTION COMPARISON GRAPH <strong>SALE</strong> AND <strong>VOID</strong> ALL STORES</h2>
          </div>
          <div class="body">
            <canvas id="pie_chart" height="183"></canvas>
          </div>
        </div>
		<?php
			if($st!=-1){
			$t = mysqli_fetch_array(mysqli_query($connect, "select sum(grandtotal) from transaksi where ID_user='$_SESSION[id_user]' and hapus='0'"));
			$v=mysqli_fetch_array(mysqli_query($connect, "select sum(grandtotal) from transaksi where status!=1 and ID_user='$_SESSION[id_user]' and hapus='0'"));
            $s=mysqli_fetch_array(mysqli_query($connect, "select sum(grandtotal) from transaksi where status=1 and ID_user='$_SESSION[id_user]' and hapus='0'"));
			$void=(int)$v[0];
			$sale=(int)$s[0];
			$total=(int)$t[0];
			$persen_sale = round(($sale/$total)*100,2).'%';
			$persen_void = round(($void/$total)*100,2).'%';
			$data2='{"datasets":[{"data":['.$sale.','.$void.'],"backgroundColor":["rgb(50,11,223)","rgb(228,82,6)"]}],"labels":["Sale ('.$persen_sale.')","Void ('.$persen_void.')"]}';
		?>
		<script>
		$(function(e) {
			var pie_data = '<?php echo $data2;?>';
			new Chart(document.getElementById("pie_chart").getContext("2d"), getChartJs('pie',pie_data));
		});
		</script>
		<?php } ?>
	</div>
</div>
<script src="<?php echo getConfig($connect, 'base_url');?>assets/plugins/jquery-countto/jquery.countTo.js"></script> 
<script>
$(function(e) {
    $('.count-to').countTo();
});
</script>