<?php
	include_once("db.php");

	//make result object
	$result = array();
	$result['ok'] = false;

	//get post data
	$postdata = file_get_contents("php://input");
	$post = json_decode($postdata, true);

	$drivenr = $post['drivenr'];
	$idrace = $post['idrace'];

	// return $http.post('server/deleterace.php', {tier:tier, poule:poule, idchampionship:idchampionship, idteam:idteam});

	$drivedelete = pg_prepare($dbconn, "drivedelete", "delete from drive where drivenr = $1 and idrace = $2");
	$drivedelete = pg_execute($dbconn, "drivedelete", array($drivenr, $idrace));

	if($drivedelete===FALSE) {
		$result['ok'] = false;
		$result['error'] = pg_last_error($dbconn);
	} else {
		//return data
		$result['ok'] = true;
		$result['debug'] = 'deleted idrace '.$post['idrace'];
	}

	//update drivenrs
	$updatedrivenr= pg_prepare($dbconn, "updatedrivenr", "update drive set drivenr = sub.rnum from (select idrace, drivenr, row_number() OVER () as rnum from drive where idrace = $1) sub where drive.drivenr = sub.drivenr and drive.idrace = $1");
	if($updatedrivenr===false)
		$result['debug2'] = pg_last_error();
	$updatedrivenr= pg_execute($dbconn, "updatedrivenr", array($idrace));
	if($updatedrivenr===false)
		$result['debug3'] = pg_last_error();

	//print message
	$resultjson =  json_encode($result);

	echo $resultjson;
?>
