<?php 

	$lang	= $_GET['lang'];
	$pid	= $_GET['pid'];
	$xuid	= $_GET['xuid'];


	$cidx	= $_POST['cid'];
	$langx	= $_POST['langx'];
	$pidx	= $_POST['pidx'];
	$xuidx	= $_POST['xuidx'];	

	include('users.php');
	
	include('field_names.php'); 	
	
	
	// CHECK OS FOR HEIGHT OF CHARTS
	include('getos.php');	
	if ($getos == 'Windows' || $getos == 'Mac')
		{ $chart_height = 100;  }
	else { $chart_height = 300;  }
	
	
 ?>
<?php	
// EXPENSES DESC MANAGER
include('r_dbc.php');
$sqlmid = "select desc_manager
			from ccmanager_id 
			where pid='".$pid."' 
			and expenses = 1";

$resultmid = mysqli_query($conn, $sqlmid);

if (mysqli_num_rows($resultmid) > 0) {
      while($rowmid = mysqli_fetch_assoc($resultmid)) {
		$desc_manager_expenses		= $rowmid["desc_manager"];
    }
} 
mysqli_close($conn);
?>
<html>
<title><?php echo $n_center." ".$desc_manager_expenses." ".$nchart; ?></title>
<head>

<?php
		// CHECK FOR GOOGLE ANALYTICS
		$ip=$_SERVER['REMOTE_ADDR'];
		$ip = substr($ip,0,3);

			if ($ip != '192') {
				include('google.php');
			}	
?>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<title><?php echo $desc_manager_expenses." ".$ntotals." ".$nview; ?></title>
	
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.css" >

<style>
table {
  border-collapse: collapse;
  border-spacing: 0;
  width: 98%;
  border: 1px solid black;
}

td {
  font-family:verdana;
  padding: 8px;
  border: 1px solid black;
  font-size: <?php echo $rep_font_size12 ?>;    
}

th {
  font-family:verdana;
  padding: 8px;
  border: 1px solid black;
  background-color: #D6D6D6;
  font-size: <?php echo $rep_font_size12 ?>;    
}

tr:nth-child(even){background-color: #f2f2f2}


p.a {
  font-family:verdana;
  text-align:right;
  font-size: <?php echo $rep_font_size14 ?>;    
}

</style>	

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

</head>
<body>
<br>
		<div class='container'>
		<div class='col-lg-12'>
<?php	if ($cidxv == '') {		?>
<form method="post">
<div class="mb-3">
<?php
echo "<select name='cid' class='chosen form-control' > ";
printf ("<option value = ''> $nchoose $n_center_group $nor $n_center </option>");
include('r_dbc.php');
 		$sql = "select 'total' as cid,'' as code_center,upper('".$nworld."') as desc_center,'01' as ordr
		UNION
		select concat('totalg',c.code_center_group) as cid,c.code_center_group as code_center,concat('- ',upper(g.desc_center_group)) as desc_center,'02' as ordr
		from ccmanager r, centers_groups g, centers c
		where r.pid = '".$pid."'
		and r.pid = c.pid
		and c.code_center_group = g.code_center_group
		and c.pid = g.pid
		and r.mid in (select mid from ccmanager_id where expenses = 1 and pid = c.pid)
		and r.rab_number > 0
		and r.code_center = c.code_center
		".$user_cc1."	
		UNION
		select distinct concat('totalc',r.code_center) as cid,r.code_center,concat('- ',c.desc_center) as desc_center,'03' as ordr
		from ccmanager r, centers c
		where r.pid = '".$pid."'
		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
		and r.code_center = c.code_center
		".$user_cc1."		
 		order by ordr,code_center";

$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
      while($row = mysqli_fetch_assoc($result)) {
		printf ("<option value = %s > %s %s </option>", $row["cid"],$row["code_center"],$row["desc_center"]);
		}
	}
mysqli_close($conn);
echo "</select>";

		// echo $sql;

?>  
<input type="hidden" id="langx" name="langx" value="<?php echo $lang ?>" >
<input type="hidden" id="pidx" name="pidx" value="<?php echo $pid ?>" >
<input type="hidden" id="xuidx" name="xuidx" value="<?php echo $xuid ?>" >
<br><br>
	<div class="d-grid gap-2">
		<button name="submit" type="submit" class="btn btn-primary btn-block" > <?php echo $nrun_report ?> </button></p>
	</div>
</div>
</form>
<?php } ?>
<br>

