<?php

if (core::getGet("id") == NULL AND access::ifAccesss("add-lottery") == FALSE) {
    get::page("unknow");
    exit();
}

if (core::getGet("id") == NULL) {
    $titre = "Ajouter un tirage au sort";
} else {
    $lottery = lottery::getFiche(core::getGet("id"));
    if(empty($lottery["id"])){
        get::page("unknow");
        exit();
    }
    
    $badgeCSS = " font-size:0.4em; margin-top:-5px;";
        
    $titre = "[#" . $lottery["id"] . "] " . $lottery["titre"];
    $dataLottery = lottery::getInscriptionData(core::getGet("id"));
}

$tab_fiche = $tab_inscrits = $tab_winners = "false";

if(alert::ifTab()){

    switch (alert::getTab()) {
        case "inscrits-tab":
            $tab_inscrits = "true";
            break;
        case "winners-tab":
            $tab_winners = "true";
            break;
        default:
            $tab_fiche = "true";
            break;
    }
    
} else {
    $tab_fiche = "true";
}

?>

<header class="d-flex flex-column flex-md-row align-items-md-center p-3 bg-light ">

    <div class="row" style="width:100%;">
        <div class="col-11">
            <h2 class="bd-title" id="content">
                <?php echo $titre ?>
            </h2>
        </div>
        <?php if(isset($user["id"]) AND session::getId() != $user["id"]){ ?>
            <div style="float:right; margin: -30px;">
            </div>
            <div class="fix-container-button-nav">
                <a href="/submit.php?from=lottery-delete&id=<?php echo $lottery["id"] ?>" onclick="return confirm('Voulez-vous supprimer ce tirage au sort ?')"><button type="submit" class="btn btn-outline-danger btn-sm"><?php icon::getFont(["icon" => "bi-trash-fill"]) ?></i> Supprimer</button></a>
            </div>
        <?php } ?>
    </div>
</header>
<?php 
        if(core::ifGet("add")) {
            $labelFil = "Ajouter un tirage au sort";
            $lienFil = "/?p=lottery&add=1";
        } else {
            $labelFil = "[#" . $lottery["id"] . "] " . $lottery["titre"];
            $lienFil = "/lottery-".core::getGet("id").".html";
        }
        echo core::filAriane(array(
            "current" => $labelFil, 
            "arbo" => array( 
                "Tirage au sort" => NULL,
                "Listes des tirages au sort" => "/lotterys.html",
                $labelFil => $lienFil)
        )); 
?>
<br />
    <ul class="nav nav-tabs" id="event-fiche" role="tablist">
        <li class="nav-item">
            <a class="nav-link<?php if($tab_fiche == "true"){ echo ' active'; } if(empty($lottery["id"])){ echo " disabled"; } ?>" data-toggle="tab" id="fiche-tab" role="tab" aria-selected="<?php echo $tab_fiche ?>">Fiche du tirage au sort</a>
        </li>
<?php if(isset($lottery["id"])){ ?>
        <li class="nav-item">
            <a class="nav-link<?php if($tab_inscrits == "true"){ echo ' active'; } if(empty($lottery["id"])){ echo " disabled"; } ?>" data-toggle="tab" id="inscrits-tab" role="tab" aria-selected="<?php echo $tab_inscrits ?>">Inscriptions</a>
        </li> 
<?php 
    if($dataLottery["inscrits"] > 0) {
?>   
        <li class="nav-item">
            <a class="nav-link<?php if($tab_winners == "true"){ echo ' active'; } if(empty($lottery["id"])){ echo " disabled"; } ?>" data-toggle="tab" id="winners-tab" role="tab" aria-selected="<?php echo $tab_winners ?>">Tirage au sort</a>
        </li>
<?php } } ?>
    </ul>
<br />

<div class="tab-content">

    <div id="tab-fiche"<?php if($tab_fiche == "false"){ echo ' style="display: none;"'; } ?>> 
        <?php get::page("lottery-fiche") ?> 
    </div>
<?php if(isset($lottery["id"])){ ?>
    <div id="tab-inscrits"<?php if($tab_inscrits == "false"){ echo ' style="display: none;"'; } ?>>
        <?php get::page("lottery-inscrits") ?>
    </div>
<?php 
    if($dataLottery["inscrits"] > 0) {
?>
    <div id="tab-winners"<?php if($tab_winners == "false"){ echo ' style="display: none;"'; } ?>>
        <?php get::page("lottery-winners") ?>
    </div> 
<?php } } ?>
    
</div>

<script>
    $(document).ready(function(){
    
        $("#fiche-tab").click(function() {
            $("#tab-fiche").show();
            $("#tab-inscrits").hide();
            $("#tab-winners").hide();
        });
<?php if(isset($lottery["id"])){ ?>       
        $("#inscrits-tab").click(function() {
            $("#tab-fiche").hide();
            $("#tab-inscrits").show();
            $("#tab-winners").hide();
        });    
        $("#winners-tab").click(function() {
            $("#tab-fiche").hide();
            $("#tab-inscrits").hide();
            $("#tab-winners").show();
        });
<?php } ?>
    })
    
    
</script>

<?php if(isset($lottery["id"]) AND isset($lottery["type_emargement"]) AND $lottery["type_emargement"] != 1){ ?>
<div class="modal fade" id="QRCodeModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-body">
                <?php echo event::getQRCode($lottery["id"], "100%") ?>
            </div>
        </div>
    </div>
</div>
<?php } ?>
