<?php    $dataLottery = lottery::getInscriptionData(core::getGet("id"));
    $sortLottery = lottery::getCloture(core::getGet("id"));


    $jsonTarget = "/json.php?jsonData=lottery-winners&id=" . core::getGet("id");
    if(debug::isFile("debug")){
        debug::log(debug::getBadge($jsonTarget, "OUVRIR LE JSON : ".$jsonTarget), "JSON chargé en arrière plan");
    } 

if($sortLottery["sortDate"] == NULL) {
?>
<div class="card text-center">
    <div class="card-header" style="font-size: 1.3em;">
    Combien de personnes doivent être tirées au sort ?
    </div>
    <div class="card-body">

        <form method="post" action="/submit.php">

            <input type="hidden" name="from" value="lottery-winners">
            <input type="hidden" name="lottery" value="<?php echo core::getGet("id") ?>">

            <div class="form-group">
                <label>Saisissez un nombre entre 1 et <?php echo $dataLottery["eligible"] ?></label>
                <input type="number" pattern="\d*" min="1" max="<?php echo $dataLottery["eligible"] ?>" style="font-size:30px" value="" class="form-control text-center" name="nb_winners" required>
            </div>
            <br />
            
            <input class="btn btn-primary btn-lg" style="width: 100%" type="submit" value="Tirer au sort">

        </form>
    </div>
</div>
<?php
} else {
?>
<div class="card text-center" style="border-color:green;">
    <div class="card-header" style="font-size: 1.3em; background-color:#d4edda; color:green;">
    Résultat du tirage au sort
    </div>
    <div class="card-body">
        <div class="row">
            <div class="col">
                <label style="color: gray;">Date du tirage au sort</label>
                <input type="text" value="<?php
                                            if (isset($sortLottery["sortDate"])) {
                                                echo $sortLottery["sortDate"];
                                            }
                                            ?>" class="form-control text-center" disabled>
            </div>
            <div class="col">
                <label style="color: gray;">Réalisé par</label>
                <input type="text" value="<?php
                                            if (isset($sortLottery["sortBy"])) {
                                                echo $sortLottery["sortBy"];
                                            }
                                            ?>" class="form-control text-center" disabled>
            </div>
            <div class="col">
                <label style="color: gray;">Paramètre</label>
                <input type="text" value="<?php
                                            if (isset($sortLottery["sortNb"])) {
                                                if($sortLottery["sortNb"] > 1){ echo $sortLottery["sortNb"] . " gagnants"; }
                                                else { echo $sortLottery["sortNb"] . " gagnant"; }
                                            }
                                            ?>" class="form-control text-center" disabled>
            </div>
        </div>
        <br />
        <table
            id="table"
            class="table-striped table-hover table-sm" 
            data-page-size="25"
            data-toggle="table"
            data-buttons-align="left"
            data-pagination="true"
            data-filter-control="true"
            data-flat="true"
            data-url="<?php echo $jsonTarget ?>">
            <thead>
                <tr>
                    <th data-sortable="true" data-field="id_dossier" data-filter-control="input" data-width="50" data-formatter="selectDossier">Id Dossier</th>
                    <th data-sortable="true" data-field="id_salarie" data-filter-control="input" data-width="50">Id Salarié</th>
                    <th data-sortable="true" data-field="login" data-filter-control="input">Login</th>
                    <th data-sortable="true" data-field="prenom" data-filter-control="input">Prénom</th>
                    <th data-sortable="true" data-field="nom" data-filter-control="input">Nom</th>
                    <th data-sortable="true" data-field="selected">Date du tirage</th>
                </tr>
            </thead>
        </table> 

    </div>
</div>

<script>
    function selectDossier(value) { 
        return '<a href="https://www.cse-invent.com/gestion/prestations/dossiers/' + value + '/edit?pagesize=20&page=1" target="_blank"><button type="submit" class="btn btn-outline-primary btn-sm">' + value + '</button></a>';
    }
    function selectPrestation(value, row) { 
        return '<a href="https://www.cse-invent.com/gestion/prestations/' + value + '/edit?search=avance&page=1&pagesize=20" target="_blank"><button type="submit" class="btn btn-outline-primary btn-sm">' + value + '</button></a>';
    }
</script>

<?php
}
?>