<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');
 
foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "id") 
    $os_id = $paramValue; 
  else if ($paramName == "fkid") 
    $pa_id = $paramValue; 
  else if ($paramName == "mapareaid") 
    $mapareaid = $paramValue; 
  else if ($paramName == "location") 
    $location = $paramValue; 
  else if ($paramName == "groundcovertype_id") 
    $groundcovertype_id = $paramValue; 
  else if ($paramName == "groundconditiontype_id") 
    $groundconditiontype_id = $paramValue; 
  else if ($paramName == "groundvisibility_id") 
    $groundvisibility_id = $paramValue; 
  else if ($paramName == "assesstype_id") 
    $assesstype_id = $paramValue; 
  else if ($paramName == "modification") 
    $modification = $paramValue; 
  else if ($paramName == "rationale") 
    $rationale = $paramValue; 
  else if ($paramName == "mapcode") 
    $mapcode = $paramValue; 
  else if ($paramName == "note") 
    $note = $paramValue; 
}

$dal = new DAL();

$sql = "SELECT id FROM assesstrategy " .
       "WHERE operationspatialunit_id = $os_id LIMIT 1 ";
$result = $dal->query($sql);

if (count($result) == 0) {
    
  $at_id = hexdec(uniqid());

  $sql = "INSERT INTO assesstrategy (id, operationspatialunit_id, " .
         "assesstype_id, modification, rationale) " .
         "VALUES ($at_id, $os_id, $assesstype_id, '$modification', " .
         "'$rationale') ";
  $result = $dal->query($sql);
  
  $uc_id = hexdec(uniqid());

  $sql = "INSERT INTO unitcondition (id, operationspatialunit_id, " .
         "groundcovertype_id, groundconditiontype_id, groundvisibility_id) " .
         "VALUES ($uc_id, $os_id, $groundcovertype_id, " .
         "$groundconditiontype_id, $groundvisibility_id) ";
  $result = $dal->query($sql);
}

else {

  $sql = "UPDATE assesstrategy SET assesstype_id=$assesstype_id, " .
         "modification='$modification', rationale='$rationale' " .
         "WHERE operationspatialunit_id = $os_id ";
  $result = $dal->query($sql);
  
  $sql = "UPDATE unitcondition SET groundcovertype_id=$groundcovertype_id, " .     "groundconditiontype_id=$groundconditiontype_id, " .
         "groundvisibility_id=$groundvisibility_id " .
         "WHERE operationspatialunit_id = $os_id ";
  $result = $dal->query($sql);
}

$sql = "UPDATE operationspatialunit SET note='$note' WHERE id = $os_id ";
$result = $dal->query($sql);


echo json_encode($os_id);

?>

