     <script src="./chartjs/dist/Chart.bundle.js"></script>
<?php
	include('r_minmax_dates.php'); 			
	
				// CHECK DAYS FOR SUGGESTION ON PERIOD WITH $ccmanager_date_diff WHICH COMES r_minmax_dates.php
			if ($ccmanager_date_diff <= 90) 	
				{  
					$date_form1 	= "%d-%m-%Y";
					$date_form2 	= "%d-%b-%Y";
					$chartlabel1	= $nday;
				}
			if ($ccmanager_date_diff > 90) 	
				{  
					$date_form1 = "%m-%Y";
					$date_form2 = "%b-%Y";
					$chartlabel1	= $nmonthly;					
				}


?>


<?php

// FIND SUMMARY TOTAL



include('r_dbc.php');

if ($cidx1 == "total") 
{
$sqlm = "select sum(r.rab_number) as mtotal,count(distinct date_format(r.rab_date,'".$date_form1."')) as count_months
		from ccmanager r, centers c
		where r.pid = '".$pidx."'
		and r.code_center = c.code_center
		and r.pid = c.pid
		and r.mid in (select mid from ccmanager_id where expenses = 1 and pid = c.pid)
		and r.rab_number > 0";
}
if ($cidx1 == "totalg") 
{
$sqlm = "select sum(r.rab_number) as mtotal,count(distinct date_format(r.rab_date,'".$date_form1."')) as count_months
		from ccmanager r, centers c
		where c.code_center_group = '".$cidx2."'
		and r.pid = '".$pidx."'
		and r.code_center = c.code_center
		and r.pid = c.pid
		and r.mid in (select mid from ccmanager_id where expenses = 1 and pid = c.pid)
		and r.rab_number > 0";
}	
if ($cidx1 == "totalc") 
{
$sqlm = "select sum(r.rab_number) as mtotal,count(distinct date_format(r.rab_date,'".$date_form1."')) as count_months
		from ccmanager r, centers c
		where r.code_center = '".$cidx2."'
		and r.pid = '".$pidx."'
		and r.code_center = c.code_center
		and r.pid = c.pid
		and r.mid in (select mid from ccmanager_id where expenses = 1 and pid = c.pid)
		and r.rab_number > 0";
}


$resultm = mysqli_query($conn, $sqlm);		
if (mysqli_num_rows($resultm) > 0) {
      while($rowm = mysqli_fetch_assoc($resultm)) {
		$count_months	= $rowm["count_months"];
		$mtotal 		= $rowm["mtotal"];
		}		
    }


$mtotal = 0;
$mtotr = 0;
$mtotr_td = "";
$mcol_img = "";
$myear_td = "";
$icolor = 0;
$colheight = 250;
$title_chart = $desc_manager_expenses." ".$nchart;


include('r_dbc.php');
if ($cidx1 == "total") 
{
$sql = "select date_format(r.rab_date,'".$date_form2."') as myear,sum(r.rab_number) as mtot
		from ccmanager r, centers c
		where r.pid = '".$pidx."'
		and r.code_center = c.code_center
		and r.pid = c.pid
		and r.mid in (select mid from ccmanager_id where expenses = 1 and pid = c.pid)
		and r.rab_number > 0
		group by date_format(r.rab_date,'".$date_form2."')
		order by r.rab_date";
}
if ($cidx1 == "totalg") 
{
$sql = "select date_format(r.rab_date,'".$date_form2."') as myear,sum(r.rab_number) as mtot
		from ccmanager r, centers c
		where c.code_center_group = '".$cidx2."'
		and r.pid = '".$pidx."'
		and r.code_center = c.code_center
		and r.pid = c.pid
		and r.mid in (select mid from ccmanager_id where expenses = 1 and pid = c.pid)
		and r.rab_number > 0
		group by date_format(r.rab_date,'".$date_form2."')
		order by r.rab_date";
}
if ($cidx1 == "totalc") 
{
$sql = "select date_format(r.rab_date,'".$date_form2."') as myear,sum(r.rab_number) as mtot
		from ccmanager r, centers c
		where r.code_center = '".$cidx2."'
		and r.pid = '".$pidx."'
		and r.code_center = c.code_center
		and r.pid = c.pid
		and r.mid in (select mid from ccmanager_id where expenses = 1 and pid = c.pid)
		and r.rab_number > 0
		group by date_format(r.rab_date,'".$date_form2."')
		order by r.rab_date";
}

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
   // output data of each row
      while($row = mysqli_fetch_assoc($result)) {
		$myear 			= $row["myear"];
		$mtot 			= $row["mtot"];	
		$mtotr			= $mtotr + $mtot;
		$c_mtotr	    = number_format($mtotr, 0, $septh, $sepdc);		
		$c_mtot	    	= number_format($mtot, 0, $septh, $sepdc);		

	$myear_td = $myear_td.",'".$myear."'";		
	$mtotal   = $mtotal.",".$mtot;		
	$mtotr_td = $mtotr_td.",".$mtotr;			

	
		}		
    }

 
mysqli_close($conn);

$myear_td 		= "[".substr($myear_td,1)."]";
$mtotal 		= "[".substr($mtotal,2)."]";
$mtotr_td 		= "[".substr($mtotr_td,1)."]";

// echo $mtotal;

?> 
<div class="row">
	<div class="col-lg">
		<canvas id="line-chart" height="<?php echo $chart_height ?>"></canvas>
	</div>
</div>

<script>
Chart.defaults.global.defaultFontFamily = "Segoe UI";
Chart.defaults.global.defaultFontSize = 14;
new Chart(document.getElementById("line-chart"), {
  type: 'line',
  data: {
    labels: <?php echo $myear_td ?>,
    datasets: [{ 
        data: <?php echo $mtotal ?>,
        label: "<?php echo $chartlabel1; ?>",
        borderColor: "green",
        fill: false
      }, { 
        data: <?php echo $mtotr_td ?>,
        label: "<?php echo $ntotal." ".$nlinear_scale; ?>",
        borderColor: "blue",
        fill: false
      }
    ]
  },
  options: {
	  responsive: true,
    title: {
	  display: true,
      text: '<?php echo $title_chart ?>',
	  height:300
    }
  }
});
</script>
</canvas>