<?php

$T_INFOS = v('T_INFOS');
$T_EQI = $T_INFOS['T_EQI'];
$T_EQI_INV = v('T_EQI_INV');
$T_MAT = $T_INFOS['T_MAT'];
$T_MAT_LB = $T_INFOS['T_MAT_LB'];
$T_LB = $T_INFOS['T_LB'];

//echo pr($T_EQI);
//echo pr($T_MAT);

$nb_eq = sizeof($T_EQI);
$optimized = false;
$loser_bracket = $T_LB;
/* Trouver le nombre de tour */
$nb_tour = zk_tn_nb_tour($nb_eq);

//On vérifie que le nombre d'équipe soit bien une puissance de 2 supérieure à 2.
if ( $nb_tour==0 ) { die(__('com.err.bad.nb_equipes')); }

$t_pr = array(); //Prochaine rencontre (indique ligne + col)
$t_flags = array(); //Tableau des flags (activé ? début ? moitié ? fin ?)
$t_decalages = array(); //Tableau des décalages de hauteur (distance entre équipes)
$t_rowspan = array(); //Optimisation des zones vides
$t_rowspan_s = array(); //Optimisation des zones vides petites

$t_decalages[1] = 2;//Utilisé pour les flags uniquement

foreach($T_EQI_INV as $k=>$v){
    $T_EQI[$k] = $v;
}

?>
<style type="text/css">
table.arbre tr td.eq { width: 160px; }
</style>

<?php 
if ( $T_LB == 1 ){
	$INFOS_W = v('INFOS_W');

	$WIN_W = $INFOS_W['FIN'][$INFOS_W['indice'][0]];
	$WIN_L = $INFOS_W['FIN'][$INFOS_W['indice'][1]];
	$WIN_EQI = isset($INFOS_W['WIN_EQI'])?$INFOS_W['WIN_EQI']:'';
	?>
	<table class="arbre" width="300px" align="center">
		<caption><?php echo __('com.table.finale'); ?></caption>
		<tr>
			<td class="eq">
			<?php
			$html = '';
			if ( is_array($WIN_W) && isset($WIN_W['EQI_CLE']) && isset($T_EQI[$WIN_W['EQI_CLE']]) ){
				$FP = isset($WIN_W['FP'])?$WIN_W['FP']:'';
				$html = $FP!=''?'<!--['.$FP.']DEB-->':'';
				$html .= score($WIN_W['EQI_S'],$WIN_W['EQI_WIN']);
				$html .= zk_eq_tn($T_EQI[$WIN_W['EQI_CLE']],false);
				$html .= $FP!=''?'<!--['.$FP.']FIN-->':'';
			} else {
				$html .= '&nbsp;';
			}
			echo $html;
			?>
			</td>
			<td class="s d"></td>
			<td></td>
		</tr>
		
		<tr>
			<td></td>
			<td class="s m"></td>
			<td class="eq">
			<?php
			$html = '';
			if ( is_array($WIN_EQI) && isset($WIN_EQI['EQI_CLE']) && isset($T_EQI[$WIN_EQI['EQI_CLE']]) ){
				$FP = isset($WIN_EQI['FP'])?$WIN_EQI['FP']:'';
				$html = $FP!=''?'<!--['.$FP.']WINNER-->'.img('icons/cup.gif','winner'):'';
				$html .= zk_eq_tn($T_EQI[$WIN_EQI['EQI_CLE']],false);
				$html .= $FP!=''?'<!--['.$FP.']FIN-->':'';
			} else {
				$html .= '&nbsp;';
			}
			echo $html;
			?>
			</td>
		</tr>
		<tr>
			<td class="eq">
			<?php
			$html = '';
			if ( is_array($WIN_L) && isset($WIN_L['EQI_CLE']) && isset($T_EQI[$WIN_L['EQI_CLE']]) ){
				$FP = isset($WIN_L['FP'])?$WIN_L['FP']:'';
				$html = $FP!=''?'<!--['.$FP.']DEB-->':'';
				$html .= score($WIN_L['EQI_S'],$WIN_L['EQI_WIN']);
				$html .= zk_eq_tn($T_EQI[$WIN_L['EQI_CLE']],false);
				$html .= $FP!=''?'<!--['.$FP.']FIN-->':'';
			} else {
				$html .= '&nbsp;';
			}
			echo $html;
			?>
			</td>
			<td class="s f"></td>
			<td></td>
		</tr>

	</table>
	<?php
}
?>

