<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "fkid") 
    $lo_id = $paramValue;  
  else if ($paramName == "id") 
    $lc_id = $paramValue;  
  else if ($paramName == "composition_id") 
    $composition_id = $paramValue;  
  else if ($paramName == "munselcolour_id") 
    $munselcolour_id = $paramValue;  
  else if ($paramName == "compaction_id") 
    $compaction_id = $paramValue;  
  else if ($paramName == "texture_id") 
    $texture_id = $paramValue;  
  else if ($paramName == "sphericity_id") 
    $sphericity_id = $paramValue;  
  else if ($paramName == "rounding_id") 
    $rounding_id = $paramValue;  
  else if ($paramName == "note") 
    $note = $paramValue;  
}


$dal = new DAL();

if ($lc_id == 0) { 

  $lc_id = hexdec(uniqid());
    
  $sql = "INSERT INTO lotcomposition " .    
         "(id, lot_id, composition_id, munselcolour_id, compaction_id, note) " .
         "VALUES ($lc_id, $lo_id, $composition_id, $munselcolour_id, $compaction_id, '$note') ";
  $result = $dal->query($sql);

  if ($composition_id == 1) {

  $se_id = hexdec(uniqid());

    $sql = "INSERT INTO sediment     " .    
           "(id, lotcomposition_id, texture_id, sphericity_id, rounding_id) " .
           "VALUES ($se_id, $lc_id, $texture_id, $sphericity_id, $rounding_id) ";
    $result = $dal->query($sql);
  }
}

else {
  $sql = "UPDATE lotcomposition SET composition_id=$composition_id, " .  
         "munselcolour_id=$munselcolour_id, " . 
         "compaction_id=$compaction_id, note='$note' " .
         "WHERE id = $lc_id " ;
  $result = $dal->query($sql);

  if ($composition_id == 1) { 
    $sql = "UPDATE sediment SET " .
           "texture_id=$texture_id, sphericity_id=$sphericity_id, " .
           "rounding_id=$rounding_id " .
           "WHERE lotcomposition_id = $lc_id " ;
    $result = $dal->query($sql);
  }
    
  else {
    $sql = "DELETE FROM sediment WHERE lotcomposition_id = $lc_id " ;
    $result = $dal->query($sql);
  }    
}

echo json_encode($lc_id);

?>

