<?php
require('auth-validate.php');
require('../../db.php');


$id_user = $_SESSION['id_user'];
$id_file_row = isset($_REQUEST['id_file_row']) ? $_REQUEST['id_file_row'] : null;


$fields_to_check = array('id_driver', 'trailer');
$dynamic_fields = '';

$params = array(
	':id_file_row' => $id_file_row
);


foreach($fields_to_check as $key)
	if(isset($_REQUEST[$key]))
	{
		$params[":$key"] = $_REQUEST[$key];
		
		if(strlen($dynamic_fields) > 0)
			$dynamic_fields .= ", ";
		
		$dynamic_fields .= "$key = :$key";
	}

	
if(isset($_REQUEST['date_canceled']))
{
	if(strlen($dynamic_fields) > 0)
		$dynamic_fields .= ", ";

	$dynamic_fields .= "date_canceled = " . ($_REQUEST['date_canceled'] ? 'NOW()' : 'NULL');
}


$sql = "UPDATE file_row
	SET $dynamic_fields
	WHERE id_file_row = :id_file_row";


$stmt = $dbh->prepare($sql);
	
try
{
	$ret = $stmt->execute($params);
}
catch(Exception $e)
{
	http_response_code(500);
	
	//TODO: probably shouldn't report this internal exception message to the user
	echo $e->getMessage();
	
	return;
}

echo 'OK';


?>