<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "tr_idfrom")
    $tr_id = $paramValue;
  elseif ($paramName == "from_type") 
    $type = $paramValue;  
  elseif ($paramName == "from_start_time") 
    $start_time = $paramValue;  
  elseif ($paramName == "from_duration") 
    $travel_dur = $paramValue;  
  elseif ($paramName == "from_mileage") 
    $mileage = $paramValue;
}

$duration = str_replace(":", ".", $travel_dur);
if ($type == "Day")
  // Day passengers: substract 1 hour 
  $passengerDur = (($duration - 1) < 0) ? 0 : ($duration - 1);
else
  $passengerDur = $duration;

$dal = new DAL();

// Travel measures
$sql = "UPDATE travel SET type='$type', start_time='$start_time' " .
       "WHERE id = $tr_id " ;
$result = $dal->query($sql);

// Driver mileage
$sql = "UPDATE personneltravel SET mileage=$mileage " .
       "WHERE travel_id = $tr_id AND travelrole_id = 1 ";
$result = $dal->query($sql);

// Driver and navigator travel duration 
$sql = "UPDATE personneltravel SET duration=$duration " .
       "WHERE travel_id = $tr_id AND travelrole_id <> 3 ";
$result = $dal->query($sql);

// Passenger travel duration 
$sql = "UPDATE personneltravel SET duration=$passengerDur " .
       "WHERE travel_id = $tr_id AND travelrole_id = 3 ";
$result = $dal->query($sql);

echo json_encode($result);

?>

