<html>
<head>

<?php
  include "stuff.php";
  $con = mysqli_connect($server,$user,$password,$db);
  $rid = $_GET['rid'];

  $res = mysqli_query($con,"SELECT results.name,
                                   results.preset,
                                   presets.is_custom,
                                   models.name,
                                   estates.name,
                                   estates.type 
                            FROM results 
                            INNER JOIN presets ON results.preset=presets.id
                            INNER JOIN models ON results.model=models.id 
                            INNER JOIN estates ON models.estate=estates.id 
                            WHERE results.id={$rid}");
  $row = mysqli_fetch_array($res);
  $pname = $row[0];
  $pid = $row[1];
  $is_custom = $row[2];
  $mname = $row[3];
  $ename = $row[4];
  $etid = $row[5];
?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script>
window.onload = function(){

  var srid = <?php echo $rid; ?>;
  var spid = <?php echo $pid; ?>;
  var setid = <?php echo $etid; ?>;
  var sis_custom = <?php echo $is_custom; ?>;
  $.ajax({
    url: "../Scripts/check_request_inputs.php",
    type: "GET",
    data: { rid: srid, etid: setid, is_custom: sis_custom, pid: spid },
    dataType: "json",
    success: function(result){

      // Domestic.
      if (setid==4 || setid==5) {        
        set_box("space_heating",result.in01);
        set_box("lighting",result.in02);
        set_box("equipment",result.in03);
        set_box("DHW",result.in04);
        // Emissions.
        set_box("CO2",result.in05);
        set_box("NOX",result.in06);
        set_box("SOX",result.in07);
        set_box("O3",result.in08);
        // Thermal comfort.
        set_box("max_Top_liv",result.in09);
        set_box("min_Top_liv",result.in10);
        set_box("max_Top_kit",result.in11);
        set_box("min_Top_kit",result.in12);
        set_box("max_Top_bed",result.in13);
        set_box("min_Top_bed",result.in14);
        set_box("max_Top_bath",result.in15);
        set_box("min_Top_bath",result.in16);
        set_box("max_Top_WC",result.in17);
        set_box("min_Top_WC",result.in18);
        set_box("max_Top_hall",result.in19);
        set_box("min_Top_hall",result.in20);
        // Indoor air quality.      
        set_box("min_fresh_air",result.in21);
        document.getElementById("air_supply_units").value = result.in22;
        document.getElementById("air_supply_units").setAttribute("disabled",true);
        document.getElementById('air_supply_units').setAttribute('class','greyed-cell grey-text');
        // document.getElementById("air_supply_units_fwd").value = result.in22;
        set_box("max_CO2_conc",result.in23);
      }

      // Office.
      else if (setid==2) {
        // Energy.
        set_box("HVAC",result.in01);
        set_box("lighting",result.in02);
        set_box("equipment",result.in03);
        set_box("DHW",result.in04);
        // Emissions.
        set_box("CO2",result.in05);
        set_box("NOX",result.in06);
        set_box("SOX",result.in07);
        set_box("O3",result.in08);
        // Thermal comfort.
        set_box("max_Top",result.in09);
        set_box("min_Top",result.in10);
        // Indoor air quality.      
        set_box("min_fresh_air",result.in11);
        document.getElementById("air_supply_units").value = result.in12;
        document.getElementById("air_supply_units").setAttribute("disabled",true);
        document.getElementById('air_supply_units').setAttribute('class','greyed-cell grey-text');
        // document.getElementById("air_supply_units_fwd").value = result.in12;
        set_box("max_CO2_conc",result.in13);
      }

      // District heat.
      else if (setid==3) {        
        set_box("gen_efficiency",result.in01);
        set_box("net_loss",result.in02);
        set_box("load_temp",result.in03);
      }

    },
    error: function(jqXHR,errtyp,errstr){
      console.log('check_request_inputs failed');
      console.log(errtyp);
      console.log(errstr);
    }
  });
}
function set_box(id,val){  
  document.getElementById(id).value = val;
  document.getElementById(id).setAttribute("readonly", true);
  document.getElementById(id).setAttribute("class", "greyed-cell grey-text");
  // document.getElementById(id.concat("_a")).setAttribute("onClick","");
}
</script>
</head>

<body>
<?php echo ("<h4 style='color:gray;'>{$ename}: {$mname}: {$pname}</h4>");?>
<h5 style="color:gray">Data cannot be modified after resilience testing is requested.</h5>

<table style='color:gray;' id="the_table">
<?php
  if ($etid==4 || $etid==5) {
    include "domestic_table.html";
  }
  else if ($etid==2) {
    include "office_table.html";
  }
  else if ($etid==3) {
    include "distHeat_table.html";
  }
?>
</table>

</body>
</html>
