<?php


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


if (strlen($_POST['typ'])>0) 
  {

     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='<form action="?p=mesicni" method="post">Pro měsíc: 
             <input type="text" size="5" name="mesic" value="'.$mesic.'">
              v roce 
             <input type="text" size="5" name="rok" value="'.$rok.'"> 
             <input type="submit" value="Zobrazit"></form>';
*/             
             
     $cas=strtotime("1.".$mesic.".".$rok);
     
     //$obsah.=$cas;       //sekundy pulnoci, kazda hodina + 3600, kazdy den + 86400
     //$obsah.="<br>".date("j.n.Y",$cas);      
   
     if ($_POST['mesic']>=12) 
     {  $dokdy=strtotime("1.1.".($rok+1));  } 
     else
     {  $dokdy=strtotime("1.".($mesic+1).".".$rok);   }
     

     //$obsah.="<br>".date("j.n.Y",$dokdy);      
   
   
     $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],1).' kWh</td>';
     
     $aktcas=$cas;
     for ($y=1;$y<=31;$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; ">
     <a href="prehled.php?p=denni&d='.$y.'&m='.$mesic.'&r='.$rok.'">
     <img src="images/'.$barva.'.png" width="15" height="'.nastavdelku(round($maxdata[0]),round($hodnota[0],1),200).'" title="'.round($hodnota[0],2).' kWh">
     </a>
     </td>';
     $aktcas+=86400;                                                                                                              
     }
     
     $obsah.='</tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.9,1).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.8,1).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.7,1).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.6,1).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.5,1).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.4,1).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.3,1).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.2,1).' kWh</td></tr>';
     $obsah.='<tr><td style="text-align:right;">'.number_format($maxdata[0]*0.1,1).' kWh</td></tr>';
     $obsah.='<tr><td style="background:url(\'images/popis_mesic.png\') no-repeat center"></td><td>1</td><td>2</td><td>3</td>
                           <td>4</td><td>5</td><td>6</td><td>7</td>
                           <td>8</td><td>9</td><td>10</td><td>11</td>
                           <td>12</td><td>13</td><td>14</td><td>15</td>
                           <td>16</td><td>17</td><td>18</td><td>19</td>
                           <td>20</td><td>21</td><td>22</td><td>23</td>
                           <td>24</td><td>25</td><td>26</td><td>27</td>
                           <td>28</td><td>29</td><td>30</td><td>31</td>
     </tr>';
     
     $obsah.='</tr></td></table>';
     
     $obsah.='</td></tr>';
     
     }
     
     
     $obsah.='</table>';
  

?>
