<?php
$UN_TNS = v('UN_TNS');
$T_SQL = v('T_SQL');
$oCompManager = v('oCompManager');
$responsables = ZAuth::getResponsables($UN_TNS['TNS_CLE']);

if (ZAuth::isAuth(9) || !ZAuth::isResponsable()) {
    ?>


<div id="responsables">
    <big>Responsables : </big>
    <?php echo href(img('icons/add.png', 'ajouter'), array('controleur' => 'tabresponsables', 'action' => 'ajouter', 'params' => Admin::$TNS_SEL)); ?>

    <?php if (sizeof((array)$responsables) > 0) {

    foreach ((array)$responsables as $responsable) {
        ?><span class="un_joueur"><?php
            echo zk_joueur($responsable) . href(img('icons/cross.png', 'retirer ce joueur'), array('controleur' => 'tabresponsables', 'action' => 'supprimer', 'params' => $UN_TNS['TNS_CLE'] . '-' . $responsable['JOU_CLE']));
            ?></span><?php

    }

} ?>
</div>

<div id="tn" class="right">

    <div id="choixtournoi">
        <?php

        $choix = zk_choix_tournoi();
        echo $choix;
        ?>
        <?php
                if ($choix != '' && ZK_SEL_TN == -1) {
        ?>
        <div><big>&larr;</big> <?php echo __('com.menu.admin.choix_tn'); ?></div>

        <?php

    } ?>
    </div>
</div>
<?php } ?>
<?php