<table class="arbre" align="center">
<caption><?php echo __('com.table.win'); ?></caption>
<?php
for($i=1;$i<$nb_eq*2;$i++){
	if ( !$optimized ) { echo "\n"; }
	?><tr><?php
	for($j=1;$j<$nb_tour*2;$j++){
		//On définit le décalage
		if ( $j>1 && $j%2!=0 && !isset($t_decalages[$j])){
			$t_decalages[$j] = pow(2,($j-1)/2);
			$t_pr[$j] = $t_decalages[$j];
		}
		$t_class = array();//Tableau des styles à attribuer
		$html = '';

		if ( $j%2==0 ){ $t_class[] = 's'; } //Petite colonne
		if ( $j==1 && $i%2!=0 ) { $t_class[] = 'eq'; } //Equipe de la première colonne

		if ( isset($t_pr[$j]) && $t_pr[$j] == $i){
			$t_class[] = 'eq';

			if ( $i == $t_decalages[$j] ){
				//On est sur le premier des rencontres, on recalcule l'écart
				$t_decalages[$j] *= 2;
			}
			$t_pr[$j] = $i+$t_decalages[$j];
		}

		if ( $j%2!=0 && isset($t_class) && in_array('eq',$t_class) ){
			//On est sur une équipe
			if ( !isset($t_flags[$j+1]) ){
				$t_flags[$j+1] = array('debut'=>$i,'fin'=>$i+$t_decalages[$j],'milieu'=>($i+$i+$t_decalages[$j])/2);
			}
			/**
			 * 
			 * 	AFFICHAGE D UNE EQUIPE
			 * 
			 */
			$FP = isset($T_MAT[$j]) && isset($T_MAT[$j][$i]) && isset($T_MAT[$j][$i]['FP'])?$T_MAT[$j][$i]['FP']:'';
			$boo_winner = $j == ($nb_tour*2-1);

			if ( $boo_winner ){
				if ( $T_LB == 0 ){
					$html = $FP!=''?'<!--['.$FP.']WINNER-->'.img('icons/cup.gif','winner'):'';
				} else {
					$html = $FP!=''?'<!--['.$FP.']WIN_W-->':'';
				}
			} else {
				$html = $FP!=''?'<!--['.$FP.']DEB-->':'';
			}
			if ( isset($T_MAT[$j]) && isset($T_MAT[$j][$i]) && isset($T_EQI[$T_MAT[$j][$i]['EQI_CLE']]) ){
				if ( !$boo_winner ) { $html .= score($T_MAT[$j][$i]['EQI_S'],$T_MAT[$j][$i]['EQI_WIN']); }
				$html .= zk_eq_tn($T_EQI[$T_MAT[$j][$i]['EQI_CLE']],false);

			} else {
				//$html .= 'a venir('.$j.')('.$i.')';
				$html .= '&nbsp;';
			}
			$html .= $FP!=''?'<!--['.$FP.']FIN-->':'';
			//$html = '['.$j.']['.$i.']';
		}
		else {
			if ( isset($t_flags[$j]) ){
				if ( $t_flags[$j]['debut'] == $i){
					$t_class[] = 'd';
				} else if ( $t_flags[$j]['fin'] == $i){
					$t_class[] = 'f';
					unset($t_flags[$j]);
					unset($t_rowspan_s[$j]);
				} else if ( $i > $t_flags[$j]['debut'] && $i < $t_flags[$j]['fin']){
					if ( $t_flags[$j]['milieu'] == $i ) {
						$t_class[] = 'm';
					} else {
						$t_class[] = 'i';
					}
				}
			}
		}

		/* AFFICHAGE DE LA CELLULE */

		if (sizeof($t_class)==0) {

			//echo '<td>';

			if ( $j >= 3 ){
				//On peut faire des rowspan
				if ( $t_decalages[$j]-1 > 1 ){
					if ( !isset($t_rowspan[$j]) ){
						//Début du rowspan
						if ( $i+$t_decalages[$j]-1 > $nb_eq*2-1 ){
							$t_rowspan[$j] = ($nb_eq*2-1) - $i +1;
						} else {
							$t_rowspan[$j] = $t_decalages[$j]-1;
						}
						if ( !$optimized ) { echo "\n\t"; }
						echo '<td';
						if ( $t_rowspan[$j]>1) {
							echo ' rowspan="'.$t_rowspan[$j].'"';
						}
						echo '></td>';

					} else {
						echo '';//RIEN
					}
				} else {
					if ( !$optimized ) { echo "\n\t"; }
					?><td></td><?php //Premiàre ligne, troisème colonne
				}
			} else {
				if ( !$optimized ) { echo "\n\t"; }
				?><td></td><?php
			}

		} else {
			unset($t_rowspan[$j]);

			if ( $j%2==0 && !isset($t_flags[$j]) && isset($t_class) && !in_array('f',$t_class) ){

				if ( ($t_decalages[$j-1]-1) > 1 ){
					if ( !isset($t_rowspan_s[$j]) ){

						//Début du rowspan
						if ( $i+$t_decalages[$j-1]-1 > $nb_eq*2-1 ){
							$t_rowspan_s[$j] = ($nb_eq*2-1) - $i +1;
						} else {
							$t_rowspan_s[$j] = $t_decalages[$j-1]-1;
						}
						if ( !$optimized ) { echo "\n\t"; }
						echo '<td class="s"';
						if ( $t_rowspan_s[$j]>1) {
							echo ' rowspan="'.$t_rowspan_s[$j].'"';
						}
						echo '></td>';

					} else {
						echo '';//RIEN
					}
				} else {
					if ( !$optimized ) { echo "\n\t"; }
					?><td class="s"></td><?php //Première ligne, troisème colonne
				}
			} else {

				if ( isset($T_MAT[$j]) && isset($T_MAT[$j][$i]) ){
					if ( zk_est_alert($T_MAT[$j][$i]['MAT_FLAG'],$T_MAT[$j][$i]['MAT_ETAT']) ){
						$t_class[] = 'alert';
					}
					if ( zk_est_look($T_MAT[$j][$i]['MAT_FLAG'],$T_MAT[$j][$i]['MAT_ETAT']) ){
						$t_class[] = 'look';
					}
				}

				if ( !$optimized ) { echo "\n\t"; }
				echo '<td';
				if ( isset($t_class) && sizeof($t_class) > 0 ){
					echo ' class="'.join($t_class,' ').'"';
				}
				?>><?php
				echo $html;
				?></td><?php
			}
		}
	}
	if ( !$optimized ) { echo "\n"; }
	?></tr><?php
}
?></table><?php

