<?php
/**
 * Template part for displaying slider section
 *
 * @package Product Review
 * @subpackage product_review
 */

?>
<?php if (get_theme_mod('product_review_slider_arrows', true) != '') : ?>
  <div id="slider">
    <div class="container-fuild m-lg-5 m-3">
      <div class="row">
        <div class="col-lg-8 col-md-7 left-slider">
          <div id="owl-carousel" class="slides owl-carousel">
           <?php
            $product_review_post_category = get_theme_mod('product_review_featured_section_category', '');
            $product_review_num_posts = absint(get_theme_mod('product_review_num_posts', 3));

            if (!empty($product_review_post_category)) :
              $product_review_page_query = new WP_Query(array(
                'category_name'  => sanitize_text_field($product_review_post_category),
                'posts_per_page' => $product_review_num_posts,
              ));

              if ($product_review_page_query->have_posts()) :
                while ($product_review_page_query->have_posts()) : $product_review_page_query->the_post(); ?>
                  <div class="item">
                    <div class="cat-inner-box">
                      <div class="image-box">
                        <?php if (has_post_thumbnail()) : ?>
                          <img src="<?php echo esc_url(get_the_post_thumbnail_url(null, 'full')); ?>" alt="<?php echo esc_attr(get_the_title()); ?>" />
                        <?php else : ?>
                          <img src="<?php echo esc_url(get_template_directory_uri() . '/assets/images/post-img1.png'); ?>" alt="<?php echo esc_attr(get_the_title()); ?>" />
                        <?php endif; ?>
                      </div>
                      <div class="inner-content px-md-3 py-4 px-2">
                        <div class="slide-top-detail">
                          <div class="serv-tags">
                            <?php
                            // Get the categories of the current post
                            $product_review_post_categories = get_the_category();
                            if ($product_review_post_categories) {
                              // Take only the first category
                              $product_review_first_category = $product_review_post_categories[0];
                              echo '<div class="post-tags">';
                              echo '<a href="' . esc_url(get_category_link($product_review_first_category->term_id)) . '" class="tag-link">' . esc_html($product_review_first_category->name) . '</a>';
                              echo '</div>';
                            }
                            ?>
                          </div>
                          <div class="post-reviews ps-4">
                            <?php echo do_shortcode( '[kkstarratings]' );?>
                          </div>
                        </div>
                        <h1>
                          <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        </h1>
                        <div class="serv-metafield mb-2">
                          <a class="me-md-4 me-3" href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>">
                            <?php the_author(); ?>
                          </a>
                          <a href="<?php echo esc_url(get_day_link(get_the_date('Y'), get_the_date('m'), get_the_date('d'))); ?>" class="entry-date">
                            <i class="far fa-calendar pe-lg-2 pe-1"></i> <?php echo esc_html(get_the_date('d F Y')); ?>
                          </a>
                        </div>
                      </div>
                    </div>
                  </div>
                <?php endwhile;
                wp_reset_postdata();
              endif;
            endif;
            ?>
          </div>
        </div>
        <div class="col-lg-4 col-md-5">
          <div class="right-slider">
            <?php
            $product_review_right_post_category = get_theme_mod('product_review_featured_section_right_category', '');
            $product_review_right_num_posts     = absint(get_theme_mod('product_review_right_num_posts', 3)); // multiple of 3

            if (!empty($product_review_right_post_category)) :

              $product_review_right_page_query = new WP_Query(array(
                'category_name'  => sanitize_text_field($product_review_right_post_category),
                'posts_per_page' => $product_review_right_num_posts,
              ));

              if ($product_review_right_page_query->have_posts()) :
                $product_review_count = 0;
                $product_review_row_number = 1; 

                while ($product_review_right_page_query->have_posts()) : $product_review_right_page_query->the_post();

                  if ($product_review_count % 3 === 0) {
                    echo '<div class="item"><div class="row row-' . $product_review_row_number . '">';
                  }
                  //Add dynamic post background class
                  $product_review_bg_class = 'post-bg-' . (($product_review_count % 3) + 1);
                  ?>
                  <div class="col-12 mb-3">
                    <div class="cat-inner-box <?php echo esc_attr($product_review_bg_class); ?>">
                      <div class="image-box">
                        <?php if (has_post_thumbnail()) : ?>
                          <img src="<?php echo esc_url(get_the_post_thumbnail_url(get_the_ID(), 'full')); ?>" alt="<?php echo esc_attr(get_the_title()); ?>" />
                        <?php else : ?>
                          <img src="<?php echo esc_url(get_template_directory_uri() . '/assets/images/serv-img1.png'); ?>" alt="<?php echo esc_attr(get_the_title()); ?>" />
                        <?php endif; ?>
                      </div>
                      <div class="inner-content px-3 py-4">
                        <div class="slide-top-detail">
                          <div class="serv-tags">
                            <?php
                              // Get the categories of the current post
                              $product_review_post_categories = get_the_category();
                              if ($product_review_post_categories) {
                                // Take only the first category
                                $product_review_first_category = $product_review_post_categories[0];
                                echo '<div class="post-tags">';
                                echo '<a href="' . esc_url(get_category_link($product_review_first_category->term_id)) . '" class="tag-link">' . esc_html($product_review_first_category->name) . '</a>';
                                echo '</div>';
                              }
                            ?>
                          </div>
                          <div class="post-reviews ps-4">
                            <?php echo do_shortcode('[kkstarratings]'); ?>
                          </div>
                        </div>
                        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                        <div class="serv-metafield mb-2">
                          <a  href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>">
                            <?php the_author(); ?>
                          </a>
                          <a class="ms-md-5 ms-3" href="<?php echo esc_url(get_day_link(get_the_date('Y'), get_the_date('m'), get_the_date('d'))); ?>" class="entry-date">
                            <i class="far fa-calendar pe-lg-2 pe-1"></i> <?php echo esc_html(get_the_date('d F Y')); ?>
                          </a>
                        </div>
                      </div>
                    </div>
                  </div>
                  <?php
                  $product_review_count++;
                  if ($product_review_count % 3 === 0) {
                    echo '</div></div>';
                    $product_review_row_number++; 
                  }
                endwhile;
                if ($product_review_count % 3 !== 0) {
                  echo '</div></div>';
                }
                wp_reset_postdata();
              endif;
            endif;
            ?>
          </div>
        </div>
      </div>
    </div>
  </div>
<?php endif; ?>