

<!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('supp' == $_GET['msg'])
    {
    ?>
    <script type='text/javascript'>
        iziToast.success({
            title: '',
            message: "Photo supprimé avec succes",
            timeout: 5000,
            transitionOut: 'flipOutX'
        });
    </script>
<?php }
else{
?>
        <script type='text/javascript'>
            iziToast.success({
                title: '',
                message: "Photo téléchargée avec succes",
                timeout: 5000,
                transitionOut: 'flipOutX'
            });
        </script>
    <?php
}
} ?>



<?php
require_once("db/functions.php");
require_once("db/db-const.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;

/*
 *
$page = $_GET["page"] ? $_GET["page"] : 1;

$sql = "SELECT * from image_table limit ("+$page+"-1)*9,9";*/

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

$type = $_GET["type"];
$id = $_GET["id"];


if (logged_in() != true)
    $clause = "AND audience = 'public'";
else
    $clause = "";


$sql = "SELECT * from image_table WHERE typePhototheque = '$type' AND idTypeImage = '$id' $clause ORDER BY id DESC limit $offset,$size ";

$result = $mysqli->query($sql);

$countImages = array_values(mysqli_fetch_array($mysqli->query("SELECT COUNT(*) as total FROM image_table  WHERE typePhototheque = '$type' AND idTypeImage = '$id'")))[0];

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


?>

<div id="page-content">
    <div class="content-wrapper">
        <div class="container">
            <?php if ($countImages > 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 target="_blank" href="UploadedImages/<?= $row["name"] ?>">
                                        <img class="example-image" src="UploadedImages/<?= $row["name"] ?>" alt=""/>
                                    </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/imageDelete.php?image=<?=$row["name"]?>&type=<?=$_GET["type"]?>&page=<?=isset($_GET["page"]) ? $_GET["page"] : 1?>&id=<?=$_GET["id"]?>
                                    "
                                   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="images.php?type=<?= $_GET["type"] ?>&id=<?= $_GET["id"] ?>&page=<?= $page ?>">
                                    <?= $page ?>
                                    <?php } ?>

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


            <?php } else { ?>

                <h2 class="center "> Aucune photo ! </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 photo ?",
            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>
</body>
</html>