if ( $loser_bracket ){

	unset($t_pr,$t_decalages,$t_flags);

	$nb_tour = zk_tn_lb_nb_tour($nb_eq);
	$decalage_max = zk_tn_lb_decalage_max($nb_tour);

	//Calculs des premiéres rencontres
	$t_decalages = zk_tn_lb_calcul_positions($nb_eq,$nb_tour,$decalage_max);

	?><table class="arbre" align="center">
	<caption><?php echo __('com.table.lb'); ?></caption>
	<?php
	for($i=1;$i<$nb_eq+$decalage_max;$i++){
		if ( !$optimized ) { echo "\n"; }
		?><tr><?php
		for($j=1;$j<$nb_tour*2;$j++){
			$html = '';
			$t_class = array();

			if ( $j%2==0 )
			{ $t_class[] = 's'; }
			else {

				if ( isset($t_decalages[$j]['debut']) && $t_decalages[$j]['debut']==$i){
					//Calcul du futur décalage
					if ( $t_decalages[$j]['qte']>0){
						$t_class[] = 'eq';
						$t_decalages[$j]['debut'] = $i+$t_decalages[$j]['lignes'];
						$t_decalages[$j]['qte']--;
					} else {
						unset($t_decalages[$j]['debut']);
					}
				}
			}
			if ( in_array('eq',$t_class)) {
				/**
			 	* 
			 	* 	AFFICHAGE D UNE EQUIPE
			 	* 
			 	*/
				if ( !isset($t_flags[$j+1]) ){
					$t_flags[$j+1] = array('debut'=>$i,'fin'=>$i+$t_decalages[$j]['lignes'],'milieu'=>($i+$i+$t_decalages[$j]['lignes'])/2);
				}

				$FP = isset($T_MAT_LB[$j]) && isset($T_MAT_LB[$j][$i]) && isset($T_MAT_LB[$j][$i]['FP'])?$T_MAT_LB[$j][$i]['FP']:'';

				$boo_winner = $j == ($nb_tour*2-1);

				if ( $boo_winner ){
					if ( $T_LB == 0 ){
						$html = $FP!=''?'<!--['.$FP.']WINNER-->':'';
					} else {
						$html = $FP!=''?'<!--['.$FP.']WIN_L-->':'';
					}
				} else {
					$html = $FP!=''?'<!--['.$FP.']DEB-->':'';
				}
				if ( isset($T_MAT_LB[$j]) && isset($T_MAT_LB[$j][$i]) && isset($T_EQI[$T_MAT_LB[$j][$i]['EQI_CLE']]) ){
					$html .= $boo_winner?'':score($T_MAT_LB[$j][$i]['EQI_S'],$T_MAT_LB[$j][$i]['EQI_WIN']);;
					$html .= zk_eq_tn($T_EQI[$T_MAT_LB[$j][$i]['EQI_CLE']],false);

				} else {
					//$html .= 'a venir('.$j.')('.$i.')';
					$html .= '&nbsp;';
				}
				$html .= $FP!=''?'<!--['.$FP.']FIN-->':'';
				//$html = '['.$j.']['.$i.']';

			} else {
				$html = '';

				if ( isset($t_flags[$j]) ){
					if ( $t_flags[$j]['debut'] == $i){
						$t_class[] = 'd';
					} else if ( $t_flags[$j]['fin'] == $i){
						$t_class[] = 'f';
						unset($t_flags[$j]);
						//unset($t_rowspan_s[$j]);
					} else if ( $i > $t_flags[$j]['debut'] && $i < $t_flags[$j]['fin']){
						if ( $t_flags[$j]['milieu'] == $i ) {
							$t_class[] = 'm';
						} else {
							$t_class[] = 'i';
						}
					}
				}
			}


			if ( !$optimized ) { echo "\n\t"; }
			echo '<td';
			if ( isset($t_class) && sizeof($t_class) > 0 ){
				echo ' class="'.join($t_class,' ').'"';
			}
			?>><?php
			echo $html;
			?></td><?php
		}
		if ( !$optimized ) { echo "\n"; }
		?></tr><?php
	}
	?></table><?php
}

