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

// Get the courses setting.
  $product_review_servicesec = get_theme_mod('product_review_service_show_hide', 1);
  if ($product_review_servicesec == 1) : 
  ?>
  <section id="service-section" class="py-5">
    <div class="container"> 
      <div class="serv-head mb-md-5 mb-3"> 
          <div class="serv-left">
            <?php if (get_theme_mod('product_review_section_title')) : ?>
              <h2 class="section-title"><?php echo esc_html(get_theme_mod('product_review_section_title')); ?></h2>
            <?php endif; ?> 
          </div>
          <div class="serv-right">
              <span class="view-more-button">
                  <?php if (get_theme_mod('product_review_view_more_button_link') && get_theme_mod('product_review_view_more_button_text', 'View All')): ?>
                      <a href="<?php echo esc_url(get_theme_mod('product_review_view_more_button_link')); ?>"><span><?php echo esc_html(get_theme_mod('product_review_view_more_button_text', 'View All')); ?></span></a>
                  <?php endif; ?>
              </span>
          </div>
      </div>
      <div class="row">
        <div class="col-lg-4 col-md-4">
          <div id="owl-carousel" class="serv-1 owl-carousel">
           <?php
            $product_review_second_section_first_category = get_theme_mod('product_review_second_section_first_category', '');
            $product_review_second_section_first_num_posts = absint(get_theme_mod('product_review_second_section_first_num_posts', 3));

            if (!empty($product_review_second_section_first_category)) :
              $product_review_page_query = new WP_Query(array(
                'category_name'  => sanitize_text_field($product_review_second_section_first_category),
                'posts_per_page' => $product_review_second_section_first_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-1">
                      <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/sec-serv1.png'); ?>" alt="<?php echo esc_attr(get_the_title()); ?>" />
                        <?php endif; ?>
                      </div>
                      <div class="inner-content">
                        <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>
                        <h3>
                          <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        </h3>
                        <div class="serv-metafield mb-2">
                          <a class="me-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-alt pe-lg-2 pe-1"></i> <?php echo esc_html(get_the_date('d F Y')); ?>
                          </a>
                          <a href="<?php comments_link(); ?>" class="entry-comments">
                            <i class="far fa-comment-alt  pe-lg-2 pe-1"></i><?php comments_number(__('0 Comments', 'product-review'), __('1 Comment', 'product-review'), __('% Comments', 'product-review')); ?>
                          </a>
                        </div>
                      </div>
                    </div>
                  </div>
                <?php endwhile;
                wp_reset_postdata();
              endif;
            endif;
            ?>
          </div>
        </div>
        <div class="col-lg-4 col-md-4">
           <div class="serv-2">
            <?php
            $product_review_second_section_sec_category = get_theme_mod('product_review_second_section_sec_category', '');
            $product_review_second_section_sec_num_posts     = absint(get_theme_mod('product_review_second_section_sec_num_posts', 3)); // multiple of 3

            if (!empty($product_review_second_section_sec_category)) :

              $product_review_right_page_query = new WP_Query(array(
                'category_name'  => sanitize_text_field($product_review_second_section_sec_category),
                'posts_per_page' => $product_review_second_section_sec_num_posts,
              ));

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

                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">'; // open slide wrapper
                  }
                  ?>
                  <div class="cat-inner-box-2 mb-3">
                    <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/sec-middle1.png'); ?>" alt="<?php echo esc_attr(get_the_title()); ?>" />
                      <?php endif; ?>
                    </div>
                    <div class="inner-content px-lg-3 py-xl-4 px-2 py-lg-2">
                      <span 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>';
                          }
                        ?>
                      </span>
                      <span class="serv-metafield">
                        <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-alt pe-lg-2 pe-1"></i> <?php echo esc_html(get_the_date('d F Y')); ?>
                        </a>
                      </span>
                      <h3 class="my-0"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    </div>
                  </div>
                  <?php
                  $product_review_count++;
                  if ($product_review_count % 3 === 0) {
                    echo '</div>'; // close slide wrapper
                  }
                endwhile;
                // If posts are not a multiple of 3, close the last wrapper
                if ($product_review_count % 3 !== 0) {
                  echo '</div>';
                }
                wp_reset_postdata();
              endif;
            endif;
            ?>
          </div>
        </div>
        <div class="col-lg-4 col-md-4">
          <div class="serv-3">
            <?php
            $product_review_second_section_third_category = get_theme_mod('product_review_second_section_third_category', '');
            $product_review_second_section_third_num_posts = absint(get_theme_mod('product_review_second_section_third_num_posts', 2)); // multiple of 2

            if (!empty($product_review_second_section_third_category)) :

              $product_review_right_page_query = new WP_Query(array(
                'category_name'  => sanitize_text_field($product_review_second_section_third_category),
                'posts_per_page' => $product_review_second_section_third_num_posts,
              ));

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

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

                  // Open slide wrapper every 2 posts (2 rows × 1 column)
                  if ($product_review_count % 2 === 0) {
                    echo '<div class="item"><div class="row">';
                  }
                  ?>
                  <div class="col-12 mb-3"><!-- 1 column per row -->
                    <div class="cat-inner-box-3">
                      <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/sec-last1.png'); ?>" alt="<?php echo esc_attr(get_the_title()); ?>" />
                        <?php endif; ?>
                      </div>
                      <div class="inner-content px-3 py-4">
                        <h3 class="my-2"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                      </div>
                    </div>
                  </div>
                  <?php
                  $product_review_count++;
                  // Close row + item wrapper after 2 posts
                  if ($product_review_count % 2 === 0) {
                    echo '</div></div>';
                  }
                endwhile;
                // Close last wrapper if not multiple of 2
                if ($product_review_count % 2 !== 0) {
                  echo '</div></div>';
                }
                wp_reset_postdata();
              endif;
            endif;
            ?>
          </div>
        </div>
      </div>
    </div>
  </section>
<?php endif; ?>