<?php
require_once("db/functions.php");
require_once("db/db-const.php");

//TODO
//if (logged_in() != true) {
//    redirect_to("index.php");
//}

$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
    echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
    exit();
}

$currPage = isset($_GET["page"]) ? $_GET["page"] : 1;

$offset = ($currPage - 1) * 9;
$size = 9;


    $countPres = 0;
    $sql = "SELECT * from presentation_table ORDER BY id DESC limit $offset,$size ";
    $result = $mysqli->query($sql);
    $countPres = array_values(mysqli_fetch_array($mysqli->query("SELECT COUNT(*) as total FROM presentation_table")))[0];

    $countPages = floor(($countPres - 1) / 9 + 1);



?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<?php include 'includes/headImages.php'; ?>
<body>
<?php include 'includes/header.php'; ?>
<!-- LOGO HEADER END-->
<?php include 'includes/navbar.php'; ?>
<!-- MENU SECTION END-->
<?php
if (isset($_GET['msg'])) {
    if ("1" == $_GET['msg']) {
        ?>
        <script type='text/javascript'>
            iziToast.success({
                title: '',
                message: "Présentation téléchargée avec succes",
                timeout: 5000,
                transitionOut: 'flipOutX'
            });
    </script>
<?php }} ?>

<div id="page-content">
    <div class="content-wrapper">
        <div class="container">
            <?php if ($countPres > 0) { ?>


                <div id="main-content">
                <!--<div class="row">-->
                <?php $i = 0;
                while ($row = $result->fetch_assoc()) {

                    if ($i % 3 == 0) {
                        if ($i != 0) { ?>
                            </div>
                        <?php } ?>
                        <div class="row">
                    <?php } ?>

                    <div class="col-md-4">

                        <article class="box-shadow text-center">
                            <div class="art-header">
                                <div class="zoom-container">
                                    
                                    <a href="<?=$row["folder"].$row["name"]?>" download="<?=$row["titre"]."_".$row["name"]?>">
                                        <img src="img/download.png"/>
                                    </a>


                                </div>
                                <div class="comment-user"><i class="fa fa-user"></i>
                                    <?=
                                    getUserById($row["userId"])['first_name'] . " " .
                                    getUserById($row["userId"])['last_name']
                                    ?>
                                    <br><i class="glyphicon glyphicon-map-marker"></i>
                                    Cabinet : <?= getUserById($row["userId"])['cabinet'] ?>
                                </div>
                            </div>
                            <div class="art-content">
                                <br>
                                <br>
                                <h3><?= $row["titre"] ?></h3>
                                <p><?= $row["description"] ?></p>
                                <?php if(isset($_SESSION['user_id']) && ($row["userId"] == $_SESSION['user_id'])){?>

                                    <a type="button"
                                       id="deleteImage"
                                       href="db/presentationDelete.php?id=<?=$row["id"]?>&name=<?=$row["name"]?>&page=<?=$currPage?>"
                                       class="confirmation btn btn-danger remove-img">Supprimer</a>
                                <?php } ?>

                            </div>
                        </article>
                    </div>
                    <?php $i++;
                } ?>
                </div>

                <!--Pagination-->
                <?php if ($countPages > 1) { ?>
                    <div class="text-center">
                        <ul class="pagination">
                            <?php for ($page = 1;
                            $page <= $countPages;
                            $page++){ ?>
                            <li <?php if ($page == $currPage) { ?>
                                class="active"
                            <?php } ?>
                            >
                                <a href="presentations.php?page=<?= $page ?>">
                                    <?= $page ?>
                                    <?php } ?>

                                </a>
                            </li>
                        </ul>
                    </div>
                <?php } ?>


            <?php } else { ?>

                <h2 class="center "> Aucune presentation ! </h2>

            <?php } ?>
        </div>

    </div>
</div>
</div>
<!-- CONTENT-WRAPPER SECTION END-->
<?php include 'includes/footer-scripts.php';

$mysqli->close(); ?>
<script>
    $('.confirmation').on('click', function (e) {
        e.preventDefault();
        href = $(this).attr('href');
        bootbox.confirm({
            title: "Confirmation",
            message: "Voulez-vous vraiment supprimer cette presentation ?",
            buttons: {
                cancel: {
                    label: '<i class="fa fa-times"></i> Non'
                },
                confirm: {
                    label: '<i class="fa fa-check"></i> Oui'
                }
            },
            callback: function (result) {
                if(result)
                    window.location = href
            }
        });
    });
</script>

<?php if (isset($_GET['msg'])) {
    if ( "supp" == $_GET['msg']) { ?>
    <script type='text/javascript'>
        iziToast.success({
            title: ' ',
            message: "Votre presentation a été supprimé",
            timeout: 5000,
            transitionOut: 'flipOutX'
        });
    </script>
    <?php }
    } ?>

</body>
</html>