<?php
/**
 * Template part for displaying slider section
 *
 * @package Food Blogger Hub
 * @subpackage food_blogger_hub
 */

?>
<?php
$food_blogger_hub_hidepageboxes = get_theme_mod('food_blogger_hub_slider', true);
$food_blogger_hub_catData = get_theme_mod('food_blogger_hub_slider_catData');

if ($food_blogger_hub_hidepageboxes && $food_blogger_hub_catData) {
    $food_blogger_hub_slidepage_query = new WP_Query(array(
        'category_name' => esc_attr($food_blogger_hub_catData),
        'posts_per_page' => 5, // Limit to 5 posts
    ));
    if ($food_blogger_hub_slidepage_query->have_posts()) :
?>
    <section id="slider-cat">
        <div class="slideimg">
            <div class="owl-carousel sliderbox">
                <?php while ($food_blogger_hub_slidepage_query->have_posts()) : $food_blogger_hub_slidepage_query->the_post(); ?>
                    <div class="item">
                        <div class="imagebox slider-slide-content">
                            <div class="slide-thumbbx">
                                <?php if (has_post_thumbnail()) : ?>
                                    <?php the_post_thumbnail('full', array('class' => 'full-width-img')); ?>
                                <?php else : ?>
                                    <div class="slide-thumbbx-color full-width-img"></div>
                                <?php endif; ?>
                                <div class="text-content">
                                    <div class="text-inner-box">
                                        <div class="top-slider">
                                            <span class="service-category-name me-4">
                                                <i class="fas fa-tag me-2"></i>
                                                  <?php
                                                  $categories = get_the_category();
                                                  if (!empty($categories)) {
                                                      echo esc_html($categories[0]->name);
                                                  }
                                                  ?>
                                            </span>
                                            <span class="slider-time">
                                                <i class="fas fa-clock pe-1"></i> <?php echo esc_html(get_the_time('H:i')); ?> <?php esc_html_e('HOURS', 'food-blogger-hub'); ?>
                                            </span>
                                        </div>
                                        <?php if (get_theme_mod('food_blogger_hub_show_slider_title', true)) : ?>
                                            <h1 class="text-uppercase">
                                                <a href="<?php the_permalink(); ?>">
                                                    <?php
                                                    $food_blogger_hub_title = get_the_title();
                                                    $food_blogger_hub_words = explode(' ', $food_blogger_hub_title);
                                                    if (count($food_blogger_hub_words) >= 4) {
                                                        $food_blogger_hub_words[2] = '<span class="highlight">' . $food_blogger_hub_words[2];
                                                        $food_blogger_hub_words[3] = $food_blogger_hub_words[3] . '</span>';
                                                    }
                                                    $food_blogger_hub_styled_title = implode(' ', $food_blogger_hub_words);
                                                    echo $food_blogger_hub_styled_title;
                                                    ?>
                                                </a>
                                            </h1>
                                        <?php endif; ?>
                                        <?php if (get_theme_mod('food_blogger_hub_show_slider_content', true)) : ?>
                                            <p class="mb-2"><?php echo esc_html(wp_trim_words(get_the_content(), 60)); ?></p>
                                        <?php endif; ?>
                                        <div class="review-box mt-3 text-start d-flex align-items-center">
                                            <?php
                                                $food_blogger_hub_customer_img = get_theme_mod('food_blogger_hub_customer_img');
                                                if (!empty($food_blogger_hub_customer_img)) {
                                                    echo '<img src="' . esc_url($food_blogger_hub_customer_img) . '" alt="Customer Image" />';
                                                } else {
                                                }
                                            ?>
                                            <?php if ($food_blogger_hub_reviewer_name = get_theme_mod('food_blogger_hub_reviewer_name')) : ?>
                                                <p class="mb-0 me-md-4 me-2 ms-2 text-capitalize customer-name"><?php echo esc_html($food_blogger_hub_reviewer_name); ?></p>
                                            <?php endif; ?>
                                            <?php if ($food_blogger_hub_total_rating = get_theme_mod('food_blogger_hub_total_rating')) : ?>
                                                <p class="mb-0 rating"><i class="fas fa-star me-2"></i><?php echo esc_html($food_blogger_hub_total_rating); ?></p>
                                            <?php endif; ?>
                                            <?php $food_blogger_hub_blog_archive_ordering = get_theme_mod('blog_meta_order', array('comment')); 
                                                foreach ($food_blogger_hub_blog_archive_ordering as $food_blogger_hub_blog_data_order) : 
                                                    if ('comment' === $food_blogger_hub_blog_data_order) : ?>
                                                        <div class="commentmain ms-3"><i class="fas fa-comments me-2"></i><span class="entry-comments"><?php echo esc_html(get_comments_number()); ?></span></div>
                                                    <?php endif;
                                            endforeach; ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endwhile; ?>
            </div>
        </div>
        <div class="slide-cat">
            <div class="carousel-thumb-line">
                <div class="numbered-circles">
                    <?php for ($food_blogger_hub_i = 1; $food_blogger_hub_i <= 5; $food_blogger_hub_i++) : ?>
                        <span class="circle circle-<?php echo $food_blogger_hub_i; ?>" data-index="<?php echo $food_blogger_hub_i; ?>"><?php echo $food_blogger_hub_i; ?></span>
                    <?php endfor; ?>
                </div>
            </div>
            <div class="owl-carousel owl-theme">
                <?php rewind_posts(); // Reset the loop ?>
                <?php while ($food_blogger_hub_slidepage_query->have_posts()) : $food_blogger_hub_slidepage_query->the_post(); ?>
                    <div class="slider-imagebox owl-item">
                        <?php if (has_post_thumbnail()) : ?>
                            <a href="<?php the_permalink(); ?>" class="thumbnail-link">
                                <?php the_post_thumbnail('thumbnail-size', array('class' => 'thumbnail-img')); ?>
                            </a>
                        <?php else : ?>
                            <div class="slide-img-color"></div>
                        <?php endif; ?>
                    </div>
                <?php endwhile; ?>
            </div>
        </div>
    </section>
<?php
    endif; // end have_posts check
    wp_reset_postdata(); // Reset post data
}
?>