if ( $T_LB == 0 && v('INFOS_3') ){
	$INFOS_3 = v('INFOS_3');

	$WIN_W = $INFOS_3['FIN'][$INFOS_3['indice'][0]];
	$WIN_L = $INFOS_3['FIN'][$INFOS_3['indice'][1]];
	$WIN_EQI = isset($INFOS_3['WIN_EQI'])?$INFOS_3['WIN_EQI']:'';
?>
<table class="arbre" width="300px" align="center">
	<caption><?php echo __('com.table.3rd'); ?></caption>
	<tr>
		<td class="eq">
		<?php
		$html = '';
		if ( is_array($WIN_W) && isset($WIN_W['EQI_CLE']) && isset($T_EQI[$WIN_W['EQI_CLE']]) ){
			$FP = isset($WIN_W['FP'])?$WIN_W['FP']:'';
			$html = $FP!=''?'<!--['.$FP.']DEB-->':'';
			$html .= score($WIN_W['EQI_S'],$WIN_W['EQI_WIN']);
			$html .= zk_eq_tn($T_EQI[$WIN_W['EQI_CLE']],false);
			$html .= $FP!=''?'<!--['.$FP.']FIN-->':'';
		} else {
			$html .= '&nbsp;';
		}
		echo $html;
		?>
		</td>
		<td class="s d"></td>
		<td></td>
	</tr>
	
	<tr>
		<td></td>
		<td class="s m"></td>
		<td class="eq">
		<?php
		$html = '';
		if ( is_array($WIN_EQI) && isset($WIN_EQI['EQI_CLE']) && isset($T_EQI[$WIN_EQI['EQI_CLE']]) ){
			$html .= zk_eq_tn($T_EQI[$WIN_EQI['EQI_CLE']],false);
		} else {
			$html .= '&nbsp;';
		}
		echo $html;
		?>
		</td>
	</tr>
	<tr>
		<td class="eq">
		<?php
		$html = '';
		if ( is_array($WIN_L) && isset($WIN_L['EQI_CLE']) && isset($T_EQI[$WIN_L['EQI_CLE']]) ){
			$FP = isset($WIN_L['FP'])?$WIN_L['FP']:'';
			$html = $FP!=''?'<!--['.$FP.']DEB-->':'';
			$html .= score($WIN_L['EQI_S'],$WIN_L['EQI_WIN']);
			$html .= zk_eq_tn($T_EQI[$WIN_L['EQI_CLE']],false);
			$html .= $FP!=''?'<!--['.$FP.']FIN-->':'';
		} else {
			$html .= '&nbsp;';
		}
		echo $html;
		?>
		</td>
		<td class="s f"></td>
		<td></td>
	</tr>

</table>

<?php
}
?>