<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "rowid")
    $ve_id = $paramValue;
  elseif ($paramName == "selid") 
    $pw_id = $paramValue;  
}

$dal = new DAL();
if ($ve_id > 0) {   

  $tr_id = hexdec(uniqid());
  
  $sql = "INSERT INTO travel (id, destination, vehicle_id) VALUES ($tr_id, 'to', $ve_id) " ;
  $result = $dal->query($sql);
}
else
  $tr_id = 0;

$sql = "SELECT TR.id, CONCAT (VE.name, ' (', VE.seating, ') ', VE.descr) text, 0 added " .
       "FROM (travel TR, vehicle VE, projectworkdaypersonnel PP) " .
       "LEFT JOIN personneltravel PT ON PT.travel_id = TR.id " .
       "WHERE PT.projectworkdaypersonnel_id = PP.id " .
       "AND PP.projectworkevent_id = $pw_id AND TR.destination = 'to' " .
       "AND VE.id = TR.vehicle_id " .
       "GROUP BY TR.id " .
       "UNION " .
       "SELECT TR.id, CONCAT (VE.name, ' (', VE.seating, ') ', VE.descr) text, 2 added " .
       "FROM vehicle VE, travel TR WHERE VE.id = TR.vehicle_id AND TR.id = $tr_id ";         
$result = $dal->query($sql);

echo json_encode($result);

?>

