<?php 

			// Project ID
			$pid 	= $_GET['pid'];
			$xuid 	= $_GET['xuid'];
			$lang 	= $_GET['lang'];
			include('field_names.php'); 			
			include('users.php'); 
			// include('menu_side.php');		

?>
<?php
/* COUNT CENTERS & CATEGORIES FOR HEIGHT OF BAR CHARTS */
include('r_dbc.php');

$sql = "select count(distinct r.code_center) as cnt_center,count(distinct r.code_categ) as cnt_categ
		from ccmanager r
		where r.pid = '".$pid."'
		and r.mid in (select x.mid from ccmanager_id x where x.pid = '".$pid."' and x.expenses = 1)";
		
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
   // output data of each row
      while($row = mysqli_fetch_assoc($result)) {
		$cnt_center 	= $row["cnt_center"];
		$cnt_categ 		= $row["cnt_categ"];
		}		
    }
 
mysqli_close($conn);

// if ($cnt_center <= 10) { $chartheight = 200; }
// elseif ($cnt_center > 10 && $cnt_center <= 20) { $chartheight = 300; }
// elseif ($cnt_center > 20) { $chartheight = 400; }

// if ($cnt_categ <= 10) { $chartheight_cat = 200; }
// elseif ($cnt_categ > 10 && $cnt_center <= 20) { $chartheight_cat = 300; }
// elseif ($cnt_categ > 20) { $chartheight_cat = 400; }
$chartheight_cat = 100;
$chartheight = 100;
?>

<?php
/* HEADERS FROM CCMANAGER ID */
include('r_dbc.php');
$sqlmg = "select desc_manager
			from ccmanager_id 
			where pid='".$pid."' 
			and expenses = 1";

$resultmg = mysqli_query($conn, $sqlmg);

if (mysqli_num_rows($resultmg) > 0) {
      while($rowmg = mysqli_fetch_assoc($resultmg)) {
		$desc_manager	= $rowmg["desc_manager"];
	  }
}
mysqli_close($conn);

?>


<?php
/* STATISTICS FOR HEADER */
include('r_dbc.php');

$sql = "select '01' as actv,count(cid) as mrecs
		from ccmanager
		where pid = '".$pid."'
		and date_format(crdate,'%d-%m-%Y') = date_format(now(),'%d-%m-%Y')
		UNION
		select '02' as actv,count(cid) as mrecs
		from ccmanager
		where pid = '".$pid."'
		and date_format(crdate,'%m-%Y') = date_format(now(),'%m-%Y')		
		UNION
		select '03' as actv,sum(rab_number) as mrecs
		from ccmanager
		where pid = '".$pid."'
		and mid in (select x.mid from ccmanager_id x where x.pid = '".$pid."' and x.expenses = 1)
		and rab_date >= (NOW() - INTERVAL 7 DAY)
		and rab_date <= NOW()
		UNION
		select '04' as actv,sum(rab_number) as mrecs
		from ccmanager
		where pid = '".$pid."'
		and mid in (select x.mid from ccmanager_id x where x.pid = '".$pid."' and x.expenses = 1)
		and date_format(rab_date,'%m-%Y') = date_format(now(),'%m-%Y')";

// echo $sql;

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

if (mysqli_num_rows($result) > 0) {
   // output data of each row
      while($row = mysqli_fetch_assoc($result)) {
		  $actv = $row["actv"];
		  if ($actv == '01') {	$today_recs 	= round($row["mrecs"],0); }
		  if ($actv == '02') {	$month_recs 	= round($row["mrecs"],0); }
		  if ($actv == '03') {	$week_exps 	= number_format($row["mrecs"], $totdc, $septh, "."); }
		  if ($actv == '04') {	$month_exps 	= number_format($row["mrecs"], $totdc, $septh, "."); }
	
		}		
    }
 
mysqli_close($conn);


?>

<!doctype html>
<html lang="en-US" class="no-js">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<title><?php include('title.php'); echo " - ".$title_dashboard; ?></title>
	
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.css" >
    <link rel="stylesheet" href="./fontawesome/css/all.css" >
    <script src="./chartjs/dist/Chart.bundle.js"></script>
	<script src="./jquery/jquery-3.4.1.js"></script>
	
