<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');
 
foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "scopeid") 
    $lw_id = $paramValue; 
  else if ($paramName == "id") 
    $we_id = $paramValue; 
  else if ($paramName == "starttime") 
    $starttime = $paramValue; 
  else if ($paramName == "cloudcover_id") 
    $cloudcover_id = $paramValue; 
  else if ($paramName == "atmosphere_id") 
    $atmosphere_id = $paramValue; 
  else if ($paramName == "temperature") 
    $temperature = $paramValue; 
  else if ($paramName == "humidity") 
    $humidity = $paramValue; 
  else if ($paramName == "lighting_id") 
    $lighting_id = $paramValue; 
  else if ($paramName == "precipitation_id") 
    $precipitation_id = $paramValue; 
}

$dal = new DAL();

if ($we_id == 0) {

  $we_id = hexdec(uniqid());

  $sql = "INSERT INTO weather " .
         "(id, loginworkday_id, starttime, cloudcover_id, atmosphere_id, " .
         "temperature, humidity, lighting_id, precipitation_id) " .
         "VALUES " .
         "($we_id, $lw_id, '$starttime', $cloudcover_id, $atmosphere_id, " .
         "$temperature, $humidity, $lighting_id, $precipitation_id) ";
  $result = $dal->query($sql);

}

else {
  $sql = "UPDATE weather SET " .
         "start_time='$start_time', cloudcover_id=$cloudcover_id,  " .
         "atmosphere_id=$atmosphere_id, temperature=$temperature, " .
         "humidity=$humidity, lighting_id=$lighting_id, " .
         "precipitation_id=$precipitation_id " .
         "WHERE id = $we_id ";
  $result = $dal->query($sql);
}  

echo json_encode($we_id);

?>

