<?php

$tex = file_get_contents("assets/Autorisation_parentale.tex");

/**
 * @var User $user
 * @var Team $team
 * @var Tournament $tournament
 */
$user = $_SESSION["user"];
$team = $_SESSION["team"];

if (!isset($user) || isset($_GET["blank"]) || $_SESSION["role"] == Role::ORGANIZER || $_SESSION["role"] == Role::ADMIN) {
	$tournament = Tournament::fromName($_GET["blank"]);

	if ($tournament == null)
		require_once "server_files/403.php";

	$tex = preg_replace("#{PARTICIPANT_NAME}#", "\\hrulefill", $tex);
	$tex = preg_replace("#{BIRTHDAY}#", "\phantom{232323}/\phantom{232323}/\phantom{232323}", $tex);
	$tex = preg_replace("#{PRONOUN}#", "Elle/il", $tex);
}
else {
	$tournament = $team->getEffectiveTournament();
	$tex = preg_replace("#{PARTICIPANT_NAME}#", "\\texttt{" . $user->getFirstName() . " " . $user->getSurname() . "}", $tex);
	$tex = preg_replace("#{BIRTHDAY}#", "\\texttt{" . strftime("%d %B %G", strtotime($user->getBirthDate())) . "}", $tex);
	$tex = preg_replace("#{PRONOUN}#", $user->getGender() == "M" ? "Il" : "Elle", $tex);
}

$tex = preg_replace("#{TOURNAMENT_NAME}#", $tournament->getName(), $tex);
$tex = preg_replace("#{PLACE}#", $tournament->getPlace(), $tex);
$tex = preg_replace("#{START_DATE}#", strftime("%d %B", strtotime($tournament->getStartDate())), $tex);
$tex = preg_replace("#{END_DATE}#", strftime("%d %B", strtotime($tournament->getEndDate())), $tex);
$tex = preg_replace("#{YEAR}#", $YEAR, $tex);

shell_exec("mkdir tmp");

file_put_contents("tmp/file.tex", $tex);

shell_exec("pdflatex -synctex=1 -interaction=nonstopmode -shell-escape -output-directory=tmp tmp/file.tex");
header("Content-type: application/pdf");
readfile("tmp/file.pdf");

exit(0);