<?php include './include/header.php' ?>

  <!-- Jumbotron -->
  
  <div class="span12 jumbotron">
    <h2>Suivez votre consommation en temps réel !</h2>
    <p class="lead">Bienvenue sur l'interface de votre ZedBox. Cette interface est destinée au pilotage des radiateurs de votre maison. Ajoutez vos modules Zed_modules par le panneau de configuration. </br> Si c'est votre première navigation, lancez le Wizard.</p>

    <a class="btn btn-large btn-primary" href="#">Wizard - Config.</a>
  </div>

<div class="row-fluid">
  <div id="forcast" class="span4 acceuil">
      <label for="forcast">
        <h5 class="page-header">Prévisions Météo</h5>
      </label>
      <?php
        $requete= "SELECT m_temperature,DATE_FORMAT(m_time,'%H'), DATE_FORMAT(NOW(),'%i') FROM METEO WHERE HOUR(m_time) = HOUR(NOW());";
        $resultat = mysqli_query($link, $requete) or die ("Erreur SQL météo :".mysqli_error());
        $num = mysqli_num_rows($resultat);
        $i = 0;
        while ($i < $num){
          mysqli_data_seek($resultat,$i);
          $row = mysqli_fetch_row($resultat);
          echo "<h4> Prévision : ".$row[0]."°C à ".$row[1]."h".$row[2]." </h4>";
          $i++;
        }
      ?>
      <div class="row-fluid">
        </br>
      </br>
      </div>
  </div>

  <div id="temp_in_room" class="span4 acceuil">
    <form align="center" method="POST">
      <label for="bat_temp">
        <h5 class="page-header">Quelle température dans cette pièce ?</h5>
      </label>

      <select class="control-group info" name="Batiment_temp" id="Batiment_temp" onChange="save_bat(this,'temp')">

        <option value="Default">Sélectionnez un batiment</option>

          <!-- Requête SQL -->

          <?php
            $query= "SELECT * FROM View_temp_piece";
            $result = mysqli_query($link, $query) or die("Erreur SQL".mysqli_error());
            $num = mysqli_num_rows($result);
            $i = 0;
            while ($i < $num){
              mysqli_data_seek($result,$i);
              $row = mysqli_fetch_row($result);
              $selection='';
              if (isset($_POST['Batiment_temp']) && ($_POST['Batiment_temp']==$row[0])){
                $selection="selected";
              };
              echo "<option value=".$row[0]." ".$selection.">".$row[0]."</option>";
              $i++;
            }
          ?>

      </select>
      <select name="room_temp" id="room_temp" disabled="true" onChange="submit()">
        <option value="Default">Sélectionnez une pièce</option>

        <?php

        $query2= "SELECT * FROM View_temp_piece";
        $result2 = mysqli_query($link, $query2) or die("Erreur SQL".mysqli_error());
        $num2 = mysqli_num_rows($result2);
        $i = 0;
        while ($i < $num2){
          mysqli_data_seek($result2,$i);
          $row2 = mysqli_fetch_row($result2);
          echo "<option id='bat_temp".$row2[1]."' value=".$row2[1].">".$row2[1]."</option>";
          $i++;
        }

        ?>

      </select>
      <div class="span8 offset2">
        <div id="success_temp" class="alert alert-success">
        <a href="#success_temp" class="close">x</a>
        <?php
        $i = 0;
        while ($i < $num){
          mysqli_data_seek($result,$i);
          $row = mysqli_fetch_row($result);
          if ($_POST['Batiment_temp']==$row[0] & $_POST['room_temp']==$row[1]) {
            echo "<script type='text/javascript'>success('temp')</script>
            <p>Température de ".$row[1]." en ".$row[0]." : <strong>".number_format($row[2], 2)." °C</strong></p>";
          }
          $i++;
        }

        mysqli_free_result($result);
        ?>
        </div>
      </div>
    </form>
  </div>
  <div id="radiator_status" class="span4 acceuil">
    <div>
      <h5 class="page-header">Mode manuel Radiateur</h5>

      <form method="POST" style="text-align:center">

        <label class="radio">
          <input type="radio" name="Radio_Radiateur" id="Rad_Check" value="check" checked>
          Uniquement vérifier le status
        </label>

        <label class="radio">
          <input type="radio" name="Radio_Radiateur" id="Rad_Change" value="change">
          Voir le status, puis le modifier
        </label> 

        <label for="bat_rad"></label>
        <select name="Batiment_rad" id="Batiment_rad" onChange="save_bat(this,'rad')">
          <option value="Default">Sélectionnez un batiment</option>
          <?php
            $query= "SELECT * FROM View_Rad_Room";
            $result = mysqli_query($link, $query) or die("Erreur SQL".mysqli_error());
            $num = mysqli_num_rows($result);
            $i = 0;
            while ($i < $num){
              mysqli_data_seek($result,$i);
              $row = mysqli_fetch_row($result);
              echo "<option value=".$row[0].">".$row[0]."</option>";
              $i++;
            }
          ?>
        </select>
        <select name="room_rad" id="room_rad" disabled="true" onChange="submit()">
          <option value="Default">Sélectionnez une pièce</option>
          <?php
            $query2= "SELECT * FROM View_Rad_Room";
            $result2 = mysqli_query($link, $query2) or die("Erreur SQL".mysqli_error());
            $num2 = mysqli_num_rows($result2);
            $i = 0;
            while ($i < $num2){
              mysqli_data_seek($result2,$i);
              $row2 = mysqli_fetch_row($result2);
              echo "<option id='bat_rad".$row2[0]."' value=".$row2[1].">".$row2[1]."</option>";
              $i++;
            }
          ?>
        </select>

        <div class="span8 offset2">
        <div id="success_rad" class="alert alert-success">
          <a href="#success_rad" class="close">x</a>
          <?php

            if (isset($_POST['room_rad'])){
              $i = 0;
              while ($i < $num){
                mysqli_data_seek($result,$i);
                $row = mysqli_fetch_row($result);
                if ($_POST['Batiment_rad']==$row[0] & $_POST['room_rad']==$row[1]) {
                  echo "<p>Les radiateurs en ".$row[1]." en ".$row[0]." sont en <strong>".$row[2]."</strong> </br>La pièce est paramétrée sur <strong>".$row[3]."</strong></p>";
                }
                $i++;
              }
              mysqli_free_result($result);
            }

          ?>
         </div>
        </div>
      </form>

      <div class="span10">
        <form id='change_rad' style="display:none" method="POST">

          <select name="bat_selected" id="bat_selected" style="display:none">
            <?php echo "<option value='".$_POST['Batiment_rad']."'>".$_POST['Batiment_rad']."</option>" ?>
          </select>

          <select name="room_selected" id="room_selected" style="display:none">
            <?php echo "<option value='".$_POST['room_rad']."'>".$_POST['room_rad']."</option>" ?>
          </select>

          <label for="rad_mode">Quel mode ?</label>
          <select name="mode_rad" id="mode_rad">
            <option value="Default">Sélectionnez une pièce</option>
            <option value="hg">Hors-Gel</option>
            <option value="less">Réduit</option>
            <option value="confort">Confort</option>
            <option value="stopped">Arreté</option>
          </select>
          <p></br></p>
            <button class="btn btn-primary" type="submit()">Go!</button>
          <p></br></p>
        </form>
      </div>

      <div class="span8 offset2">
        <div id="success_change_rad" class="alert alert-success">
          <a href="#success_change_rad" class="close">x</a>
          <p>Opération réussie</p>
        </div>
      </div>

    </div>
  
    <?php
      if (isset($_POST['room_rad'])){
        $query= "SELECT * FROM View_Rad_Room";
        $result = mysqli_query($link, $query) or die("Erreur SQL".mysqli_error());
        $i = 0;
        while ($i < $num){
          mysqli_data_seek($result,$i);
          $row = mysqli_fetch_row($result);
          if ($_POST['Batiment_rad']==$row[0] & $_POST['room_rad']==$row[1]) {
            echo "<script type='text/javascript'>success('rad')</script>";
            if ($_POST['Radio_Radiateur']=='change') {
              echo "<script type='text/javascript'>document.getElementById('change_rad').style.display='block'; </script>";
            }
          }
          $i++;
        }
        mysqli_free_result($result);
      }

      if (isset($_POST['mode_rad']) && $mod != "Default") {
        $mod=$_POST['mode_rad'];
        $ba=$_POST['bat_selected'];
        $room=$_POST['room_selected'];
        $query3="UPDATE PIECE SET p_mode='".$mod."' WHERE p_batiment='".$ba."' AND p_piece='".$room."'";
        $result3 = mysqli_query($link, $query3);
        echo "<script type='text/javascript'>success('change_rad')</script>";
        mysqli_free_result($result3);
      }
    ?>
  </div>
</div>

<div class="row-fluid">
  </br>
  </br>
</div>
<div class="row-fluid">
  </br>
  </br>
</div>
<?php include './include/footer.php' ?>