<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "id") 
    $lo_id = $paramValue;  
  else if ($paramName == "projectparcel_fk") 
    $pp_id = $paramValue;  
}

$dal = new DAL();

$sql = "SELECT name_seq " .
       "FROM spatialunit " .
       "WHERE projectparcel_id = $pp_id AND spatialunitype_id = 5 " .
       "ORDER BY name_seq DESC LIMIT 1 " ;
$result = $dal->query($sql);  

if (null == $result)
  $next_seq = 1; 
else
  $next_seq = $result->name_seq + 1;
  
$name = "PTP $next_seq";

$su_id = hexdec(uniqid());

$sql = "INSERT INTO spatialunit " .
       "(id, projectparcel_id, name, name_seq) " .
       "VALUES ($su_id, $pp_id, '$name', $next_seq) " ;
$result = $dal->query($sql);

echo json_encode($lo_id);

?>

