<?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');
}


$division = 1;
$sous_division = 0;
$y = 0;

foreach ($tPoules as $k => $oP) {
    $class_ligue = ($y == 0) ? 'une-poule' : '';
    if ($y >= 1 && $y % 2 == 1) {
        $division++;
        $sous_division = 0;
    }
    $sous_division++;
    ?>
<table class="table poules_coupe pageLesMatchs poule-classement ligue-classement" cellpadding="0" align="center" cellspacing="0">
    <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" target="_blank"><?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 && $UN_TNS['TNS_TYPE'] != 7) { ?><!--[[SUP-->
        <th>&nbsp;</th><!--SUP]]--><?php } ?>
    </tr>
    </thead>
    <?php

    $der_points = null;
    $der_classement = 1;
    $der_aff_classement = '-';
    $der_ga = null;
    $i = 0;
    $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 class="s small"><?php echo $der_aff_classement; ?></td>
            <td style="text-align: left !important;"><?php echo zk_equipe($eq); ?></td>
            <td class="s small"><?php echo $eq['JOUE']; ?></td>
            <td class="s small"><?php echo $eq['G']; ?></td>
            <td class="s small"><?php echo $eq['N']; ?></td>
            <td class="s small"><?php echo $eq['D']; ?></td>
            <td class="s small"><?php echo $eq['GA']; ?></td>
            <td class="s small PTS"><?php echo $eq['PTS']; ?></td>
            <?php if (v('fromAdmin') == 1 && v('nb_renc_a_jouer') == 0 && $UN_TNS['TNS_TYPE'] != 7) { ?>
            <!--[[SUP-->
            <td class="s 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
    ++$y;
} ?>

<hr class="espace" />
<?php
$UN_TNS = v('P_TNS');
$T_EQI = v('T_EQI');
$T_RENC = v('T_RENC');
$tPoules = v('tPoules');
$der_tour = ''; //$T_RENC[0]['MAT_TOUR'];
$der_poule = ''; //$T_RENC[0]['MAT_POU'];

$cpt = 0;
if ($UN_TNS['TNS_TYPE'] == 3 || $UN_TNS['TNS_TYPE'] == 4) {
    $der_renc = array_pop($T_RENC);
    $nb_tour = $der_renc['MAT_TOUR'];
    $T_RENC[key($der_renc)] = $der_renc;
    ?><h2><?php echo __('com.txt.matchs_aller'); ?></h2><?php
}

for ($i = 0; $i <= sizeof((array)$tPoules); ++$i) {

    foreach ($T_RENC as $MAT) {
        if ($MAT['MAT_POU'] != $i) {
            continue;
        }
        $intervention_admin = false; //$MAT['MAT_FLAG']==4||$MAT['MAT_FLAG']==8;
        $clos_mais_a_terminer = false; //$MAT['MAT_FLAG']==9&&$MAT['MAT_ETAT']==0;

        $icon = $MAT['MAT_ETAT'] == 1 ? img('icons/tick.png') : img('icons/stop.png');
        $icon = $MAT['MAT_ETAT'] == 2 ? img('icons/cross_grey.png') : $icon;
        //$icon = $intervention_admin === false ?$icon:img('icons/error.png');
        //$icon = $clos_mais_a_terminer?img('icons/eye.png'):$icon;

        if (zk_est_alert($MAT['MAT_FLAG'], $MAT['MAT_ETAT'])) {
            $icon = img('icons/error.png');
            $intervention_admin = true;
        }
        if (zk_est_look($MAT['MAT_FLAG'], $MAT['MAT_ETAT'])) {
            $icon = img('icons/eye.png');
            $clos_mais_a_terminer = true;
        }
        if (sizeof($tPoules) > 1 && $MAT['MAT_POU'] != $der_poule) {
            if ($i > 0) {
                echo "</table>";
            }
            ?>
				<table align="center" class="pageLesMatchs tabRencontres poule-matchs poule-matchs-<?php echo $i; ?>"
                       cellpadding="0" cellspacing="0"
                       border="0">
			<caption class="lead"><?php echo $tPoules[$MAT['MAT_POU']]['nom']; ?></caption>

            <?php
            $der_poule = $MAT['MAT_POU'];
            $cpt = 0;
        }
        if ($MAT['MAT_TOUR'] != $der_tour) {
            //echo $der_tour==''?'':'</table>';

            if (($UN_TNS['TNS_TYPE'] == 3 || $UN_TNS['TNS_TYPE'] == 4) && ($MAT['MAT_TOUR'] - 1) == ($nb_tour / 2)) {
                echo '<hr class="espace" /><h2>Matchs retour</h2>';
            }

            ?>
            <tr>
                <td colspan="6"
                    class="poule sep-matchs"><?php echo __('com.txt.jour'); ?> <?php echo $MAT['MAT_TOUR']; ?></td>
            </tr>
            <?php

            $der_tour = $MAT['MAT_TOUR'];
        }

        if ($MAT['MAT_ETAT'] == 1) {
            $c1 = $MAT['EQI_S1'] != '' && $MAT['EQI_S2'] != '' ? ($MAT['EQI_S1'] == $MAT['EQI_S2'] ? 'egalite' : ($MAT['EQI_S1'] > $MAT['EQI_S2'] ? 'gagnant' : 'perdant')) : '';
            $c2 = $MAT['EQI_S1'] != '' && $MAT['EQI_S2'] != '' ? ($MAT['EQI_S1'] == $MAT['EQI_S2'] ? 'egalite' : ($MAT['EQI_S1'] < $MAT['EQI_S2'] ? 'gagnant' : 'perdant')) : '';
        } else {
            $c1 = '';
            $c2 = '';
            $MAT['EQI_S1'] = '&nbsp;';
            $MAT['EQI_S2'] = '&nbsp;';
        }

        ?>
        <tr class="<?php echo $cpt++ % 2 == 0 ? 'pair' : 'impair'; echo ' '; echo ($MAT['MAT_ETAT'] == 1 || $MAT['MAT_ETAT'] == 2) ? '' : ($MAT['EQI_S1'] != 0 || $MAT['EQI_S2'] != 0 ? 'a_valider' : 'a_jouer'); echo $intervention_admin ? ' alert' : ''; echo $clos_mais_a_terminer ? ' look' : ''; echo $MAT['MAT_ETAT'] == 2 ? 'non_joue' : ''; ?>">
            <td class="s small"><?php echo href($icon, array('controleur' => '[[controleur]]', 'action' => '[[action]]', 'params' => '[[params_' . $MAT['TNS_CLE'] . '-' . $MAT['MAT_FP'] . ']]', 'class' => 'equipe ajax'), false, false, false); ?></td>
            <td class="eq"><?php echo zk_eq_tn($T_EQI[$MAT['EQI_CLE1']], false); ?></td>
            <td class="s small <?php echo $c1; ?>"><?php echo $MAT['EQI_S1']; ?></td>
            <td class="s small <?php echo $c2; ?>"><?php echo $MAT['EQI_S2']; ?></td>
            <td class="eq"><?php echo zk_eq_tn($T_EQI[$MAT['EQI_CLE2']], false); ?></td>
        </tr>
        <?php
    }
}
?>
</table>
</div>

<?php if (v('LIGUEADMIN') !== true) { ?>
<div id="tabs-ligue-1" class="tab-pane">
    <?php
    $y = 0;
    $division = 1;
    $sous_division = 0;
    foreach ($tPoules as $k => $oP) {
        $class_ligue = ($y == 0) ? 'une-poule' : '';
        if ($y >= 1 && $y % 2 == 1) {
            $division++;
            $sous_division = 0;
        }
        $sous_division++;
        ?>
        <div class="par-poule">
            <div align="center" class="interieur">
                <table class="table pageLesMatchs poules_coupe  poule-classement ligue-classement poule-classement-<?php echo $division; ?>"
                       cellpadding="0" cellspacing="0" 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" target="_blank"><?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 && $UN_TNS['TNS_TYPE'] != 7) { ?><!--[[SUP-->
                        <th>&nbsp;</th><!--SUP]]--><?php } ?>
                    </tr>
                    </thead>
                    <?php
                    $der_points = null;
                    $der_classement = 1;
                    $der_aff_classement = '-';
                    $der_ga = null;
                    $i = 0;
                    $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 class="s small"><?php echo $der_aff_classement; ?></td>
                            <td style="text-align: left !important;"><?php echo zk_equipe($eq); ?></td>
                            <td class="s small"><?php echo $eq['JOUE']; ?></td>
                            <td class="s small"><?php echo $eq['G']; ?></td>
                            <td class="s small"><?php echo $eq['N']; ?></td>
                            <td class="s small"><?php echo $eq['D']; ?></td>
                            <td class="s small"><?php echo $eq['GA']; ?></td>
                            <td class="s small PTS"><?php echo $eq['PTS']; ?></td>
                            <?php if (v('fromAdmin') == 1 && v('nb_renc_a_jouer') == 0 && $UN_TNS['TNS_TYPE'] != 7) { ?>
                            <!--[[SUP-->
                            <td class="s 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
                $der_poule = '';
                $i = $k;

                foreach ($T_RENC as $MAT) {
                    //echo '{'.$MAT['MAT_POU'].'/'.$i.'}';
                    if ($MAT['MAT_POU'] != $i) {
                        continue;
                    }
                    $intervention_admin = false; //$MAT['MAT_FLAG']==4||$MAT['MAT_FLAG']==8;
                    $clos_mais_a_terminer = false; //$MAT['MAT_FLAG']==9&&$MAT['MAT_ETAT']==0;

                    $icon = $MAT['MAT_ETAT'] == 1 ? img('icons/tick.png') : img('icons/stop.png');
                    $icon = $MAT['MAT_ETAT'] == 2 ? img('icons/cross_grey.png') : $icon;
                    //$icon = $intervention_admin === false ?$icon:img('icons/error.png');
                    //$icon = $clos_mais_a_terminer?img('icons/eye.png'):$icon;

                    if (zk_est_alert($MAT['MAT_FLAG'], $MAT['MAT_ETAT'])) {
                        $icon = img('icons/error.png');
                        $intervention_admin = true;
                    }
                    if (zk_est_look($MAT['MAT_FLAG'], $MAT['MAT_ETAT'])) {
                        $icon = img('icons/eye.png');
                        $clos_mais_a_terminer = true;
                    }
                    if (sizeof($tPoules) > 1 && $MAT['MAT_POU'] != $der_poule) {

                        ?><table class="pageLesMatchs tabRencontres poule-matchs poule-matchs-<?php echo $i; ?>"
                                 cellpadding="0"
                                 cellspacing="0" border="0">
                        <caption class="lead"><?php echo $tPoules[$MAT['MAT_POU']]['nom']; ?></caption>
                        <?php
                        $der_poule = $MAT['MAT_POU'];
                        $cpt = 0;
                    }
                    if ($MAT['MAT_TOUR'] != $der_tour) {
                        //echo $der_tour==''?'':'</table>';

                        if (($UN_TNS['TNS_TYPE'] == 3 || $UN_TNS['TNS_TYPE'] == 4) && ($MAT['MAT_TOUR'] - 1) == ($nb_tour / 2)) {
                            echo '<hr class="espace" /><h2>Matchs retour</h2>';
                        }

                        ?>
                        <tr>
                            <td colspan="5" align="center"
                                class="sep-matchs"><?php echo __('com.txt.jour'); ?> <?php echo $MAT['MAT_TOUR']; ?></td>
                        </tr>
                        <?php

                        $der_tour = $MAT['MAT_TOUR'];
                    }

                    if ($MAT['MAT_ETAT'] == 1) {
                        $c1 = $MAT['EQI_S1'] != '' && $MAT['EQI_S2'] != '' ? ($MAT['EQI_S1'] == $MAT['EQI_S2'] ? 'egalite' : ($MAT['EQI_S1'] > $MAT['EQI_S2'] ? 'gagnant' : 'perdant')) : '';
                        $c2 = $MAT['EQI_S1'] != '' && $MAT['EQI_S2'] != '' ? ($MAT['EQI_S1'] == $MAT['EQI_S2'] ? 'egalite' : ($MAT['EQI_S1'] < $MAT['EQI_S2'] ? 'gagnant' : 'perdant')) : '';
                    } else {
                        $c1 = '';
                        $c2 = '';
                        $MAT['EQI_S1'] = '&nbsp;';
                        $MAT['EQI_S2'] = '&nbsp;';
                    }

                    ?>
                    <tr class="<?php echo $cpt++ % 2 == 0 ? 'pair' : 'impair'; echo ' '; echo ($MAT['MAT_ETAT'] == 1 || $MAT['MAT_ETAT'] == 2) ? '' : ($MAT['EQI_S1'] != 0 || $MAT['EQI_S2'] != 0 ? 'a_valider' : 'a_jouer'); echo $intervention_admin ? ' alert' : ''; echo $clos_mais_a_terminer ? ' look' : '';echo $MAT['MAT_ETAT'] == 2 ? 'non_joue' : '';  ?>">
                        <td class="s small"><?php echo href($icon, array('controleur' => '[[controleur]]', 'action' => '[[action]]', 'params' => '[[params_' . $MAT['TNS_CLE'] . '-' . $MAT['MAT_FP'] . ']]', 'class' => 'equipe ajax'), false, false, false); ?></td>
                        <td class="eq"><?php echo zk_eq_tn($T_EQI[$MAT['EQI_CLE1']], false); ?></td>
                        <td class="s small <?php echo $c1; ?>"><?php echo $MAT['EQI_S1']; ?></td>
                        <td class="s small <?php echo $c2; ?>"><?php echo $MAT['EQI_S2']; ?></td>
                        <td class="eq"><?php echo zk_eq_tn($T_EQI[$MAT['EQI_CLE2']], false); ?></td>
                    </tr>
                    <?php
                } ?></table>


            </div>
        </div><?php


        ++$y;

        if ($class_ligue != '' || $sous_division == 2) {
            echo '</div><div id="tabs-ligue-' . ($division + 1) . '" class="tab-pane">';
        }
    } ?></div>
<?php } ?>