<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "scopeid") 
    $pwo_id = $paramValue;  
  else if ($paramName == "id") 
    $lo_id = $paramValue;  
  else if ($paramName == "cu_id") 
    $cu_id = $paramValue;  
  else if ($paramName == "currid") 
    $os_id = $paramValue;  
  else if ($paramName == "selectlot_name") 
    $lot_name_id = $paramValue;  
  else if ($paramName == "depth") 
    $depth = $paramValue;  
  else if ($paramName == "evel_pos") 
    $pos = $paramValue;  
  else if ($paramName == "evel_mark") 
    $mark = $paramValue;  
}

$dal = new DAL();

if ($sl_id == 0) {

  $sl_id = hexdec(uniqid());

  $sql = "INSERT INTO spatialunitlot " .
         "(id, lot_id, operationspatialunit_id) " .
         "VALUES ($sl_id, $lo_id, $os_id) "; 
  $result = $dal->query($sql);

  $po_id = hexdec(uniqid());

  $sql = "INSERT INTO points " .
         "(id, pos) VALUES ($po_id, $pos) "; 
  $result = $dal->query($sql);

  $slp_id = hexdec(uniqid());

  $sql = "INSERT INTO spatialunitlotpoints " .
         "(id, spatialunitlot_id, points_id) " .
         "VALUES ($slp_id, $sl_id, $po_id) "; 
  $result = $dal->query($sql);

  $el_id = hexdec(uniqid());

  $sql = "INSERT INTO elevation " .
         "(id, spatialunitlotpoints_id) " .
         "VALUES ($el_id, $slp_id) "; 
  $result = $dal->query($sql);
  
  $de_id = hexdec(uniqid());

  $sql = "INSERT INTO depth " .
         "(id, spatialunitlot_id) " .
         "VALUES ($de_id, $sl_id) "; 
  $result = $dal->query($sql);
  
  $lc_id = hexdec(uniqid());

  $sql = "INSERT INTO lotculturalunit " .
         "(id, spatialunitlot_id, culturalunit_id) " .
         "VALUES ($lc_id, $sl_id, $cu_id) "; 
  $result = $dal->query($sql);
}

else { 
 
  $sql = "UPDATE depth SET depth = $depth " .
         "WHERE spatialunitlot_id = $sl_id " ;
  $result = $dal->query($sql);

  $sql = "UPDATE elevation SET mark='$mark' " .
         "WHERE spatialunitlot_id = " . 
         "SELECT spatialunitlotpoints_id FROM spatialunitlotpoints " .
         "WHERE spatialunitlot_id = $sl_id " ;
  $result = $dal->query($sql);

  $sql = "UPDATE spatialunitlotpoints  SET mark='$mark' " .
         "WHERE spatialunitlot_id = $sl_id " ;
  $result = $dal->query($sql);

  $sql = "UPDATE points  SET pos='$pos' WHERE id =  " .
         "SELECT points_id FROM spatialunitlotpoints " .
         "WHERE spatialunitlot_id = $sl_id " ;
  $result = $dal->query($sql);
}
    
echo json_encode($result);

?>

