<html>
<head>
  <?php
  // include "Pages/stuff.php";
  // if (mysqli_connect_errno()){
  //   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  // }
  $mid=0;//$startcf=0;$endcf=0;
  if(isset($_GET['mid']))$mid=$_GET['mid'];
  //if(isset($_GET['start']))$startcf=$_GET['start'];
  //if(isset($_GET['end']))$endcf=$_GET['end'];
  ?>
  <link rel="stylesheet" type="text/css" href="CSS/styles.css">
  <script>
    var selpam = 0;
    function submitform(pam){
/*
      var start = document.getElementById("start-field").value;
      var stop  = document.getElementById("stop-field").value;
      if(start == null || start == ""){
        alert ("start date not filled");
        return;
      }
      if(stop == null || stop == ""){
        alert ("end date not filled");
        return;
      }
*/
      var mid = document.getElementById("mid").value;
      var eid = document.getElementById("eid").value;
      document.getElementById("form").submit();
    }
  </script>
</head>
<body style="background-color:white;">
<!-- ######## -->
<!--  HEADER  -->
<!-- ######## -->
<?php
  include "Pages/header.php";
  if($mid != 0){
    mysqli_query($con,"UPDATE users SET latest_model = {$mid} WHERE id = {$user_id}");
  }else{
    $res = mysqli_query($con,"SELECT latest_model FROM users WHERE id = {$user_id}");
    $row = mysqli_fetch_array($res);
    $mid = $row['latest_model'];
  }
?>
<!-- ########### -->
<!--  MAIN PAGE  -->
<!-- ########### -->
<div id="header-low">
  <?php
  $res = mysqli_query($con,"SELECT models.name,models.estate,estates.type
                            FROM models
                            INNER JOIN estates ON models.estate = estates.id
                            WHERE models.id={$mid}");
  while ($row = mysqli_fetch_array($res)){
    $model = $row[0];
    $eid = $row[1];
    $etid = $row[2];
  }
  $res = mysqli_query($con,"SELECT name FROM estates WHERE id={$eid}");
  while ($row = mysqli_fetch_array($res)){$name = $row['name'];}
  $res = mysqli_query($con,"SELECT type FROM estate_types WHERE id={$etid}");
  while ($row = mysqli_fetch_array($res)){$etyp = $row['type'];}
  echo("<p class='grey-text Xlarge-text'>Selection: {$name}: {$etyp}: {$model}</p><hr>");
  ?>
</div>
<div id="col-container">
<!-- LEFT PANEL --------->
  <div id="left-2col-sma-ht">
    <form action="Pages/select_preset.php" method="POST" id="form">
      <table>
        <tr>
          <td class='Xlarge-text grey-text'>Test criteria<hr></td>
          <td> </td>
          <td style='text-align:center'>
            <?php
              $res = mysqli_query($con,"SELECT preset FROM models WHERE id={$mid}");
              $row = mysqli_fetch_array($res);
              $pid = $row['preset'];
              $res = mysqli_query($con,"SELECT id,name FROM presets WHERE estate_type={$etid}");
              echo "<select name='preset' id='preset' onchange='this.form.submit()'>";
                echo "<option value='0' selected disabled hidden>please select one</option>";
                while ($row = mysqli_fetch_array($res)) {                  
                  echo "<option value='" . $row['id'] . "'";
                  if ($row['id']==$pid) {echo " selected";}
                  echo ">" . $row['name'] . "</option>";
                }
              echo "</select>";
            ?>
          </td>
        </tr>
      </table>
      <?php
        echo("<input type='hidden' id='mid'  name='mid' value={$mid}>");
      ?>
    </form>

    <form action="Pages/list_request.php" method="POST" id="form">
      <?php
        if($etid == 1){
          echo("<img src='Images/page-under-construction.png' height='50%'>");
        }elseif($etid == 2){
          include "Pages/office.php";
        }elseif($etid == 3){
          include "Pages/distHeat.php";
        }elseif($etid == 4){
          include "Pages/domestic.php";
        }elseif($etid == 5){
          include "Pages/domestic.php";
        }
        echo("<input type='hidden' id='mid'  name='mid' value={$mid}>");
        echo("<input type='hidden' id='eid'  name='eid' value={$eid}>");
        echo("<input type='hidden' id='etid'  name='etid' value={$etid}>");
      ?>
    </form>
  </div>

<!-- RIGHT PANEL ---------->
  <div id="right-2col-sma-ht">
    <table>
      <tr><td width='25%' class='Xlarge-text grey-text'>Requests<hr><td width='25%'></tr>
      <?php
        $res = mysqli_query($con,"SELECT result,name,id FROM results WHERE results.model = {$mid}");
        while ($row = mysqli_fetch_array($res)){
          $result = $row[0];
          $rname = $row[1];
          $rid = $row[2];
          echo("<tr class='large-text grey-text'><td>{$rname}</td><td>");
          if(!is_numeric($result))
            echo("<a href='Pages/submit.php?id={$rid}' class='no-underline'><input type='submit' style='cursor:pointer;' class='medium-text grey-text' value='submit'></a>");
          echo("</td></tr>");
        }
      ?>
      <tr class='large-text grey-text'>
        <td></td><td></td>
        <td><a href='dashboard.php' class='no-underline'><input type='submit' style='cursor:pointer;' class='medium-text grey-text' value='dashboard'></a></td>
      </tr>
    </table>
  </div>

</div>
<?php mysqli_close($con);?>
<!-- ######## -->
<!--  FOOTER  -->
<!-- ######## -->
<?php footer:?>
<div id="footer">
  <hr>
  <input action="action" onclick="window.history.go(-1); return false;"  style= "cursor:pointer; position:absolute" class="medium-text grey-text" type="button" value="Back" />
  <?php include "Pages/footer.php";?>
</div>
</body>
</html>
