<?php include('sections/head.php') ?>

<body>
    <?php include('sections/header.php') ?>
    
    <?php
    if (isset($_GET['tid'])) {
        $tid = $_GET['tid'];
    }
    if (isset($_GET['year'])) {
        $year = $_GET['year'];
    }

    ?>

    <div class="page-title-area">
        <div class="container">
            <div class="page-title-content">
                <?php $found_type_topic = Type_topic::find_by_id($tid) ?>
                <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 = 10;
                $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_all_active_by_date_pagination($tid,$year, $offset, $limitPerPage);
                $total_count = Topics::count_all_active_by_date_pagination($tid,$year);
                $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';
                        }
                        //count
                        $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_by_topic($found_topic->topic_id) ?? 0;
                    
                       
                        if(!empty($galeries[0])){
                    ?>

                        <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>  <?php echo changedateusfr($found_topic->topic_date_creation)  ?></span>
                                        <span style="margin-right: 15px;"> <i class="fas fa-thumbs-up"></i>  <?php echo ($nbr_dislikes+$nbr_likes)  ?></span>
                                        <span style="margin-right: 15px;"> <i class="fa fa-share-alt"></i>  <?php echo  $nbr_shares  ?></span>
                                        <span style="margin-right: 15px;"> <i class="fa fa-comments"></i>  <?php echo $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="archives-press-' . $year . '-' . $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=\"archives-press-{$year}-{$i}-{$type_topic->type_topic_id}\">{$i}</a>";
                            } else {
                                echo "<a class=\"page-numbers\" href=\"archives-press-{$year}-{$i}-{$type_topic->type_topic_id}\">{$i}</a>";
                            }
                        }
                    }
                    ?>
                    <?php
                    if ($pagination->has_next_page()) {
                        echo '<a class="prev page-numbers" href="archives-press-' . $year . '-' . $pagination->previous_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
          ====================================================================== -->