<?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 lot " .
       "WHERE projectparcel_id = $pp_id " .
       "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 = $next_seq;  

$lo_id = hexdec(uniqid());

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

echo json_encode($lo_id);

?>

