<?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 surpresseur 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';
	
	$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['volume'];
}

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

$while_date = $date1;
for($i = 0; $i < count($tab_vol_6); $i++)
{
	if(isset($tab_vol_6[date("Y-m-d", strtotime("-1 day", strtotime($while_date)))]) && $tab_vol_6[date("Y-m-d", strtotime("-1 day", strtotime($while_date)))] != 'null' && $tab_vol_6[$while_date] != 'null')
	{
		// recherche mini et Maxi calcul total
		$vol = ($tab_vol_6[$while_date]-$tab_vol_6[date("Y-m-d", strtotime("-1 day", strtotime($while_date)))]);
		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)));
}

	// 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 surpresseur 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: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                valueSuffix: 'm3'
            },
            legend: {
                layout: 'vertical',
                align: 'center',
                verticalAlign: 'bottom',
                borderWidth: 0
            },
            series: [{
                name: 'Volume pompé',
                data: [<?php echo $s_vol_6;?>]
            }]
        });
    });
</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'); ?>