<?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 projectworkday 
$sql = "SELECT VE.id, CONCAT (VE.name, ' (', VE.seating, ') ', VE.descr) text, 0 added " .
       "FROM  (vehicle VE) " .
       "LEFT JOIN (travel TR, personneltravel PT, projectworkdaypersonnel PP, projectworkday PW) " .
       "ON VE.id = TR.vehicle_id AND TR.id = PT.travel_id AND PP.id = PT.projectworkdaypersonnel_id " .
       "AND PW.id = PP.projectworkday_id AND PW.workday_id = $wo_id " .
       "WHERE TR.ID IS NULL " .
       "UNION " .
       "SELECT TR.id, CONCAT (VE.name, ' (', VE.seating, ') ', VE.descr) text, 1 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.projectworkday_id = $pw_id AND VE.id = TR.vehicle_id " .
       "GROUP BY VE.id ";
         
$result = $dal->query($sql);

echo json_encode($result);

?>