<?php 

	if ($cidxv != '') 
		{ 
			$cidx = $cidxv; 
			$langx	= $lang;
			$pidx	= $pid;
			$xuidx	= $xuid;			
		}
	
	// echo $cidxv." -- ".$cidx;

	if ($cidx != '') {
		$cidx1 = trim(substr($cidx,0,6));
		$cidx2 = trim(substr($cidx,6,100));
		 // echo "<br><br>".$cidx1." ".$cidx2;

	
			// DESC OF CODE CENTER
			include('r_dbc.php');
			$sqlmid = "select desc_center
						from centers 
						where pid='".$pidx."' 
						and code_center = '".$cidx2."'
						UNION
						select desc_center_group as desc_center
						from centers_groups
						where pid='".$pidx."' 
						and code_center_group = '".$cidx2."'";
			
			$resultmid = mysqli_query($conn, $sqlmid);

			if (mysqli_num_rows($resultmid) > 0) {
				  while($rowmid = mysqli_fetch_assoc($resultmid)) {
					$desc_center		= $rowmid["desc_center"];
				}
			} 
			mysqli_close($conn);
		// echo "<div class='container'>";
		// echo "<div class='col-lg-12'>";
		echo "<h4>".$desc_center."</h4>";

		if (str_word_count($desc_center) == 1 && $cidx1 == "totalc") {
			$filename = "https://flags.byte12.com/flags/images/flags/png/".strtolower($desc_center).".png";

			// Get dimensions of the image 
			 list($imwidth, $imheight, $imtype, $imattr) = getimagesize($filename);   
			if ($imwidth != "") {
						echo "<img src='".$filename."' width=60 height=auto ><br>";
			}
		}
		
		if ($pidx == 701) {
			// POPULATION
			include('r_dbc.php');
			if ($cidx1 == "total") 
			{			
			$sqlmid = "select sum(rab_number) as population
						from ccmanager
						where pid='".$pid."'
						and mid = 1";
			}
			if ($cidx1 == "totalg") 
			{			
			$sqlmid = "select sum(rab_number) as population
						from ccmanager
						where pid='".$pid."'
						and code_center in (select code_center from centers where code_center_group = '".$cidx2."')
						and mid = 1";
			}			
			if ($cidx1 == "totalc") 
			{			
			$sqlmid = "select rab_number as population
						from ccmanager
						where pid='".$pid."'
						and code_center = '".$cidx2."'
						and mid = 1";
			}
			$resultmid = mysqli_query($conn, $sqlmid);

			if (mysqli_num_rows($resultmid) > 0) {
				  while($rowmid = mysqli_fetch_assoc($resultmid)) {
					$population	= $rowmid["population"];
				}
			} 
			mysqli_close($conn);
		
		$c_population  	= number_format($population, 0, $septh, $sepdc);
		
		echo "   ".$npopulation." : ".$c_population;

		}


		if ($pidx != 701) {		
		include('r_filter_center_chart_1.php');
		echo "<br><br>";	
		include('r_filter_center_chart_2.php');
		}
		if ($pidx == 701) {
		include('r_filter_center_chart_1_701.php');
		echo "<br><br>";	
		include('r_filter_center_chart_1.php');
		echo "<br><br>";						
		include('r_filter_center_chart_2_701.php');
		}

	}
?>

<?php
	if ($cidx != "") {
	include('system_date.php');	
	}
		if ($pidx == 701) {	
	printf("<hr style='border-top: 3px solid black;'>");
	printf("<p class='a'>");
	printf("Powered by <a href='https://www.rabio.eu' target=_blank >Rapid Business Information Organizer</a>");
	printf("<br> <a href='https://www.rabio.eu' target=_blank >www.rabio.eu</a>");
	printf("<br> Data Source <a href='https://data.europa.eu/euodp/en/data/dataset/covid-19-coronavirus-data' target=_blank >EU Open Data Portal</a>");
	printf("</p>");
		}
		echo "</div>";
		echo "</div>";	
?>

</body>
<?php 
include('nf_chosen02.php'); 
	include('views.php');
?>
</html>