<?php
include('sections/head.php')
?>
<!-- ======================================================================
                                        START HEADER
    ======================================================================= -->
<?php
include('sections/header.php');
?>
<!-- ======================================================================
                                        END HEADER
    ======================================================================= -->
<?php
if (isset($_GET['tid'])) {
    $tid = $_GET['tid'];
    $found_type_topic = Type_topic::find_by_id($tid);
    //$tid = $found_topic->topic_typetopic_id;
}
?>
<div class="page-title-area">
    <div class="container">
        <div class="page-title-content">

            <h2><?php echo $found_type_topic->type_topic_desc ?></h2>
            <ul>
                <li><a href="index.php">Accueil</a></li>
                <li><?php echo $found_type_topic->type_topic_desc ?></li>
            </ul>
        </div>
    </div>
</div>
</br></br>

<section class="default-news-area">
    <div class="container">
        <div class="row">
            <?php
                $page = !empty($_GET['page']) ? (int) $_GET['page'] : 1;
                $limitPerPage = 14;
                $offset = ($page - 1) * $limitPerPage;
                //$found_topics = Topics::find_by_typetopic_pagination($tid, $offset, $limitPerPage);
                //$total_count = Topics::countTotalfind_by_typetopic_pagination($tid);
                $found_topics = Topics::find_by_typetopic_current_year_pagination($tid, $offset, $limitPerPage);
                $total_count = Topics::countTotalfind_by_typetopic_current_year_pagination($tid);
                $totalPages = ceil($total_count / $limitPerPage);
            ?>
            <div class="col-lg-8">

                <div class="row">
                    <?php
                    foreach ($found_topics as $found_topic) {
                        $found_topic_type = Type_topic::find_by_id($found_topic->topic_typetopic_id); //type topic correspondant
                        $galeries = explode(",", $found_topic->topic_image_path);
                        if (!empty($found_topic->topic_url)) {
                            $url = $found_topic->topic_url;
                        } else {
                            $url = 'u';
                        }

                        $nbr_likes = $found_topic->topic_likes ?? 0;
                        $nbr_dislikes = $found_topic->topic_dislikes ?? 0; 
                        $nbr_shares = $found_topic->topic_shares ?? 0; 
                        $nbr_commentaire = Comment_topic::count_valid_by_topic($found_topic->topic_id);
                      
                        if(!empty($galeries[0])){
                            $image_path = $repository. $galeries[0];
                            if (file_exists($image_path)) {
                    ?>

                        <div class="col-lg-6 col-md-6">
                            <div class="single-most-popular-news">
                                <div class="popular-news-image">
                                    <a href="singlepost-<?= $url ?>-<?= $found_topic_type->type_topic_id ?>-<?= $found_topic->topic_id ?>">
                                        <img src="<?= $repository . $galeries[0] ?>" alt="<?= $found_topic->topic_title ?>" style="width: 450px; height: 250px">
                                    </a>
                                </div>
                                <div class="popular-news-content">
                                    <span>
                                        <?= $found_topic_type->type_topic_desc?>
                                    </span>
                                    <h3>
                                        <a href="singlepost-<?= $url ?>-<?= $found_topic_type->type_topic_id ?>-<?= $found_topic->topic_id ?>"><?= $found_topic->topic_title ?></a>
                                    </h3>
                                    <p>
                                        <span style="margin-right: 15px;"><i class="fa fa-calendar"></i> <?= changedateusfr($found_topic->topic_date_creation) ?></span>
                                        <span style="margin-right: 8px;"> <i class="fas fa-thumbs-up"></i> <?= ($nbr_dislikes+$nbr_likes) ?></span>
                                        <span style="margin-right: 8px;"> <i class="fa fa-share-alt"></i> <?= $nbr_shares ?></span>
                                        <span style=""> <i class="fa fa-comments"></i> <?= $nbr_commentaire ?></span>
                                    </p>
                                </div>
                            </div>
                        </div>
                    <?php
                                }
                            }
                        }
                    ?>
                </div>
                <div class="pagination-area">
                    <?php
                     $pagination = new Pagination($page, $limitPerPage, $total_count);
                    $type_topic = Type_topic::find_by_id($tid);
                    $url_nav = sanitize_url($type_topic->type_topic_desc);
                    $url_nav = replace_accents($url_nav);
                    ?>
                    <?php
                    if ($pagination->has_previous_page()) {
                        echo '<a class="prev page-numbers" href="post-' . $url_nav . '-' . $pagination->previous_page() . '-' . $type_topic->type_topic_id . '"><i class="bx bx-chevron-left"></i></a>';
                    }
                    ?>
                    <?php
                    if ($pagination->total_pages() > 1) {

                        for ($i = 1; $i <= $pagination->total_pages(); $i++) {
                            if ($i == $page) {

                                echo "<a class='page-numbers current' href=\"post-{$url_nav}-{$i}-{$type_topic->type_topic_id}\">{$i}</a>";
                            } else {
                                echo "<a class=\"page-numbers\" href=\"post-{$url_nav}-{$i}-{$type_topic->type_topic_id}\">{$i}</a>";
                            }
                        }
                    }
                    ?>
                    <?php
                    if ($pagination->has_next_page()) {
                        echo '<a class="prev page-numbers" href="post-' . $url_nav . '-' . $pagination->next_page() . '-' . $type_topic->type_topic_id . '"><i class="bx bx-chevron-right"></i></a>';
                    }
                    ?>
                </div>
            </div>

            <div class="col-lg-4">
                <!-- =====================================================================
                                                 START SIDEBAR
                ====================================================================== -->
                <?php include('sections/sidebar.php') ?>
                <!-- =====================================================================
                                                 END SIDEBAR
                ====================================================================== -->

            </div>
        </div>
    </div>
</section>
<!-- =====================================================================
                                           START FOOTER
         ====================================================================== -->
<?php include 'sections/footer.php'; ?>
<!-- =====================================================================
                                           END FOOTER
          ====================================================================== -->