<?php include('sections/parts/head-post.php') ?>
<?php include('sections/parts/header.php') ?>
<?php
$tid = (int)$_GET['bid'];
$page_id = $_GET['pid'] ?? 407;
$topic = topics::find_by_id($tid );
$page_children_id = 431;
$custom_page_title = $topic->topic_title;
?>
<!-- page section -->
<?php include_once('sections/parts/page-header-post.php') ?>
<!--end page section-->


<!--Blog Details Start-->
<section class="blog-details">
    <div class="container">
        <div class="row">
            <div class="col-xl-8 col-lg-7">
                <div class="blog-details__left">
                    <div class="blog-details__img-box">
                        <?php if (isset($topic->topic_image_path) && !empty($topic->topic_image_path)) : ?>
                            <?php
                            // si image_path contient ',' alors on url est le premier element
                            $image_path = explode(',', $topic->topic_image_path);
                            $image_path = $image_path[0];
                            ?>
                            <?php if ($image_path !== '' && is_file(FOLDER_MANAGER . $image_path)) : ?>
                                <img src="<?= FOLDER_MANAGER . $image_path ?>" alt="Image">
                                <div class="blog-details__date">
                                    <p><?= date('d', strtotime($topic->topic_date_creation)) ?>
                                        <span><?php $date = new DateTime($topic->topic_date_creation);
                                                echo $date->format("M"); ?>
                                        </span>
                                    </p>
                                </div>
                            <?php else : ?>
                                <!-- img src="assets/images/ac.jpg" alt="Not Found" -->
                            <?php endif; ?>
                        <?php else : ?>
                            <!-- img  src="assets/images/ac.jpg" alt="Not Found" -->
                        <?php endif; ?>
                    </div>
                    <div class="blog-details__content">
                        <ul class="blog-details__meta list-unstyled">
                            <li>
                                <p><i class="fa fa-user"></i> &nbsp; <?= User_info::find_by_user_account($topic->topic_author)->full_name() ?></p>
                            </li>
                            <li>
                                <p><i class="fa fa-eye"></i> <?= mnber_format_short($topic->topic_views??0) ?> </p>
                            </li>
                            <li>
                                <p><i class="fa fa-share-alt"></i> <?= mnber_format_short($topic->topic_shares??0) ?> </p>
                            </li>
                            <li>
                                <p>
                                    <i class="fa fa-comments"></i> 
                                <?php 
                                $nbr_commentaire = Comment_topic::count_valid_by_topic($topic->topic_id);
                                echo mnber_format_short($nbr_commentaire) ?? 0; ?>
                                </p>
                            </li>
                            <li>
                                <a> &nbsp; <i class="far fa-calendar"></i>
                                    <?php $creationDate = new DateTime($topic->topic_date_creation);
                                    echo $creationDate->format('d M Y'); ?> &nbsp;</a>
                            </li>
                        </ul>
                        <h3 class="blog-details__title"> <?= $topic->topic_title ?></h3>
                        <p class="blog-details__text-1"></p>
                        <p class="blog-details__text-2">
                            <?php echo str_replace("ckeditor_uploads", "https://presses.lerefletdafrique.tg/workplace/ckeditor_uploads",$topic->topic_text) ?>
                        </p>
                    </div>
                    <div class="blog-details__bottom">
                        <p class="blog-details__tags">
                            <?php
                            if (isset($topic->topic_icon) && !empty($topic->topic_icon)) {
                                echo '<span>Tags</span>';
                                $tags = explode(',', $topic->topic_icon);
                                foreach ($tags as $tag) {
                                    echo '<a href="#blog-tag"> ' . $tag . '</a>';
                                }
                            }
                            ?>
                        </p>
                        <div class="blog-details__social-list">
                            <a data-sharer="x" data-title="<?php echo $topic->topic_title; ?>" data-topic-id="<?php echo $topic->topic_id; ?>" data-hashtag="ThePermanentMissionofSierraLeone" data-description="<?php echo $topic->gettextlimit(255); ?>" data-url="<?php echo $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>"><i class="fab fa-twitter"></i></a>
                            <a data-sharer="facebook" data-title="<?php echo $topic->topic_title; ?>" data-topic-id="<?php echo $topic->topic_id; ?>" data-hashtag="ThePermanentMissionofSierraLeone" data-description="<?php echo $topic->gettextlimit(255); ?>" data-url="<?php echo $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>"><i class="fab fa-facebook"></i></a>
                            <a data-sharer="whatsapp" data-web  data-title="<?php echo $topic->topic_title; ?>" data-topic-id="<?php echo $topic->topic_id; ?>" data-hashtag="ThePermanentMissionofSierraLeone" data-description="<?php echo $topic->gettextlimit(255); ?>" data-url="<?php echo $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>"><i class="fab fa-whatsapp"></i></a>
                            <a data-sharer="telegram" data-web  data-title="<?php echo $topic->topic_title; ?>" data-topic-id="<?php echo $topic->topic_id; ?>" data-hashtag="ThePermanentMissionofSierraLeone" data-description="<?php echo $topic->gettextlimit(255); ?>" data-url="<?php echo $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>"><i class="fab fa-telegram"></i></a>
                        </div>
                    </div>
                    <div class="blog-details__pagenation">
                        <div class="blog-details__pagenation-left">
                            <?php $prev_topic = Topics::find_previous_topic($topic->topic_id, $topic->topic_typetopic_id);
                            if ($prev_topic) : ?>
                                <div class="blog-details__pagenation-left-img">
                                    <?php
                                    // si image_path contient ',' alors on url est le premier element
                                    $image_path = explode(',', $prev_topic->topic_image_path);
                                    $image_path = $image_path[0];
                                    ?>
                                    <?php if ($image_path !== '' && is_file(FOLDER_MANAGER . $image_path)) : ?>
                                        <img src="<?= FOLDER_MANAGER . $image_path ?>" alt="">
                                    <?php else : ?>
                                        <img src="assets/images/ac.jpg" alt="">
                                    <?php endif ?>
                                </div>
                                <div class="blog-details__pagenation-left-content">
                                    <h4 class="blog-details__pagenation-left-title">
                                        <a href="<?= $prev_topic->topic_id ?>-post-<?= $prev_topic->topic_url ?>-<?= $page_id ?>#contenu"> <?= $prev_topic->gettitlelimit(50) ?></a>
                                    </h4>
                                    <p class="blog-details__pagenation-left-date">
                                        <i class="fas fa-clock"></i>
                                        <?= date('d', strtotime($prev_topic->topic_date_creation)) ?>,
                                        <?php
                                        $date = new DateTime($prev_topic->topic_date_creation);
                                        echo $date->format("M");
                                        echo ' ' . $date->format("Y");
                                        ?>
                                    </p>
                                </div>
                            <?php endif ?>
                        </div>
                        <div class="blog-details__pagenation-right">
                            <?php $next_topic = Topics::find_next_topic($topic->topic_id, $topic->topic_typetopic_id);
                            if ($next_topic) : ?>
                                <div class="blog-details__pagenation-right-content">
                                    <h4 class="blog-details__pagenation-right-title"><a href="<?= $next_topic->topic_id ?>-post-<?= $next_topic->topic_url ?>-<?= $page_id ?>#contenu"> <?= $next_topic->gettitlelimit(50) ?></a></h4>
                                    <p class="blog-details__pagenation-right-date">
                                        <i class="fas fa-clock"></i>
                                        <?= date('d', strtotime($next_topic->topic_date_creation)) ?>,
                                        <?php
                                        $date = new DateTime($next_topic->topic_date_creation);
                                        echo $date->format("M");
                                        echo ' ' . $date->format("Y");
                                        ?>
                                    </p>
                                </div>
                                <div class="blog-details__pagenation-right-img">
                                    <?php
                                    // si image_path contient ',' alors on url est le premier element
                                    $image_path = explode(',', $next_topic->topic_image_path);
                                    $image_path = $image_path[0];
                                    ?>
                                    <?php if ($image_path !== '' && is_file(FOLDER_MANAGER . $image_path)) : ?>
                                        <img src="<?= FOLDER_MANAGER . $image_path ?>" alt="">
                                    <?php else : ?>
                                        <img src="assets/images/ac.jpg" alt="">
                                    <?php endif ?>
                                </div>
                            <?php endif ?>
                        </div>
                    </div>
                    <div class="comment-one">
                        <?php
                                $found_topic_comments = Comment_topic::find_comment_topic_published($topic->topic_id);
                         ?>
                                <h3 class="comment-one__title"><?php echo $nbr_commentaire; ?> Comments</h3>
                                <?php foreach ($found_topic_comments as $found_topic_comment): ?>                                    
                                <div class="comment-one__single">
                                    
                                    <div class="comment-one__content">
                                        <h3><?php echo $found_topic_comment->comment_topic_author ?></h3>
                                        <p><?php echo $found_topic_comment->comment_topic_text ?></p>
                                        <!-- a href="" class="thm-btn comment-one__btn"></a -->
                                    </div>
                                </div>
                                <?php endforeach ?>
                            </div>

                            <div class="comment-form">
                                <h3 class="comment-form__title">Leave a Comment</h3>
                                <form action="sendcomment.php" class="comment-one__form contact-form-validated" novalidate="novalidate" method="POST">
                                    <input type="hidden" name="tid" value="<?=$topic->topic_id?>" required>
                                    <input type="hidden" name="cid" value="0" required>
                                    <input type="hidden" name="captcha" value="<?php echo date('Y-m-d') ?>" required>
                                    <div class="row">
                                        <div class="col-xl-6">
                                            <div class="comment-form__input-box">
                                                <input type="text" placeholder="Your Name" name="name" required>
                                            </div>
                                        </div>
                                        <div class="col-xl-6">
                                            <div class="comment-form__input-box">
                                                <input type="email" placeholder="Email Address" name="email" required>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-xl-12">
                                            <div class="comment-form__input-box text-message-box">
                                                <textarea name="message" placeholder="Write  a Comment" required></textarea>
                                            </div>

                                            <p class="comment-form__input-box">
                                                <div 
                                                        class="g-recaptcha" 
                                                        data-sitekey="6LfUT2AqAAAAAK_RR3DwpSNTED85zioGfjIsW2Lb" 
                                                        data-callback="recaptchaCallback"
                                                        data-callback='onSubmit' 
                                                        data-action='submit'
                                                >
                                                </div>
                                               
                                                <br>
                                                <span id="captchaError" style="color: red; display: none;">Invalid reCAPTCHA.</span>
                                                
                                            </p>
                                            <div class="comment-form__btn-box">
                                                <button type="submit" id="submitBtn" class="thm-btn comment-form__btn" onclick="onSubmit()" disabled>Submit Comment</button>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                                <div class="result"></div>
                            </div>
                </div>
            </div>
            <div class="col-xl-4 col-lg-5 blog-sidebar__col">
                <?php include('sections/pages/post-sidebar.php') ?>
            </div>
        </div>
    </div>
