<?php 
//session_start();
include('../includes/dbcon.php');
include('processitem.php');
if(empty($_SESSION['id'])):
header('Location:home.php');
endif;

 $sqlcp=mysqli_query($con,"SELECT SUM(tot_net) AS totnet,SUM(tot_commision) AS totprofit ,branch.branch_code FROM inv_header LEFT JOIN branch ON branch.branch_id=inv_header.branch_id GROUP BY inv_header.branch_id  ")or die(mysqli_error($con));
	     $stack = array();		 
  		$count=mysqli_num_rows($sqlcp);	
		if ($count>0) {
			while($row=mysqli_fetch_array($sqlcp)){
			$data['branch_code'] = $row['branch_code']; 
			$data['pp'] = $row['totnet']; 
			$data['totprofit'] = $row['totprofit'];
			array_push( $stack,$data );			
			}
		}	

$theArray = json_encode( $stack );


//////nawaloka profit
//$sqlpro=mysqli_query($con,"SELECT SUM(tot_net) AS totnet,SUM(tot_commision) AS totprofit ,branch.branch_code ,LEFT(MONTHNAME(inv_date),3)as pmon FROM inv_header LEFT JOIN branch ON branch.branch_id=inv_header.branch_id WHERE branch_code='NAW' GROUP BY inv_header.branch_id,inv_date  ")or die(mysqli_error($con));
	   // $stack_pro = array();		 
  		//$count_pro=mysqli_num_rows($sqlpro);	
		//if ($count_pro>0) {
			//while($row_pro=mysqli_fetch_array($sqlpro)){
			//$data_pro['pmon'] = $row_pro['pmon']; 			
			//$data_pro['totprofit'] = $row_pro['totprofit'];
			//array_push( $stack_pro,$data_pro );			
			//}
		//}	

$sqlpro=mysqli_query($con,"SELECT SUM(CASE WHEN branch_id=4 THEN tot_net ELSE 0 END) AS naw,SUM(CASE WHEN branch_id=2 THEN tot_net ELSE 0 END) AS opx,SUM(CASE WHEN branch_id=3 THEN tot_net ELSE 0 END) AS DEN,SUM(CASE WHEN branch_id=1 THEN tot_net ELSE 0 END) AS LIC,LEFT(MONTHNAME(inv_date),3) AS pmon FROM inv_header GROUP BY inv_date")or die(mysqli_error($con));
	    $stack_pro = array();		 
  		$count_pro=mysqli_num_rows($sqlpro);	
		if ($count_pro>0) {
			while($row_pro=mysqli_fetch_array($sqlpro)){
			$data_pro['pmon'] = $row_pro['pmon']; 			
			$data_pro['naw'] = $row_pro['naw'];
			$data_pro['opx'] = $row_pro['opx'];
			$data_pro['DEN'] = $row_pro['DEN'];
			$data_pro['LIC'] = $row_pro['LIC'];
			array_push( $stack_pro,$data_pro );			
			}
		}


?>

/* global Chart:false */

<script>

