<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "id") 
    $ab_id = $paramValue;  
  else if ($paramName == "fkid") 
    $ba_id = $paramValue;  
  else if ($paramName == "affiliation_id") 
    $affiliation_id = $paramValue;  
  else if ($paramName == "material_id") 
    $material_id = $paramValue;  
  else if ($paramName == "form_id") 
    $form_id = $paramValue;  
  else if ($paramName == "completeness_id") 
    $completeness_id = $paramValue;  
  else if ($paramName == "diagnostic") 
    $diagnostic = $paramValue;  
  else if ($paramName == "docucount") 
    $docucount = $paramValue;  
  else if ($paramName == "collectcount") 
    $collectcount = $paramValue;  
  else if ($paramName == "samplerationale_id")  
    $samplerationale_id = $paramValue;  
  else if ($paramName == "note") 
    $note = $paramValue;  
}

$dal = new DAL();

if ($ab_id == 0) {

  $ab_id = hexdec(uniqid());

  $sql = "INSERT INTO artifactbatch " .
         "(id, bag_id, affiliation_id, material_id, form_id, completeness_id, " .
         "diagnostic, collectcount, docucount, samplerationale_id, note) " .
         "VALUES ($ab_id, $ba_id, $affiliation_id, $material_id, $form_id, $completeness_id, " .
         "$diagnostic, '$collectcount', '$docucount', $samplerationale_id, '$note') "; 
  $result = $dal->query($sql);
}

else { 
 
  $sql = "UPDATE artifactbatch SET " .
         "affiliation_id=$affiliation_id, material_id=$material_id " .
         "form_id=$form_id, completeness_id=$completeness_id, ".
         "diagnostic=$diagnostic, collectcount='$collectcount', docucount='$docucount', " .
         "samplerationale_id=$samplerationale_id, note='$note' " .
         "WHERE id = $ab_id " ;
  $result = $dal->query($sql);
}
    
echo json_encode($ab_id);

?>

