<?php require('head.php');
if(@$infos_user['droit'] > 0)
{
?>
<script src="js/highcharts.js"></script>
<script src="js/modules/exporting.js"></script>
<script type="text/javascript" src="calendrier.js"></script>

<table class="ds_box" cellpadding="0" cellspacing="0" id="ds_conclass" style="display: none;">
			<tr>
				<td id="ds_calclass"></td>
			</tr>
</table>

<?php
if(isset($_GET['du'], $_GET['au']) && strtotime($_GET['du']) && strtotime($_GET['au']))
{
	$date1 = date('Y-m-d', strtotime($_GET['du']));
	$date2 = date('Y-m-d', strtotime($_GET['au']));
}
else
{
	$date1 = date('Y-m-d', strtotime("-1 month", strtotime(date('Y-m-d'))));
	$date2 = date('Y-m-d');
}

$result_vol_6 = $connexion->query("SELECT * FROM chateau WHERE heure='06:00:00' and date >= '".$date1."' and date <= '".$date2."' ORDER BY date, heure ASC");
$result_vol_22 = $connexion->query("SELECT * FROM chateau WHERE heure='22:00:00' and date >= '".$date1."' and date <= '".$date2."' ORDER BY date, heure ASC");

$while_date = $date1;

$s_date = '';

while(strtotime($while_date) <= strtotime($date2))
{
	$tab_vol_6[$while_date] = 'null';
	$tab_vol_22[$while_date] = 'null';
	
	$s_date = $s_date.'"'.date("d/m/y", strtotime($while_date)).'",';
	$while_date = date("Y-m-d", strtotime("+1 day", strtotime($while_date)));
}

while($fetch = $result_vol_6->fetch())
{
	$tab_vol_6[$fetch['date']] = $fetch['compteur_impulsion'];
}
while($fetch = $result_vol_22->fetch())
{
	$tab_vol_22[$fetch['date']] = $fetch['compteur_impulsion'];
}

$s_vol_6 = '';
$s_vol_22= '';
$volmin = 100;
$volmax = 0;
$voltot = 0;

$while_date = $date1;
for($i = 0; $i < count($tab_vol_6); $i++)
{
	if(isset($tab_vol_22[date("Y-m-d", strtotime("-1 day", strtotime($while_date)))]) && $tab_vol_22[date("Y-m-d", strtotime("-1 day", strtotime($while_date)))] != 'null' && $tab_vol_6[$while_date] != 'null')
	{
		$vol = ($tab_vol_6[$while_date]-$tab_vol_22[date("Y-m-d", strtotime("-1 day", strtotime($while_date)))]);
	// recherche mini et Maxi calcul total
		if ($vol<$volmin)  $volmin = $vol; 
		if ($vol>$volmax)  $volmax = $vol;
		$voltot = $voltot + $vol;
		
		$s_vol_6 = $s_vol_6.$vol.',';
	}
	else $s_vol_6 = $s_vol_6.'null,';
	$while_date = date("Y-m-d", strtotime("+1 day", strtotime($while_date)));
}

$while_date = $date1;
for($i = 0; $i < count($tab_vol_22); $i++)
{
	if($tab_vol_22[$while_date] != 'null' && $tab_vol_6[$while_date] != 'null')
	{
		$vol = ($tab_vol_22[$while_date]-$tab_vol_6[$while_date]);
	// recherche mini et Maxi calcul total
		if ($vol<$volmin)  $volmin = $vol; 
		if ($vol>$volmax)  $volmax = $vol;
		$voltot = $voltot + $vol;
		
		$s_vol_22 = $s_vol_22.$vol.',';
	}
	else $s_vol_22 = $s_vol_22.'null,';
	$while_date = date("Y-m-d", strtotime("+1 day", strtotime($while_date)));
}

// calcul du nombre de jours sur l'intervalle
	$TDfin = strtotime($date2); // conversion timestamp
	$TDDeb = strtotime($date1);
	$Nbjours = round(($TDfin-$TDDeb)/(60*60*24)); //division des secondes pour avoir les jours
	//$Nbjours = 31;
	// Affichage des données bilan sur la période;
	$vmoy=$voltot/($Nbjours); // moyenne par jour

?>

<form method="get">Du <input name="du" type="text" value="<?php echo $date1;?>" onclick="ds_sh(this);"> au <input name="au" type="text" value="<?php echo $date2;?>" onclick="ds_sh(this);"><input type="submit" value="Afficher le graphique"><em style="font-size:12px;">(format ex.: 2014-05-27)</em></form>

<script type="text/javascript">
$(function () {
        $('#container').highcharts({
			chart: {
				type: 'column',
            	zoomType: 'x'
			},
			title: {
                text: 'Volumes pompés du château d\'eau du <?php echo "<b>".date("d/m/Y", strtotime($date1))."</b> au <b>".date("d/m/Y", strtotime($date2))."</b>";?>',
                x: -20 //center
            },
            subtitle: {
						<?php
                echo " text: 'Volume pompé ";
				if($voltot == 0) $volmin = 0;
				echo " --- V total = <b>".$voltot."</b> m³ -- Vmoy/jour = <b>".round($vmoy, 2)."</b> m³ -- Vmin = <b>".$volmin."</b> m³ -- VMax = <b>".$volmax."</b> m³'";
                        ?>

                
            },
            xAxis: {
				labels: {
					rotation: -60
				},
				categories: [<?php echo $s_date;?>]
            },
            yAxis: {
                title: {
                    text: 'Volume (m3)'
                },
                plotLines: [{
                    value: 60,
                    width: 2,
                    color: '#F00',
					dashStyle: 'longdash'
                }]
            },
            tooltip: {
                valueSuffix: 'm3'
            },
            legend: {
            layout: 'vertical',
            backgroundColor: '#FFFFFF',
            align: 'center',
            verticalAlign: 'bottom'
			},
			
			plotOptions: {
                column: {
                    pointPadding: 0.2,
                    borderWidth: 0
            },
            series: {
                     borderWidth: 0,
                     dataLabels: {
                         enabled: true,
                         format: '{point.y}',
                         color: 'blue'
						}
                    }
            },
			
            series: [{
                name: 'Volume pompé entre 22hoo-06hoo',
                data: [<?php echo $s_vol_6;?>]
            }, {
                name: 'Volume pompé entre 06hoo-22hoo',
                data: [<?php echo $s_vol_22;?>]
            }]
        });
    });
</script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<?php
}
else echo 'Vous n\'êtes pas autorisé/connecté!';
include('foot.php'); ?>