<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "addlistid")
    $wo_id = $paramValue;
  elseif ($paramName == "listid") 
    $pw_id = $paramValue;  
}

$dal = new DAL();

// All unused vehicles UNION travel records for the projectworkevent 
$sql = "SELECT VE.id, CONCAT (VE.name, ' (', VE.seating, ') ', VE.descr) text, 0 added " .
       "FROM  (vehicle VE) " .
       "LEFT JOIN (travel TR, personneltravel PT, projectworkdaypersonnel PWP, projectworkevent PW) " .
       "ON VE.id = TR.vehicle_id AND TR.id = PT.travel_id AND PWP.id = PT.projectworkdaypersonnel_id " .
       "AND PW.id = PWP.projectworkevent_id AND PW.workday_id = $wo_id " .
       "UNION ALL " .
       "SELECT TR.id, CONCAT (VE.name, ' (', VE.seating, ') ', VE.descr) text, 0 added " .
       "FROM (travel TR, vehicle VE, projectworkdaypersonnel PWP) " .
       "LEFT JOIN personneltravel PT ON PT.travel_id = TR.id " .
       "WHERE PT.projectworkdaypersonnel_id = PWP.id " .
       "AND PWP.projectworkevent_id = $pw_id AND VE.id = TR.vehicle_id " .
       "GROUP BY VE.id ";
         
$result = $dal->query($sql);

echo json_encode($result);

?>