<style>
body {
  background-image: linear-gradient(to bottom right, #d6d6d6, #d3d3d3);
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

</style>	
	
</head>

<body>

	<?php
		$border_radius 	= "5px";
		$div_bgcolor 	= "#ffffff";
		$borderwidth	= "";
	?>

    <div class="container-xxl">
      <div class="header clearfix">
        <h3><?php include('title.php'); ?></h3>
        <h4><?php echo $title_dashboard; ?></h4>
      </div>
	  
    <div class="row marketing">
        <div class="col-lg-5 text-white bg-primary text-center" style="border-radius: <?php echo $border_radius ?>; background-color: <?php echo $div_bgcolor; ?> " >
				<br>
				<b><?php echo $n_today_recs ?>:</b> <?php echo $today_recs." ".$nrecords; ?>
				<br><br>
		</div>
        
		<div class="col-lg-2">
		</div>		
		

        <div class="col-lg-5 text-white bg-secondary text-center" style="border-radius: <?php echo $border_radius ?>; background-color: <?php echo $div_bgcolor; ?>" >
				<br>		
					<b><?php echo $n_month_recs ?>:</b> <?php echo $today_recs." ".$nrecords; ?>
				<br><br>					
		</div>
	</div>


<br>

    <div class="row marketing">
	
        <div class="col-lg-5 text-white bg-primary text-center" style="border-radius: <?php echo $border_radius ?>; background-color: <?php echo $div_bgcolor; ?>" >
			<br>			
				<b><?php echo $n_week_exps." ".$desc_manager; ?>:</b> <?php echo $week_exps ?>
			<br><br>				
		</div>

		<div class="col-lg-2">
		</div>				

        <div class="col-lg-5 text-white bg-secondary text-center" style="border-radius: <?php echo $border_radius ?>; background-color: <?php echo $div_bgcolor; ?>" >
			<br>			
				<b><?php echo $n_month_exps." ".$desc_manager; ?>:</b> <?php echo $month_exps ?>
			<br><br>
		</div>

    </div>	  



	<br>

      <div class="row marketing">
		<div class="col-lg-12 text-dark" style="border-radius: <?php echo $border_radius ?>; background-color: <?php echo $div_bgcolor; ?>" >
        
		<canvas id="line-chart" height="80" ></canvas>
			<?php 
				include('Chart.Line.php');
			?>
		</div>
      </div>

	<br>
	
      <div class="row marketing">
	<div class="col-lg-3 text-dark" style="border-radius: <?php echo $border_radius ?>; background-color: <?php echo $div_bgcolor; ?>" >
				<canvas id="pie-chart-GroupsCen" height="300" ></canvas>
				<?php 
				include('Chart.Pie.GroupsCen.php');
				?>          
        </div>
		<div class="col-lg-1">
		</div>
		<div class="col-lg-8 text-dark" style="border-radius: <?php echo $border_radius ?>; background-color: <?php echo $div_bgcolor; ?>" >
					<canvas id="bar-chart-horizontal-center"  height="<?php echo $chartheight ?>" ></canvas>
						<?php 
						include('Chart.Bar.Cen.php');
						?>


        </div>		
		<div class="col-lg-1">
		</div>		

		</div>


 	  <?php if ($cnt_categ > 1) { ?> 
	  <br><br>

		<div class="row marketing">

		<div class="col-lg-3 text-dark" style="border-radius: <?php echo $border_radius ?>; background-color: <?php echo $div_bgcolor; ?>" >
				<canvas id="pie-chart-GroupsCat" height="300" ></canvas>
				<?php 
				include('Chart.Pie.GroupsCat.php');
				?>												      		
        </div>
		<div class="col-lg-1">
		</div>										
				
	<div class="col-lg-8 text-dark" style="border-radius: <?php echo $border_radius ?>; background-color: <?php echo $div_bgcolor; ?>" >
					<canvas id="bar-chart-horizontal-categ" height="<?php echo $chartheight_cat ?>" ></canvas>
					<?php 
					include('Chart.Bar.Cat.php');
					?>								
				</div>
      </div>	  
	  
	  <?php } ?>

	<div class="row marketing">
		<div class="col-lg-12 text-dark text-end" >
			<?php
				include('system_date.php');	
			?>
		</div>
	</div>



    </div> <!-- /container -->



</body>
</html>

<?php	include('views.php'); ?>