<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "spatialunitlot_id") 
    $sl_id = $paramValue;  
  else if ($paramName == "id") 
    $ba_id = $paramValue;  
  else if ($paramName == "refno") 
    $refno = $paramValue;  
  else if ($paramName == "title") 
    $title = $paramValue;  
}

$dal = new DAL();

if ($ba_id == 0) {

  $ba_id = hexdec(uniqid());

  $sql = "INSERT INTO bag " .
         "(id, spatialunitlot_id, refno, title) " .
         "VALUES ($ba_id, $sl_id, '$refno', '$title') "; 
  $result = $dal->query($sql);
}

else { 
 
  $sql = "UPDATE bag SET " .
         "refno='$refno', title='$title' " .
         "WHERE id = $ba_id " ;
  $result = $dal->query($sql);
}
    
echo json_encode($ba_id);

?>

