<?php
if(!defined('INDEX')) { header('Location: ../'); exit; }
$page = 'classement';
$titre = 'Classement';

//Page affichée
$q = gpc('q');
if (is_numeric($q) && $q > 0) $pageClassement = $q;
else $pageClassement = 1;

//Type de classement
$t = gpc('t');
switch($t){
	case '0':
		$TypeSearch = false;
		break;
	default:
		$TypeSearch = true;
}
$template->assign('t', $TypeSearch);

//Catégorie du classement
$e = gpc('e');
switch($e){
	case 'batiment':
		$export = 'batiments';
		break;
	case 'flotte':
		$export = 'flottes';
		break;
	case 'recherches':
		$export = 'recherches';
		break;
	default:
		$export = 'points';
}
$template->assign('export', $export);

//Nombre de joueurs par page
$n = gpc('n');
if (is_numeric($n) && $n > 0) $nbppage = $n;
else $nbppage = 100;

$pageClassementN = ($pageClassement-1);

$nbpages = ceil(($nbhumain['humains']+$nbcovie['covenants'])/$nbppage);
if ($pageClassement > $nbpages) $pageClassement = $nbpages;

$TEMP_pages = '<select name="q">';
for($i=1 ; $i<=$nbpages ; $i++) {
	if ($i == $pageClassement) $TEMP_pages .= '<option value="'.$i.'" selected="selected">'.(($i-1)*$nbppage+1).'-'.($i*$nbppage).'</option>';
	else $TEMP_pages .= '<option value="'.$i.'">'.(($i-1)*$nbppage+1).'-'.($i*$nbppage).'</option>';
}
$TEMP_pages .= '</select>';
$template->assign('q', $TEMP_pages);

$plage_debut = $nbppage * $pageClassementN;
$plage_fin = $nbppage * ($pageClassementN + 1);
if ($TypeSearch) {
	$bdd->connexion();
	$data = $bdd->query("SELECT C.".$export.", C.nbr_membres FROM $table_classement_alliances C JOIN $table_alliances A ON 1 = A.id ORDER BY C.$export DESC LIMIT $plage_fin OFFSET $plage_debut;");
	$bdd->deconnexion();
	$nbres = $bdd->num_rows;
	var_dump($data);

	$j = ($pageClassement-1)*$nbppage;

	$TEMP_classement = array();
	for ($i=0; $i<$nbres; $i++) {
		$j++;
		$TEMP_classement[] = array($j, $data[$i]['tag'], $data[$i]['nbr_membres'], separerNombres($data[$i][$export]),0);
	}
}
else {
	$bdd->connexion();
	$data = $bdd->query("SELECT C.$export, U.pseudo, U.race, A.tag, A.nom FROM $table_classement C INNER JOIN $table_user U ON C.id_user = U.id LEFT OUTER JOIN $table_alliances A ON A.id = U.id_alliance ORDER BY C.$export DESC LIMIT $plage_fin OFFSET $plage_debut;");
	$bdd->deconnexion();
	$nbres = $bdd->num_rows;

	$j = ($pageClassement-1)*$nbppage;

	$TEMP_classement = array();
	for ($i=0; $i<$nbres; $i++) {
		$j++;
		$TEMP_classement[] = array($j, $data[$i]['pseudo'], $data[$i]['race'], $data[$i]['tag'], $data[$i]['nom'], separerNombres($data[$i][$export]));
	}

	$template->assign('places', $TEMP_classement);
}

/*
if (isset($_GET['t']) && $_GET['t'] == '1')
{
	$bdd->connexion();
	$result = $bdd->query("SELECT tag, nbr_membres, ".$export.", points_".$export."_by_members FROM classement_alliances ORDER BY $export DESC LIMIT $pageClassementN OFFSET $nbppage;");
	$bdd->deconnexion();
	$nbres = $bdd->num_rows;

	$TEMP_classement = array();
	$i = ($pageClassement-1)*$nbppage;

	$template->assign('export', $export);

	for ($i=0; $i<$nbres; $i++) {
		$i++;
		$TEMP_classement[] = array($i, $data['tag'], $data['nbr_membres'],separerNombres($data[$export]),separerNombres($data["points_".$export."_by_members"]));
	}

	$template->assign('places', $TEMP_classement);
}
*/
?>