<?php

if ($_POST['tyden']=="")
{
$tyden=date("W");
$rok=date("Y");
}
else 
{
$tyden=$_POST['tyden'];
$rok=$_POST['rok'];
}




$obsah='<form action="?p=tydenni" method="post">Pro týden: 
             <input type="text" size="5" name="tyden" value="'.$tyden.'">
              v roce 
             <input type="text" size="5" name="rok" value="'.$rok.'"> 
             <input type="submit" value="Zobrazit"></form>';
     
     if ($tyden<10) {$tyden="0".$tyden;} else  {$tyden=$tyden;}
     $cas=strtotime($rok."W".$tyden."1");
     $dokdy=strtotime($rok."W".($tyden+1)."1");
     
     //$obsah.="<br>".date("j.n.Y",$cas); 
     //$obsah.=" - ".date("j.n.Y",$dokdy); 
     
     $obsah.='<br/><br/><table border="0">';
     
     $dotaz=mysql_query("SELECT DISTINCT devices.id,devices.name FROM devices,data WHERE devices.id=data.device AND data.time>=".$cas." AND data.time<".($dokdy)." ORDER BY devices.id");
     $pocet=mysql_num_rows($dotaz);
     
         
     for($i=0;$i<$pocet;$i++)
     {
     $snimac=mysql_fetch_row($dotaz);
     $obsah.='<tr><td>';
     $obsah.='<a href="detail.php?id='.$snimac[0].'"><h3>'.$snimac[1].'</h3></a>';
     
     
     $dotaz_maxdata=mysql_query("SELECT data FROM data WHERE device=".$snimac[0]." AND time>=".$cas." AND time<".$dokdy." ORDER BY data DESC LIMIT 1");
     $maxdata=mysql_fetch_row($dotaz_maxdata);
     
     $obsah.='<table border="0">';
     $obsah.='<tr><td>'.round($maxdata[0]).' kWh</td>';
     
     $aktcas=$cas;
     for ($y=0;$y<7;$y++)
     {
     $dotaz_hodnota=mysql_query("SELECT avg(data) FROM data WHERE device=".$snimac[0]." AND time>=".$aktcas." AND time<".($aktcas+86400));
     $hodnota=mysql_fetch_row($dotaz_hodnota);
     $barva=($i % 5)+1;

     //$obsah.='<td rowspan="10" style="text-align:center; vertical-align:bottom; height:200px; background-image: url(\'images/table_back.png\'); z-index:-3; height:100%; background-attachment:move; background-repeat: repeat-x;"><img src="images/'.$barva.'.png" width="15" height="'.nastavdelku(round($maxdata[0]),round($hodnota[0],1),200).'" alt="'.round($hodnota[0],1).'"></td>';
     $obsah.='<td rowspan="10" style="text-align:center; vertical-align:bottom; height:200px; "><img src="images/'.$barva.'.png" width="15" height="'.nastavdelku(round($maxdata[0]),round($hodnota[0],1),200).'" title="'.round($hodnota[0],1).'"></td>';
     $aktcas+=86400;                                                                                                              
     }
     
     $obsah.='</tr>';
     $obsah.='<tr><td style="text-align:right;">'.round($maxdata[0]*0.9).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.round($maxdata[0]*0.8).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.round($maxdata[0]*0.7).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.round($maxdata[0]*0.6).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.round($maxdata[0]*0.5).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.round($maxdata[0]*0.4).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.round($maxdata[0]*0.3).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.round($maxdata[0]*0.2).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.round($maxdata[0]*0.1).' kWh</td></tr>';
     $obsah.='<tr><td></td><td>Po</td><td>Út</td><td>St</td><td>Čt</td>
                           <td>Pá</td><td>So</td><td>Ne</td>
     </tr>';
     
     $obsah.='</tr></td></table>';
     
     $obsah.='</td></tr>';
     
     }
     
     
     $obsah.='</table>';
    

?>
