<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "taken") 
    $timeTaken = $paramValue;  
  else if ($paramName == "selectid") 
    $sp_id = $paramValue;  
  else if ($paramName == "refcode") 
    $refCode = $paramValue;  
}

$refCode = "2020-0001";

$dal = new DAL();

$sql = "INSERT INTO photo (time_taken) VALUES ('$timeTaken') ";
$ph_id = $dal->query($sql);
//print_r ("ph:$ph_id");

$path = PHOTO_PATH . $refCode ;
$fileName = $refCode . "_" . str_pad($ph_id, 8, '0', STR_PAD_LEFT);
$filePath = $path . "/" . $fileName;

$sql = "UPDATE photo SET filename='$filePath' WHERE id = $ph_id ";
$result = $dal->query($sql);       

$sql = "INSERT INTO photospace (photo_id, space_id) " .
         "VALUES ($ph_id, $sp_id) "; 
$result = $dal->query($sql);
    
echo json_encode(array($ph_id, $fileName, $refCode, $path, $timeTaken));

?>

