<?php
$UN_TNS = v('P_TNS');
$T_EQI = v('T_EQI');
$T_RENC = v('T_RENC');
$tPoules = v('tPoules');

//@TODO : vérifications à faire

$T_P = array();

foreach ($T_RENC as $MAT){
	if ( !isset($T_P[$MAT['MAT_POU']])){
		$T_P[$MAT['MAT_POU']] = array();
	}
	if ( !isset($T_P[$MAT['MAT_POU']][$MAT['EQI_CLE1']])){
		$T_P[$MAT['MAT_POU']][$MAT['EQI_CLE1']] = $T_EQI[$MAT['EQI_CLE1']];
	}


	if ( !isset($T_P[$MAT['MAT_POU']][$MAT['EQI_CLE2']])){
		$T_P[$MAT['MAT_POU']][$MAT['EQI_CLE2']] = $T_EQI[$MAT['EQI_CLE2']];
	}

	if ( $MAT['MAT_ETAT'] == 1 ){


		$T_P[$MAT['MAT_POU']][$MAT['EQI_CLE1']]['G'] += $MAT['EQI_S1']>$MAT['EQI_S2']?1:0;
		$T_P[$MAT['MAT_POU']][$MAT['EQI_CLE1']]['N'] += $MAT['EQI_S1']==$MAT['EQI_S2']?1:0;
		$T_P[$MAT['MAT_POU']][$MAT['EQI_CLE1']]['D'] += $MAT['EQI_S1']<$MAT['EQI_S2']?1:0;
		$T_P[$MAT['MAT_POU']][$MAT['EQI_CLE1']]['GA'] += $MAT['EQI_S1']-$MAT['EQI_S2'];
		$T_P[$MAT['MAT_POU']][$MAT['EQI_CLE1']]['POULE'] += $MAT['MAT_POU'];

		$T_P[$MAT['MAT_POU']][$MAT['EQI_CLE2']]['G'] += $MAT['EQI_S1']<$MAT['EQI_S2']?1:0;
		$T_P[$MAT['MAT_POU']][$MAT['EQI_CLE2']]['N'] += $MAT['EQI_S1']==$MAT['EQI_S2']?1:0;
		$T_P[$MAT['MAT_POU']][$MAT['EQI_CLE2']]['D'] += $MAT['EQI_S1']>$MAT['EQI_S2']?1:0;
		$T_P[$MAT['MAT_POU']][$MAT['EQI_CLE2']]['GA'] += $MAT['EQI_S2']-$MAT['EQI_S1'];
		$T_P[$MAT['MAT_POU']][$MAT['EQI_CLE2']]['POULE'] += $MAT['MAT_POU'];

	} else {
		$T_P[$MAT['MAT_POU']][$MAT['EQI_CLE1']]['POULE'] += $MAT['MAT_POU'];
		$T_P[$MAT['MAT_POU']][$MAT['EQI_CLE2']]['POULE'] += $MAT['MAT_POU'];
	}
}

if ( $UN_TNS['TNS_TYPE'] == 3 || $UN_TNS['TNS_TYPE'] == 4 ){
	$tPoules = array(0=>array('nom'=>'&nbsp;'));
}

foreach ($tPoules as $k=>$v){
	foreach ($T_P[$k] as $cle_eq=>$v){
		$T_P[$k][$cle_eq]['JOUE'] = $T_P[$k][$cle_eq]['G'] +$T_P[$k][$cle_eq]['N'] +$T_P[$k][$cle_eq]['D'];
		$T_P[$k][$cle_eq]['PTS'] = $UN_TNS['TNS_G']*$T_P[$k][$cle_eq]['G'] + $UN_TNS['TNS_N']*$T_P[$k][$cle_eq]['N'] + $UN_TNS['TNS_D']*$T_P[$k][$cle_eq]['D'];
	}
}

if ( !function_exists('cmp2') ){
	function cmp2($a,$b) {
		if ($a['GA'] == $b['GA'])
		return 0;
		return ($a['GA'] > $b['GA']) ? -1 : 1;
	}
}
if ( !function_exists('cmp1') ){
	function cmp1($a,$b) {
		if ($a['PTS'] == $b['PTS'])
		return cmp2($a,$b);
		return ($a['PTS'] > $b['PTS']) ? -1 : 1;
	}
}
foreach ($tPoules as $k=>$v){
	usort($T_P[$k],'cmp1');
}

