<?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);

	$idchampionship = $post['idchampionship'];
	$teamname = $post['name'];

	$addteam= pg_prepare($dbconn, "addteam", "insert into team (name, idchampionship) values ($1, $2) returning idteam");
	$addteam= pg_execute($dbconn, "addteam", array($teamname, $idchampionship));

	if($addteam===FALSE) {
		$result['ok'] = false;
		$result['error'] = 'insert team failed: '.pg_last_error($dbconn);
	} else {
		// //inserted team, now the link
		// $insertedteam = pg_fetch_assoc($addteam);
		// $insertedidteam = $insertedteam['idteam'];
		// $addteamlinks= pg_prepare($dbconn, "addteamlink", "insert into team_championship (idteam, idchampionship) values ($1, $2)");
		// $addteamlinks= pg_execute($dbconn, "addteamlink", array($insertedidteam, $idchampionship));
		//
		// //check
		// if($addteamlinks===FALSE) {
		// 	$result['ok'] = false;
		// 	$result['error'] = "inserted team, couldn't insert the link to championship...";
		// } else {
		// 	//all went well
		// 	$result['ok'] = true;
		// }
		$result['ok'] = true;
	}

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

	echo $resultjson;
?>
