<?php

include('r_dbc.php');

$ccs = "";
$ccexpenses_tot = "";

$sql = "select c.code_center_group,g.desc_center_group,sum(r.rab_number) as ccexpenses
		from ccmanager r, centers c, centers_groups g
		where r.pid = '".$pid."'
		and r.pid = c.pid
		and r.code_center = c.code_center
		and c.pid = g.pid
		and c.code_center_group = g.code_center_group
		and r.mid in (select x.mid from ccmanager_id x where x.pid = '".$pid."' and x.expenses = 1)
		group by c.code_center_group
		order by 3 desc
		limit 0,10";

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

if (mysqli_num_rows($result) > 0) {
   // output data of each row
      while($row = mysqli_fetch_assoc($result)) {
		$code_center_group 	= $row["code_center_group"];
		$desc_center_group 	= $row["desc_center_group"];		
		$cc				= $desc_center_group;
		$ccexpenses 	= $row["ccexpenses"];
		$c_ccexpenses	= number_format($ccexpenses, $totdc, $septh, $sepdc);

		// $ccwidth = $barwidth  *($ccexpenses / $ccexpenses_max);
		
		$ccs = $ccs."','".$cc;
		$ccexpenses_tot		= $ccexpenses_tot.",".$ccexpenses;		
		}		
    }
 
mysqli_close($conn);

$ccs 			= "[".substr($ccs,2)."']";
$ccexpenses_tot = "[".substr($ccexpenses_tot,1)."]";


?>

<script>

new Chart(document.getElementById("pie-chart-GroupsCen"), {
    type: 'pie',
    data: {
      labels: <?php echo $ccs ?>,
      datasets: [{
        label: "<?php echo $title_chart_pie ?>",
        backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
        data: <?php echo $ccexpenses_tot; ?>
      }]
    },
    options: {
	 responsive: true,
      title: {
        display: true,
        text: '<?php echo $desc_manager." ".$title_chart_pie_center ?>'
      },
legend: {
			position : 'bottom'
		}
    }
});

</script>