if ( $UN_TNS['TNS_TYPE']== 3 || $UN_TNS['TNS_TYPE'] == 4){
	?><h2 align="center"><?php echo __('com.txt.classement'); ?></h2><?php
} else {
	?><h2 align="center"><?php echo __('com.txt.poules'); ?></h2><?php
}
foreach ($tPoules as $k=>$oP){
	?>
	<table class="table table-striped poules_coupe" cellpadding="0" cellspacing="0" align="center" height="<?php echo 20*(1+sizeof($T_P[$k])); ?>px">
		<caption class="lead"><?php echo $oP['nom']; ?>
		<a href="<?php echo WWW_ROOT; ?>poules/widget?poule=<?php echo ($k+1); ?>&tournoi=<?php echo $UN_TNS['TNS_CLE']; ?>" title="widget"><?php echo img('icons/chart_organisation.png'); ?></a>
		</caption>
        <thead>
		<tr>
			<th>#</th>
			<th><?php echo __('com.table.equipe'); ?></th>
			<th><?php echo __('com.table.poule.joue'); ?></th>
			<th><?php echo __('com.table.poule.gagne'); ?></th>
			<th><?php echo __('com.table.poule.null'); ?></th>
			<th><?php echo __('com.table.poule.perdu'); ?></th>
			<th><?php echo __('com.table.poule.diff'); ?></th>
			<th><?php echo __('com.table.poule.points'); ?></th>
			<?php if ( v('fromAdmin') == 1 && v('nb_renc_a_jouer')==0 ) { ?><!--[[SUP--><th>&nbsp;</th><!--SUP]]--><?php } ?>
		</tr>
        </thead>
	<?php
	$i = 0;
        $der_points = null;
        $der_classement = 1;
        $der_aff_classement = '-';
        $der_ga = null;
	$pts_max = -1;
	$der_eq = null;
	foreach ($T_P[$k] as $kk=>$eq){
        $der_aff_classement = $der_classement;
        if ($der_points === null) {
            $der_classement = 1;
        } else {
            if ($der_points != $eq['PTS']) {
                $der_classement++;
                $der_aff_classement = $der_classement;
            } else {
                if ($der_ga != $eq['GA']) {
                    $der_classement++;
                    $der_aff_classement = $der_classement;
                } else {
                    $der_aff_classement = '-';
                }
            }
        }
        $der_points = $eq['PTS'];
        $der_ga = $eq['GA'];
		$pts_max = $pts_max>$eq['PTS']?$pts_max:$eq['PTS'];
		?>
		<tr <?php echo $i%2==0?'class="pair"':''; ?>>
            <td><?php echo $der_aff_classement; ?></td>
			<td><?php echo zk_equipe($eq); ?></td>
			<td class="small"><?php echo $eq['JOUE']; ?></td>
			<td class="small"><?php echo $eq['G']; ?></td>
			<td class="small"><?php echo $eq['N']; ?></td>
			<td class="small"><?php echo $eq['D']; ?></td>
			<td class="small"><?php echo $eq['GA']; ?></td>
			<td class="small PTS"><?php echo $eq['PTS']; ?></td>
			<?php if ( v('fromAdmin') == 1&& v('nb_renc_a_jouer')==0 ) { ?>
					<!--[[SUP-->
					<td class="small">
					<input type="checkbox" name="eq[<?php echo $eq['EQI_CLE']; ?>]" value="1" <?php echo isset($_POST['eq'])?isset($_POST['eq'][$eq['EQI_CLE']])?'checked="checked"':'':$i<($UN_TNS['TNS_NB_EQ_TNS']/$UN_TNS['TNS_NB_POU'])?'checked="checked"':''; ?> />
					</td>
					<!--SUP]]-->
					<?php
			}
			?>
		</tr>
		<?php
		$i++;
		$der_eq = $eq;
	}
	?></table>
	<?php
}

?><hr class="espace" />