$(function () {
  'use strict'
var users = <?php echo json_encode($stack); ?>;
var rowcount= <?php echo ($count); ?>;
//alert(users[2].pp);
var l1 =[];
var branch=[];
var totprofit=[];
 for(var i=0; i<rowcount; i++){
      
	   l1[i] = users[i].pp;
	   branch[i] = users[i].branch_code;
	   totprofit[i] = users[i].totprofit;
	   // alert(totprofit[i]);
    }
	//var d1=users[1].pp;
/////////////////////////////////
var pro_naw = <?php echo json_encode($stack_pro); ?>;
var rowcount_pronaw= <?php echo ($count_pro); ?>;
var label_pro_naw =[];
var profit_naw=[];
var profit_opx=[];
var profit_DEN=[];
var profit_LIC=[];
for(var ip=0; ip<rowcount_pronaw; ip++){
	label_pro_naw[ip] = pro_naw[ip].pmon;
	profit_naw[ip]=pro_naw[ip].naw;
	profit_opx[ip]=pro_naw[ip].opx;
	profit_DEN[ip]=pro_naw[ip].DEN;
	profit_LIC[ip]=pro_naw[ip].LIC;
	 }

/////////////////////




  var ticksStyle = {
    fontColor: '#495057',
    fontStyle: 'bold'
  }

  var mode = 'index'
  var intersect = true

  var $salesChart = $('#sales-chart')
  // eslint-disable-next-line no-unused-vars
  var salesChart = new Chart($salesChart, {
    type: 'bar',
    data: {
      labels:  branch,
      datasets: [
        {
          backgroundColor: '#007bff',
          borderColor: '#007bff',
          data: l1
        },
        {
          backgroundColor: '#ced4da',
          borderColor: '#ced4da',
          data: totprofit
        }
      ]
    },
    options: {
      maintainAspectRatio: false,
      tooltips: {
        mode: mode,
        intersect: intersect
      },
      hover: {
        mode: mode,
        intersect: intersect
      },
      legend: {
        display: false
      },
      scales: {
        yAxes: [{
          // display: false,
          gridLines: {
            display: true,
            lineWidth: '4px',
            color: 'rgba(0, 0, 0, .2)',
            zeroLineColor: 'transparent'
          },
          ticks: $.extend({
            beginAtZero: true,

            // Include a dollar sign in the ticks
            callback: function (value) {
              if (value >= 1000) {
                value /= 1000
                value += 'k'
              }

              return 'Rs. ' + value
            }
          }, ticksStyle)
        }],
        xAxes: [{
          display: true,
          gridLines: {
            display: false
          },
          ticks: ticksStyle
        }]
      }
    }
  })

  var $visitorsChart = $('#visitors-chart')
  // eslint-disable-next-line no-unused-vars
  var visitorsChart = new Chart($visitorsChart, {
    data: {
      labels: label_pro_naw,
      datasets: [{
        type: 'line',
        data: profit_naw,
        backgroundColor: 'transparent',
        borderColor: '#007bff',
        pointBorderColor: '#007bff',
        pointBackgroundColor: '#007bff',
        fill: false
        // pointHoverBackgroundColor: '#007bff',
        // pointHoverBorderColor    : '#007bff'
      },
      {
        type: 'line',
        data: profit_opx,
        backgroundColor: 'tansparent',
        borderColor: '#f56954',
        pointBorderColor: '#ced4da',
        pointBackgroundColor: '#ced4da',
        fill: false
        // pointHoverBackgroundColor: '#ced4da',
        // pointHoverBorderColor    : '#ced4da'
      },
	  
	   {
        type: 'line',
        data: profit_DEN,
        backgroundColor: 'tansparent',
        borderColor: '#00a65a',
        pointBorderColor: '#ced4da',
        pointBackgroundColor: '#ced4da',
        fill: false
        // pointHoverBackgroundColor: '#ced4da',
        // pointHoverBorderColor    : '#ced4da'
      },
	   {
        type: 'line',
        data: profit_LIC,
        backgroundColor: 'tansparent',
        borderColor: '#00c0ef',
        pointBorderColor: '#ced4da',
        pointBackgroundColor: '#ced4da',
        fill: false
        // pointHoverBackgroundColor: '#ced4da',
        // pointHoverBorderColor    : '#ced4da'
      }
	  
	  
	  
	  ]
    },
    options: {
      maintainAspectRatio: false,
      tooltips: {
        mode: mode,
        intersect: intersect
      },
      hover: {
        mode: mode,
        intersect: intersect
      },
      legend: {
        display: false
      },
      scales: {
        yAxes: [{
          // display: false,
          gridLines: {
            display: true,
            lineWidth: '4px',
            color: 'rgba(0, 0, 0, .2)',
            zeroLineColor: 'transparent'
          },
          ticks: $.extend({
            beginAtZero: true,
            suggestedMax: 200
          }, ticksStyle)
        }],
        xAxes: [{
          display: true,
          gridLines: {
            display: false
          },
          ticks: ticksStyle
        }]
      }
    }
  })
  
  
  
  
  
})
</script>
