<?php

include "../../connection.php";

$id = '';
$res='';

error_reporting(0);

$id = $_POST['id'];


//zakladne udaje o treningu + info o treningu
$q = "SELECT *
		FROM `trening`
		LEFT JOIN `users` ON users.id = trening.trener_id
		WHERE trening.id =?";

$q2 = queryWithBids($q, array($id));
$zaklad=$q2->fetchAll();

//zober vsetky zapasy
$q='SELECT *
		FROM `zapas`
		WHERE trening_id =?';

$q2 = queryWithBids($q, array($id));
$zapasy=$q2->fetchAll();

$i=0;
$odosli= array();
foreach ($zapasy as &$zapas){
	//zober kazdy zapas a zisti timy a udaje o hracoch hrajucich v nich
	$q='SELECT zapasy_ucast.*, users.meno, users.priezvisko, users.login, users.id
			FROM `zapasy_ucast`
			LEFT JOIN `users` ON users.id = zapasy_ucast.hrac_id
			WHERE zapas_id =?
			AND team = "A"';

	$q2 = queryWithBids($q, array($zapas['id']));

	$teamA=$q2->fetchAll();
	
	$q='SELECT zapasy_ucast.*, users.meno, users.priezvisko, users.login, users.id
			FROM `zapasy_ucast`
			LEFT JOIN `users` ON users.id = zapasy_ucast.hrac_id
			WHERE zapas_id =?
			AND team = "B"';
	
	$q2 = queryWithBids($q, array($zapas['id']));
	
	$teamB=$q2->fetchAll();
	
	//zakladne info
	$odosli['zaklad'][$i]=$zapas;
	//pridam novy tag k zapasom
	$odosli['teamA'][$i]=$teamA;
	//pridam novy tag k zapasom
	$odosli['teamB'][$i]=$teamB;
	$i++;
}


echo json_encode(array($zaklad, $odosli ));

?>
