<?php
include "stuff.php";
$con = mysqli_connect($server,$user,$password,$db);
//$start = $_POST['start'];
//$end = $_POST['end'];
$eid = $_POST['eid'];
$mid = $_POST['mid'];
$etid = $_POST['etid'];
// $start = date_to_dmy($start);
// $end = date_to_dmy($end);

mysqli_query($con,"INSERT INTO results(model) VALUES('{$mid}')");

$res = mysqli_query($con,"SELECT MAX(id) FROM results");
$row = mysqli_fetch_array($res);
$rid = $row[0];

$res = mysqli_query($con,"SELECT preset FROM models WHERE id={$mid}");
$row = mysqli_fetch_array($res);
$pid = $row['preset'];

$res = mysqli_query($con,"SELECT name,is_custom FROM presets WHERE id={$pid}");
$row = mysqli_fetch_array($res);
$name = $row['name'];
// $etid = $row['estate_type'];
$is_custom = $row['is_custom'];
if ($is_custom==1) {$customised = true;}
else {$customised = false;}

date_default_timezone_set("Europe/London");
$dt = date("d/m/Y h:i:s",time());

if ($customised) { 
  if($etid == 4 || $etid == 5){   
    $space_heating = $_POST['space_heating'];
    $lighting = $_POST['lighting'];
    $equipment = $_POST['equipment'];
    $DHW = $_POST['DHW'];
    $CO2 = $_POST['CO2'];
    $NOX = $_POST['NOX'];
    $SOX = $_POST['SOX'];
    $O3  = $_POST['O3'];
    $max_Top_liv   = $_POST['max_Top_liv'];
    $min_Top_liv   = $_POST['min_Top_liv'];
    $max_Top_kit   = $_POST['max_Top_kit'];
    $min_Top_kit   = $_POST['min_Top_kit'];
    $max_Top_bed   = $_POST['max_Top_bed'];
    $min_Top_bed   = $_POST['min_Top_bed'];
    $max_Top_bath  = $_POST['max_Top_bath'];
    $min_Top_bath  = $_POST['min_Top_bath'];
    $max_Top_WC    = $_POST['max_Top_WC'];
    $min_Top_WC    = $_POST['min_Top_WC'];
    $max_Top_hall  = $_POST['max_Top_hall'];
    $min_Top_hall  = $_POST['min_Top_hall'];
    $min_fresh_air = $_POST['min_fresh_air'];
    $air_supply_units= $_POST['air_supply_units_fwd'];
    $max_CO2_conc  = $_POST['max_CO2_conc'];
    $name = $_POST['assmnt_name'];
    mysqli_query($con,"INSERT INTO model_inputs(model,result,in01,in02,in03,in04,in05,in06,in07,in08,in09,in10,in11,in12,in13,in14,in15,in16,in17,in18,in19,in20,in21,in22,in23)
                      VALUES (   {$mid},{$rid},
                                '{$space_heating}',
                                '{$lighting}',
                                '{$equipment}',
                                '{$DHW}',
                                '{$CO2}',
                                '{$NOX}',
                                '{$SOX}',
                                '{$O3}',
                                '{$max_Top_liv}',
                                '{$min_Top_liv}',
                                '{$max_Top_kit}',
                                '{$min_Top_kit}',
                                '{$max_Top_bed}',
                                '{$min_Top_bed}',
                                '{$max_Top_bath}',
                                '{$min_Top_bath}',
                                '{$max_Top_WC}',
                                '{$min_Top_WC}',
                                '{$max_Top_hall}',
                                '{$min_Top_hall}',
                                '{$min_fresh_air}',
                                '{$air_supply_units}',
                                '{$max_CO2_conc}')");
  }
}

mysqli_query($con,"UPDATE results SET name='{$name}', preset={$pid}, requested_at='{$dt}' WHERE id = {$rid}");

header("location: ../assmnt_req.php?mid={$mid}");

// convert YYYY-mm-dd to dd/mm/YYYY expects 10 char string input (& output)
function date_to_dmy($input){
  return substr($input,8,2) . "/" . substr($input,5,2) . "/" . substr($input,0,4);
}
?>