</section>
<!--Blog Details End-->

<?php
//update visit count
$topic->topic_views = $topic->topic_views + 1;
$topic->save();
?>
<?php include('sections/parts/footer.php') ?>
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "Article",
  "headline": "<?php echo htmlspecialchars($topic->topic_title); ?>",
  "description": "<?php echo htmlspecialchars($topic->gettextlimit(350)); ?>",
  "image": "<?php echo htmlspecialchars($first_image); ?>",
  "author": {
    "@type": "Person",
    "name": "<?= User_info::find_by_user_account($topic->topic_author)->full_name() ?>"
  },
  "datePublished": "<?php echo date('Y-m-d\TH:i:sP', strtotime($topic->topic_date_creation)); ?>",
  "dateModified": "<?php echo date('Y-m-d\TH:i:sP', strtotime($topic->topic_date_creation)); ?>",
  "aggregateRating": {
    "@type": "AggregateRating",
    "ratingValue": "4.5",
    "ratingCount": "<?php echo $topic->topic_views; ?>"  // Le nombre de vues en tant que ratingCount
  },
  "publisher": {
    "@type": "Organization",
    "name": "Permanent Mission Of Sierra Leone",
    "logo": {
      "@type": "ImageObject",
      "url": "assets/images/sllogo.png"
    }
  },
  "url": "<?php echo htmlspecialchars($topic->topic_url) . '-' . htmlspecialchars($topic->topic_id); ?>"
}
</script>

<?php include('sections/parts/scripts.php') ?>
