<?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 == "currid") 
    $su_id = $paramValue;  
  else if ($paramName == "scopeid") 
    $pwo_id = $paramValue;  
}

$dal = new DAL();

$sql = "SELECT UUID_SHORT() AS id LIMIT 1 "; 
$ph_id = $dal->query($sql)->id;

$sql = "INSERT INTO photo (id, time_taken) VALUES ($ph_id, '$timeTaken') ";
$ph_id = $dal->query($sql);       

$sql = "SELECT refno " .
       "FROM projectworkdayoperation PWO, projectworkday PW, parcel PA, project PR " .
       "WHERE PWO.id = $pwo_id AND PW.id = PWO.projectworkday_id " .
       "AND PA.id = PW.parcel_id AND PR.id = PA.project_id LIMIT 1";
$refno = $dal->query($sql)->refno;       

$path = PHOTO_PATH . $refno ;
$fileName = $refno . "_" . 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 = "SELECT UUID_SHORT() AS id LIMIT 1 "; 
$osp_id = $dal->query($sql)->id;

$sql = "INSERT INTO operationspatialunitphoto (id, photo_id, operationspatialunit_id) " .
         "VALUES ($osp_id, $ph_id, (SELECT id FROM operationspatialunit WHERE spatialunit_id = $su_id)) "; 
$result = $dal->query($sql);
    
echo json_encode(array($ph_id, $fileName, $refno, $path, $timeTaken));

?>

