<?php

if ($_GET['d']!="") 
 {$aktdatum=$_GET['d'].".".$_GET['m'].".".$_GET['r'].", ".$_GET['h'].":00:00"; 
  $hodina=$_GET['h'];
  $obsah.='<h3>Pro den: '.$aktdatum.', hodina: '.$hodina.':00 - '.$hodina.':59</h3>';  
 }
elseif($_POST['datum']!="") { 
  $aktdatum=$_POST['datum']; 
   $obsah.='<h3>Pro den: '.$aktdatum.', hodina: '.$hodina.':00 - '.$hodina.':59</h3>';
  }
     


if (strlen($_POST['typ'])>0) 
  {
     $aktdatum=$den.", ".$hodina.":00:00";
     $den=date("j",strtotime($aktdatum));
     $mesic=date("n",strtotime($aktdatum));
     $rok=date("Y",strtotime($aktdatum));

     if ($osoba=="neuvedeno") {$osoba="";}
     if ($mistnost=="neuvedeno") {$mistnost="";}
     if ($osoba!="kdokoli") {$podm_osoba="devices.person='".htmlspecialchars_decode($osoba)."' AND ";}     
     if ($mistnost!="jakakoli") {$podm_mistnost="devices.room='".$mistnost."' AND ";}  
     //if (strlen($min)>0) {$podm_min='min(data.data)>'.$min.' AND ';}   
     //if (strlen($max)>0) {$podm_max='max(data.data)<'.$max.' AND ';}
  }


     //$obsah='Pro den: '.$aktdatum;
     
     $cas=strtotime($aktdatum);
     //$obsah.=$cas;       //sekundy pulnoci, kazda hodina + 3600
     $dokdy=$cas+3600;
   
     $obsah.='<br/><br/><table border="0">';
     
     $dotaz=mysql_query("SELECT DISTINCT devices.id,devices.name,devices.type FROM devices,data WHERE ".$podm_osoba.$podm_mistnost.$podm_min.$podm_max." devices.id=data.device AND data.time>=".$cas." AND data.time<".($dokdy)." ORDER BY devices.id");
     $pocet=mysql_num_rows($dotaz);
     
     if ($pocet==0) {$obsah.='<span style="font-weight:bold;color:red;">Vyhledávání nevyhovují žádná naměřená data.</span>';}
         
     for($i=0;$i<$pocet;$i++)
     {
     $snimac=mysql_fetch_row($dotaz);
     $dotaz1=mysql_query("SELECT sensors.mac,devices.room,devices.person FROM devices,sensors WHERE devices.sensor=sensors.id AND devices.id=".$snimac[0]);
     $detail=mysql_fetch_row($dotaz1);                                  
     
     $obsah.='<tr><td>';
     if (strlen($snimac[2])>0) {$det='('.$snimac[2].')';} else {$det='';}
     $obsah.='<a href="detail.php?id='.$snimac[0].'"><h3>'.$snimac[1].' '.$det.'</h3></a>';
     
     if ($detail[0]==0) {$mac="Neasociováno";} else  {$mac="MAC: 0x".$detail[0];}
     $obsah.='<span style="color:#888888;">('.$mac.', místnost: '.$detail[1].', zodp.osoba: '.$detail[2].')</span><br/>';     
     
     
     $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 style="text-align:right;">'.number_format($maxdata[0],2).' kWh</td>';
     
     $aktcas=$cas;
     for ($y=0;$y<12;$y++)
     {
     $dotaz_hodnota=mysql_query("SELECT avg(data) FROM data WHERE device=".$snimac[0]." AND time>=".$aktcas." AND time<".($aktcas+300));
     $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="25" height="'.nastavdelku(round($maxdata[0]),round($hodnota[0],1),200).'" title="'.round($hodnota[0],2).' kWh"></td>';
     $aktcas+=300;                                                                                                              
     }
     
     $obsah.='</tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.9,2).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.8,2).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.7,2).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.6,2).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.5,2).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.4,2).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.3,2).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.2,2).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.1,2).' kWh</td></tr>';
     $d=$hodina;
     $obsah.='<tr><td style="background:url(\'images/popis_hodina.png\') no-repeat center"></td><td>'.$d.':00</td><td>'.$d.':05</td><td>'.$d.':10</td><td>'.$d.':15</td><td>'.$d.':20</td>
                           <td>'.$d.':25</td><td>'.$d.':30</td><td>'.$d.':35</td><td>'.$d.':40</td>
                           <td>'.$d.':45</td><td>'.$d.':50</td><td>'.$d.':55</td>
                          
     </tr>';
     
     $obsah.='</tr></td></table>';
     
     $obsah.='</td></tr>';
     
     }
     
     
     $obsah.='</table>';
  

?>