if (ZK_SEL_TN == -1 || $UN_TNS == null || sizeof($UN_TNS) == 0) {
    ?><h1><?php echo __('adm.err.19'); ?></h1><?php

} else {


    ?>
<h2><?php echo __('adm.tg.titre'); ?> [<u><?php echo decode($UN_TNS['TNS_NOM']); ?></u>]</h2>
<?php

    do_action('etat_tournoi');

    if ($T_SQL['action'] > 0) {
        echo zk_action_tournoi($T_SQL['action'], $T_SQL['etat']);
    }


    switch (intval($UN_TNS['TNS_ETAT'])) {
        case 0: // Inactif
            ?>
            <form action="<?php echo php_self(); ?>" method="post" class="noBorder">
                <input type="hidden" name="action" value="1"/>
                <input type="submit" name="submit" value="<?php echo __('adm.tg.action.activer'); ?>"/>
            </form>
            <?php
                                                        break;
        case 1: // ouvert
            ?>
            <div align="center">
                <form action="<?php echo php_self(); ?>" method="post" class="noBorder">
                    <input type="hidden" name="action" value="2"/>
                    <input type="submit" name="submit" value="<?php echo __('adm.tg.action.ouvrir_insc'); ?>"/>
                </form>
            </div>
            <?php
                                                        break;
        case 2: // Inscription des equipes

            if (sizeof(v('ttab_equipes')) == 0) {
                ?>
            <div class="err" style="margin: 20px auto; text-align: center"><?php echo __('adm.tg.err.nb_eq'); ?>
                <br/><?php echo href(__('adm.tg.action.aj_eq'), array('controleur' => 'tabequipes', 'action' => 'ajouter', '')); ?>
            </div>
            <?php

            }

            ?>
            <h3 align="center"><?php echo __('adm.tg.eq.val'); ?> <?php echo v('NB_EQI_VAL'); ?>
                <?php

                $LIMITE_POUR_INSCRIPTION = $UN_TNS['TNS_DIR'] != '' ? $oCompManager->getMinInscriptionPoule() : poules_limite_pour_inscription($UN_TNS['TNS_NB_EQ_TNS']);

                $LIMITE_POUR_INSCRIPTION = $UN_TNS['TNS_TYPE'] == 3 ? 4 : $LIMITE_POUR_INSCRIPTION;
                $LIMITE_POUR_INSCRIPTION = $UN_TNS['TNS_TYPE'] == 7 ? 8 : $LIMITE_POUR_INSCRIPTION;

                $echoMini = $UN_TNS['TNS_TYPE'] == 1 ?
                        '/ ' . $UN_TNS['TNS_NB_EQ_EVT'] :
                        ($UN_TNS['TNS_TYPE'] == 3 ? '/ ' . __TNS_NB_EQ_CHAMP_MIN . ' (' . __('com.txt.mini') . ')' :
                                '/ ' . $LIMITE_POUR_INSCRIPTION . ' (' . __('com.txt.mini') . ')');

                echo $echoMini;
                ?></h3>
                <?php

            $T_EQI = v('T_EQI');

            if (
                ($UN_TNS['TNS_NB_EQ_TNS'] == 6 && v('NB_EQI_VAL') == 12)
                ||
                ($UN_TNS['TNS_TYPE'] == 1 && $UN_TNS['TNS_NB_EQ_EVT'] == v('NB_EQI_VAL'))
                ||
                (($UN_TNS['TNS_TYPE'] == 2 || $UN_TNS['TNS_TYPE'] == 4 || $UN_TNS['TNS_TYPE'] == 7) && v('NB_EQI_VAL') >= $LIMITE_POUR_INSCRIPTION)
                ||
                ($UN_TNS['TNS_TYPE'] == 3 && v('NB_EQI_VAL') >= __TNS_NB_EQ_CHAMP_MIN)
            ) {
                ?>
            <div align="center">
                <form action="<?php echo php_self(); ?>" method="post" class="noBorder">
                    <input type="hidden" name="action" value="3"/>
                    <input type="submit" name="submit" value="<?php echo __('adm.tg.action.fermer_insc'); ?>"/>
                </form>
            </div>
            <?php

            } else if (
                ($UN_TNS['TNS_TYPE'] == 1 && $UN_TNS['TNS_NB_EQ_EVT'] < v('NB_EQI_VAL'))
            ) {
                ?>
            <h3><?php echo sprintf(__('adm.tg.err.nb_eq.max'), $UN_TNS['TNS_NB_EQ_EVT'], (v('NB_EQI_VAL') - $UN_TNS['TNS_NB_EQ_EVT'])); ?></h3>
            <?php

            } else {
                ?><?php

            }


            if (sizeof(v('ttab_equipes')) > 0) {
                if (!($UN_TNS['TNS_TYPE'] == 1 && $UN_TNS['TNS_NB_EQ_EVT'] == v('NB_EQI_VAL'))) {
                    echo form('form', array('method' => "post", 'legend' => "<?php echo __('adm.tg.action.insc_eq'); ?>", "class" => "tabEdition noBorder"), false, true, false); ?>
                <table class="table-css table-css-noborder table-css-400px">
                    <?php
                                                                                        if (v('erreur')) {
                    ?>
                    <tr>
                        <td colspan="2" class="erreurs"><?php echo display_erreurs(); ?></td>
                    </tr>
                    <?php } ?>
                    <tr>
                        <td class="g"><label for="Equipe"><?php echo __('com.txt.equipe'); ?></label></td>
                        <td class="d"><?php echo formSelect('EQI_CLE', array(), v('ttab_equipes'), false, false); ?></td>
                    </tr>
                    <tr valign="top">
                        <td class="validation"
                            colspan="2"><?php echo formSubmit(__('com.action.val'), array('onclick' => 'this.style.display=\'none\'')); ?></td>
                    </tr>
                </table>
                <?php echo formClose('form');
                }
            }
            ?>

                <?php
                                                                    $tEqiInscrites = v('tEqiInscrites');
            global $cfg_tEquipeVal;
            if (sizeof($tEqiInscrites) > 0) {
                $der_k = null;
                foreach ($tEqiInscrites as $k => $t) {
                    if ($der_k == null) {
                        $der_k = $t['EQI_VAL'];
                        ?>
                    <hr class="espace"/><u><?php echo zk_equipe_val($der_k); ?>s</u> :
				<ul class="liste_equipes">
				<?php

                    } else {
                        if ($der_k == $t['EQI_VAL']) {
                            //rien
                        } else {
                            $der_k = $t['EQI_VAL'];
                            ?>
					</ul>
					<hr class="espace"/><u><?php echo zk_equipe_val($der_k); ?>s</u> :
					<ul class="liste_equipes">
					<?php

                        }
                    }
                    ?>
                    <li><?php

                        if ($t['EQI_VAL'] != 0) {
                            if ($t['EQI_VAL'] == 1) {
                                echo href_ajax(img('icons/stop.png'), 'admintournoigereraccepterequipe', $k);
                            }
                            if ($t['EQI_VAL'] == 2) {
                                echo href_ajax(img('icons/accept.png'), 'admintournoigererrefuserequipe', $k);
                            }
                        } else {
                            echo href_ajax(img('icons/accept.png'), 'admintournoigereraccepterequipe', $k);
                            echo href_ajax(img('icons/stop.png'), 'admintournoigererrefuserequipe', $k);
                        }
                        echo zk_equipe(array('EQI_CLE' => $k, 'EQI_NOM' => $t['nom'])) . ' - <small>(' . zk_equipe_val($t['EQI_VAL'], true) . ')</small>';

                        ?></li><?php

                }
                ?></ul><?php

            } else {
                ?><h3 align="center"><?php echo __('adm.tg.no_eq'); ?></h3><?php

            }

            ?>
            <div class="clear">&nbsp;</div>
                <?php
                                                                        break;
        case 3: // Inscriptions closes
            ?>
            <div align="center">
                <form action="<?php echo php_self(); ?>" method="post" class="noBorder">
                    <input type="hidden" name="action" value="<?php
                    switch ($UN_TNS['TNS_TYPE']) {
                        case 1:
                            echo 4;
                            break;
                        case 7:
                        case 2:
                            echo 5;
                            break;
                        case 3:
                        case 4:
                            echo 7;
                            break;
                    }
                    ?>"/>

                                                              <?php
                                                                                                                                                                                                                                      if ($UN_TNS['TNS_TYPE'] == 2 || $UN_TNS['TNS_TYPE'] == 7) {
                    ?><h2><?php echo __('adm.ta.comp.nb_p'); ?></h2>
                    <div style="width: 400px; margin: 0px auto; text-align: left">
                        <?php
                                                                                                    $t_solutions = $oCompManager->poules_get_structure($UN_TNS['TNS_NB_EQ_EVT'], $UN_TNS['TNS_NB_EQ_TNS']);
                        $checked = false;
                        if (sizeof($t_solutions) == 0) {
                            die(__('adm.err.20'));
                        }
                        foreach ($t_solutions as $nb_poules => $t) {
                            ?><input type="radio" name="TNS_NB_POU"
                                     value="<?php echo $nb_poules; ?>" <?php echo $checked ? '' : 'checked="checked"'; ?> />
                            <?php
                                                                                                                    echo sprintf(__('adm.tg.poule.1'), ($t[0]['nb_pou'] + $t[1]['nb_pou']), $t[0]['nb_pou'], $t[0]['nb_eq']);
                            echo $t[1]['nb_pou'] > 0 ? sprintf(__('adm.tg.poule.2'), $t[1]['nb_pou'], $t[1]['nb_eq']) . '<br />' : ')<br />';
                            $checked = $checked ? $checked : !$checked;
                        }
                        ?></div><br/><?php

                }

                    ?>

                    <?php if ($UN_TNS['TNS_TYPE'] == 3 || $UN_TNS['TNS_TYPE'] == 4) { ?>
                    <input type="checkbox" value="1" checked="checked" name="TNS_RETOUR"/> avec match retour ?<br />
                    <?php } ?>
                    <input type="submit" name="submit" value="<?php echo __('adm.tg.action.com_renc'); ?>"/>
                </form>
            </div>
                                                              <?php
                                                                                                                                                                                                                                  break;
        case 4: /* Organisation des rencontres TOURNOIS */
        case 5: /* Organisation des rencontres POULE */
            $T_EQI = v('T_EQI');

            $nb_eq = 0;
            foreach ($T_EQI as $k => $v) {
                //$T_EQI[$k] = shuffle($v);
                $nb_eq += sizeof($v);
            }
            //echo 'nb_eq : '.$nb_eq.'<br />';
            if (v('erreur')) {
                echo display_erreurs();
            }

            if ($nb_eq != $UN_TNS['TNS_NB_EQ_EVT']) {
                ?><h3><?php echo __('adm.err.21'); ?></h3><?php

            } else {

                ?>
            <style type="text/css">
                .sortable {
                    list-style-type: none;
                    margin: 0 auto;
                    padding: 0;

                }

                .sortable li {
                    display: block;
                    padding: 5px;
                }

                .colonne {
                    width: <?php echo $UN_TNS['TNS_ETAT'] == 4 ? '300px' : '150px'; ?>;
                    vertical-align: top;
                }

                .colonne input {
                    width: 100%;
                    margin: 0px;
                    text-align: center;
                }

                .a_trier {
                    font-size: <?php echo $UN_TNS['TNS_ETAT'] == 4 ? '12px' : '10px'; ?> !important;
                    width: <?php echo $UN_TNS['TNS_ETAT'] == 4 ? '300px' : '150px'; ?>;
                }
            </style>



            <p style="text-align:center"><?php echo __('adm.tg.drdp'); ?>
                <?php if ($UN_TNS['TNS_ETAT'] != 4) { ?>
                    , <?php echo __('adm.tg.chp'); ?>
                    <?php } ?>
                .</p>

            <form action="<?php echo php_self(); ?>" method="post" onsubmit="maj_liste();">
                <?php
                                                        if ($UN_TNS['TNS_TYPE'] == 7) {
                $division = 1;
                $sous_division = 0;
                ?>
                <ul class="classement-ligue"><?php
            for ($i = 0; $i < v('nbColonnes'); $i++) {
                    $class_ligue = ($i == 0) ? 'une-poule' : '';
                    if ($i >= 1 && $i % 2 == 1) {
                        $division++;
                        $sous_division = 0;
                    }
                    $sous_division++;
                    ?>
                    <li class="pou <?php echo $class_ligue; ?>">
                        <input class="nom-poule" type="text" name="nom_poule[<?php echo $i; ?>]"
                               value="<?php echo __('com.txt.poule'); ?> <?php echo $division . '.' . $sous_division; ?>"/>
                        <ul class="connectedSortable sortable">
                            <?php
                                                                                                                                            foreach ($T_EQI[$i] as $k => $UNE_EQI) {
                            ?>
                            <li class="ui-state-highlight a_trier s<?php echo $i; ?>"
                                id="<?php echo $UNE_EQI['EQI_CLE']; ?>"><a
                                    name="<?php echo $UNE_EQI['EQI_CLE']; ?>"><?php echo zk_equipe($UNE_EQI, false); ?></a>
                            </li><?php

                        }
                            ?>
                        </ul>
                        <input type="hidden" name="RENC[<?php echo $i; ?>]" id="RENC_<?php echo $i; ?>" value=""/>
                    </li>
                    <?php } ?>
                </ul>
                <div style="clear:both; width: 1px; height: 1px; display: block;"></div>
                
                <input type="submit" name="submit" value="<?php echo __('adm.tg.action.com_comp'); ?>"/>

                                             <?php

            } else {
                ?>
		
		
		<table id="orga_rencontres" cellspacing="10">
			<tr>
                <?php if ($UN_TNS['TNS_ETAT'] == 4) { ?>
                <td class="colonne"><?php echo __('com.txt.equipe'); ?> A</td>
                <td class="small">&nbsp;<?php echo __('com.txt.vs'); ?>&nbsp;</td>
                <td class="colonne"><?php echo __('com.txt.equipe'); ?> B</td>
                <?php } else { ?>
                <?php
                                    for ($i = 0; $i < v('nbColonnes'); $i++) {
                    if ($UN_TNS['TNS_ETAT'] != 4 && v('nbColonnes') > 4 && $i == 4) {
                        break;
                    }
                    ?>
                    <td class="colonne">
                        <input type="text" name="nom_poule[<?php echo $i; ?>]"
                               value="<?php echo __('com.txt.poule'); ?> <?php echo chr(65 + $i); ?>"/>
                    </td><?php

                }
                ?>
                <?php } ?>
            </tr>
			<tr>
				<?php
                for ($i = 0; $i < v('nbColonnes'); $i++) {
                    if ($UN_TNS['TNS_ETAT'] == 4 && $i == 1) {
                        ?>
                        <td class="small"><?php echo __('com.txt.vs'); ?></td><?php

                    }

                    if ($UN_TNS['TNS_ETAT'] != 4 && v('nbColonnes') > 4 && $i == 4) {
                        ?></tr></table><table id="orga_rencontres" cellspacing="10"><tr><?php
                        for ($ii = 4; $ii < v('nbColonnes'); $ii++) {
                            ?>
                            <td class="colonne">
                                <input type="text" name="nom_poule[<?php echo $ii; ?>]"
                                       value="<?php echo __('com.txt.poule'); ?> <?php echo chr(65 + $ii); ?>"/>
                            </td><?php

                        }
                            ?></tr><tr><?php

                    }
                    ?>
                    <td class="colonne">
                        <ul class="connectedSortable sortable">
                            <?php
                                                                                                                                                    foreach ($T_EQI[$i] as $k => $UNE_EQI) {
                            ?>
                            <li class="ui-state-highlight a_trier s<?php echo $i; ?>"
                                id="<?php echo $UNE_EQI['EQI_CLE']; ?>"><a
                                    name="<?php echo $UNE_EQI['EQI_CLE']; ?>"><?php echo zk_equipe($UNE_EQI, false); ?></a>
                            </li><?php

                        }
                            ?>
                        </ul>
                        <input type="hidden" name="RENC[<?php echo $i; ?>]" id="RENC_<?php echo $i; ?>" value=""/>
                    </td>
                            <?php

                } ?>
			</tr>
			<tr>
                <td colspan="<?php echo $UN_TNS['TNS_TYPE'] == 1 ? v('nbColonnes') + 1 : v('nbColonnes'); ?>"
                    align="center" style="padding: 10px 0px;">
                    <?php if ( $UN_TNS['TNS_TYPE'] == 2 ){ ?>
                    <input type="checkbox" value="1" name="TNS_RETOUR"/> avec match retour ?<br />
                    <?php } ?>
                    <input type="submit" name="submit" value="<?php echo __('adm.tg.action.com_comp'); ?>"/>
                </td>
            </tr>
			</table>
			<?php } ?>
            </form>

            <script type="text/javascript">
                <!--

        $(window).load(function(){

            $(".a_trier").draggable({
                helper: 'clone',
                revert: 'invalid',
                stop: function(event, ui){
                    maj_liste();
                }
            });
            $(".a_trier").droppable({
                drop: function(event, ui) {
                    var idDest = $(this).attr("id");
                    var idSrc = $(ui.draggable).attr("id");

                    var txt = "";
                    txt = $(this).html();

                    $(this).html($(ui.draggable).html());
                    $(ui.draggable).html(txt);
                }
            });
            function maj_liste(){

                var html = '';
                <?php
                    for ($i = 0; $i < v('nbColonnes'); $i++) {
                    ?>$(".s<?php echo $i; ?>").each(function(i){
							html += $(this).children("a").attr("name")+',';
						});
						$("#RENC_<?php echo $i; ?>").attr("value",html);
						html = '';
						<?php

                }
                    ?>
				}
				maj_liste();
			});

			-->
            </script>

                <?php

            }

            break;
        case 6: /* Tournoi en cours */
            ?>
            <div align="right">
                <form method="post" action="<?php echo php_self(); ?>"
                      style="margin: 0px auto; padding: 0px; border: 0px none">
                    <input type="hidden" value="stop" name="stop"/>
                    <input type="submit" name="submit" value="Arreter le tournoi avant la fin"/>
                </form>
            </div>
            <?php
                                                        echo $oCompManager->getTournoi(true);

            ?>

            <script type="text/javascript">
                $(window).load(function() {
                    $("#box_ui").hide();
                    $('.blockOverlay').attr('title', 'Fermer').click($.unblockUI);


                    $('a.equipe').click(function(e) {
                        e.preventDefault();
                        $("#box_ui").html("<?php echo zk_loading(); ?>");

                        $.ajax({
                            url: this.href,
                            cache: false,
                            success: function(html) {
                                $("#box_ui").html(html);
                            }
                        });

                        $.blockUI({
                            message: $('#box_ui'),
                            css: { top: '10%', width: '' }
                        });


                        return false;
                    });
                });

            </script>
            <?php
                                                        break;
        case 7: /* Championnat en cours */
        case 8: /* Coupe en cours */
            if (v('erreur')) {
                echo display_erreurs();
            }

            ?>
            <style type="text/css">
                .tabRencontres {
                    width: 300px;
                    float: left;
                    font-size: 12px;
                    border: 1px solid black;
                    margin: 20px;
                }

                .tabRencontres tr td.eq {
                    width: 150px;
                    padding-left: 5px;
                }

                .tabRencontres tr.pair td {
                    background: #fafafa;
                    border-bottom: 1px solid #d0d0d0;
                }

                .tabRencontres tr.impair td {
                    background: #ececec;
                    border-bottom: 1px solid #c0c0c0;
                }

                .tabRencontres tr.joue td {
                    background: #caffca !important;
                }

                .tour {
                    background: #f0f0f0;
                    text-align: center;
                    border: 1px solid black;
                    font-size: 22px;
                    font-weight: bold;
                    height: 40px;
                }

                .poule {
                    background: #d0d0d0;
                    text-align: center;
                }

                .small {
                    width: 30px;
                    padding: 0px;
                    text-align: center;
                    border-left: 1px solid #b0b0b0;
                    border-right: 1px solid #b0b0b0;
                }

                .a_jouer td {

                }

                .a_valider td {
                    background: #fecec9 !important;
                }

                .winner {
                    font-weight: bold;
                }

            </style>

            <?php
                                                        if (v('nb_renc_a_jouer') === 0 && ($UN_TNS['TNS_TYPE'] != 3 && $UN_TNS['TNS_TYPE'] != 7)) {
                ?><p style="text-align:center"><?php echo __('adm.tg.txt2'); ?></p><?php

            }
            ?>
            <p style="text-align:center"><?php sprintf(__('adm.tg.txt3'), img('icons/stop.png', 'stop'), img('icons/tick.png', 'ok')); ?></p>
            <form method="post" action="<?php echo php_self(); ?>">

                <?php if (v('nb_renc_a_jouer') === 0) {
                if ($UN_TNS['TNS_TYPE'] == 2) {
                    ?><h2><?php echo __('adm.tg.txt4'); ?></h2>
                    <input type="submit" name="submit" value="<?php echo __('adm.tg.action.clore_cou'); ?>"/>
                    <small><?php echo __('adm.tg.txt2'); ?></small>
                    <?php

                } else if ($UN_TNS['TNS_TYPE'] == 3) {
                    //Clore le championnat
                    ?>
                    <h2><?php echo __('adm.tg.champ_clos'); ?></h2>
                    <input type="hidden" name="action" value="9"/>
                    <div align="center"><input type="submit" name="submit"
                                               value="<?php echo __('adm.tg.action.clore_champ'); ?>"/></div>
                    <?php

                } else if ($UN_TNS['TNS_TYPE'] == 4) {
                    //Clore le championnat et démarrer le tournoi
                    ?>
                    <h2><?php echo __('adm.tg.champ_clos_tn'); ?></h2>
                    <input type="hidden" name="action" value="9"/>
                    <div align="center"><input type="submit" name="submit"
                                               value="<?php echo __('adm.tg.action.clore_champ_tn'); ?>"/></div>
                    <?php

                } else if ($UN_TNS['TNS_TYPE'] == 7) {
                    ?>
                    <div align="center"><input type="submit" name="submit"
                                               value="<?php echo __('adm.cloturertournoi.txt'); ?>"/></div><?php

                }
            } ?>

            <?php
                                                            if ($UN_TNS['TNS_ETAT'] == 7) {
                echo $oCompManager->getChampionnat(true);
            } else {
                if ($UN_TNS['TNS_TYPE'] == 7) {
                    echo $oCompManager->getLigue(true);
                } else {
                    echo $oCompManager->getCoupe(true);
                }
            }
                ?>

            </form>

            <script type="text/javascript">
                $(window).load(function() {
                    $("#box_ui").hide();
                    $('.blockOverlay').attr('title', 'Fermer').click($.unblockUI);


                    $('a.equipe').click(function(e) {
                        e.preventDefault();
                        $("#box_ui").html("<?php echo zk_loading(); ?>");

                        $.ajax({
                            url: this.href,
                            cache: false,
                            success: function(html) {
                                $("#box_ui").html(html);
                            }
                        });

                        $.blockUI({
                            message: $('#box_ui'),
                            css: { top: '10%', width: '' }
                        });


                        return false;
                    });
                });

            </script>

            <?php
                                                        break;
        case 9: /* Historique */
            break;
        default:
            break;
    }
}
?>