<?php

require_once 'session_init.php';
require_once 'config.inc.php';
require_once 'db.class.php';
require_once 'entete.php';
require_once 'fonctions.inc.php';
print '<script type="text/javascript">
	function show(object, idVote)
	{
		if (document.getElementById(object+idVote).style.display === "none")
		{
			document.getElementById(object+idVote).style.display = "block";
			document.getElementById("button"+idVote).innerText = "Cacher la liste des votants"
		}else
		{
			document.getElementById(object+idVote).style.display = "none";
			document.getElementById("button"+idVote).innerText = "Voir la liste des votants"
		}

	}
</script>';

$db = new db();
print('	<br><br>
		<!-- lalis Grid Section -->

		<section id="content">
			<div class="container">
				<div class="row">
					<div class="col-lg-12 text-center"><h3>LISTE DES VOTATIONS</h3>');
if ( empty( $_GET["id"]))
{
	choixVotation('/vote.resultat.php');
}else
{
	$idVotation = $_GET["id"];
	print('
						<h3>VOTE</h3>
						<h4>');
	$query = 'SELECT *, UNIX_TIMESTAMP(DATE_ADD(dateFin, INTERVAL 1 DAY)) as tf FROM liste_votations WHERE id=' . $db->protect($idVotation);
	$result = $db->query($query);
	$votation = $db->result->fetch_array(MYSQLI_ASSOC);
	if ($result === FALSE)
	{
		print "Votation non trouvée" . EOLH;
	}else
	{
		$now = date('Y-m-d');
		if ( time() > $votation['tf'] )
		{
			echo $votation["libelle"] . "</h4>";
			$listeAdherents = listeAdherents();
			print ('				<hr class="star-primary">
								</div>
							</div>
							<div class="row">');

			$query='SELECT * FROM liste_votes WHERE idVotation="' .$db->protect( $idVotation) .'"';
			$result = $db->query($query);
			$votes = $db->result->fetch_all(MYSQLI_ASSOC);
			foreach ($votes as $vote)
			{
				$total = 0;
				echo "<div><h5>" . $vote["libelle"] . "</h5><br>";
				$query='SELECT * FROM liste_candidats WHERE idVotation=' . $idVotation . ' AND idVote=' . $vote["id"];
				$result = $db->query($query);
				$candidats = $db->result->fetch_all(MYSQLI_ASSOC);
				echo "<pre>";
				//$resultatCandidats = '';

				$query='SELECT COUNT(*) as n FROM votes WHERE idVotation=' . $db->protect($idVotation) . ' AND idVote=' . $vote["id"] .' AND idCandidat=0';
				$result = $db->query($query);
				$resultat = $db->result->fetch_array(MYSQLI_ASSOC);
				$blanc = $resultat["n"];
				echo 'Votes blancs&nbsp;: ' . $blanc . '<br>';
				foreach($candidats as $candidat)
				{
					$query='SELECT COUNT(*) as n FROM votes WHERE idVotation=' . $db->protect($idVotation) . ' AND idVote=' . $vote["id"] .' AND idCandidat=' . $candidat["id"];
					$result = $db->query($query);
					$resultat = $db->result->fetch_array(MYSQLI_ASSOC);
					echo $candidat["candidat"] . '&nbsp;:' . $resultat["n"]  . '<br>';
					$resultatCandidats[$candidat['candidat']] = $resultat["n"];
					$total += $resultat["n"];
				}
				array_multisort($resultatCandidats, SORT_DESC, SORT_NUMERIC);
				reset($resultatCandidats);
				if ($total >= $blanc)
				{
					print("<h5>Remporte le vote : " . key($resultatCandidats));
					unset($resultatCandidats);
					print("</h5>");
				}else
				{
					print("<h5>Aucun candidat n'emporte le vote, car il y a plus de 50% de votes blancs.</h5>");
				}
				listeVotants($idVotation, $vote["id"], $listeAdherents);
				print("</pre></div><br>" . EOLH);
			}
		}else
		{
			print "Bonjour, vous pourrez consulter les résultats à partir du " . formattedate('fr', $votation['tf'] , "Europe/Paris") . "</h4>";
		}
	}


		print ('				<div>
							</div>
						</div>
				</div>
			</section>');

}

require_once 'footer.html';
?>