<?php
/**
 * Template part for displaying the services section
 *
 * @package Notary Services
 * @subpackage notary_services
 */

// Get the courses setting.
$notary_services_courses = get_theme_mod('notary_services_courses_setting', true);

if ($notary_services_courses == '1') {
?>
<section id="our-services" class="py-5 px-md-0 px-3">
  <div class="container">
    <div class="serv-icon mb-2">
      <span class="line"></span>
      <i class="far fa-flag"></i>
      <span class="line"></span>
    </div>
    <div class="text-center main-sec-title">
      <?php if (get_theme_mod('notary_services_offer_section_tittle')) { ?>
        <h2 class="text-center text-capitalize mb-2"><?php echo esc_html(get_theme_mod('notary_services_offer_section_tittle')); ?></h2>
      <?php } ?>
    </div>
    <?php if (get_theme_mod('notary_services_offer_section_text')) { ?>
      <h3 class="text-center px-md-5 serv-description"><?php echo esc_html(get_theme_mod('notary_services_offer_section_text')); ?></h3>
    <?php } ?>
    <div class="row mt-4">
      <?php
        // Get the selected post category and number of posts to show.
        $notary_services_post_category = get_theme_mod('notary_services_offer_section_category');
        $notary_services_posts_to_show = get_theme_mod('notary_services_posts_to_show', 3);

        if ($notary_services_post_category) {
          // Query for posts in the selected category.
          $notary_services_page_query = new WP_Query(array(
            'category_name' => esc_html($notary_services_post_category),
            'posts_per_page' => $notary_services_posts_to_show
          ));

          if ($notary_services_page_query->have_posts()) {
            $notary_services_post_count = 0;

            // Loop through the posts.
            while ($notary_services_page_query->have_posts()) : $notary_services_page_query->the_post();
              $notary_services_post_count++;
      ?>
            <div class="col-lg-4 col-md-4 mb-4">
              <div class="cat-inner-box">
                <div class="icon-main my-2">
                  <?php
                  // Fetch icon class or set default.
                  $notary_services_icon_class = get_theme_mod('notary_services_services_icon' . ($notary_services_page_query->current_post + 1), 'fas fa-lightbulb');
                  ?>
                  <i class="<?php echo esc_attr($notary_services_icon_class); ?>"></i>
                </div>
                <h4 class="mb-3 pt-3"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                <p class="my-1 post-content text-start"><?php echo wp_trim_words(get_the_content(), 28); ?></p>
              </div>
            </div>
          <?php endwhile;
          wp_reset_postdata(); // Reset post data after the custom query.
          } else {
            // Optionally display a message if no posts are found.
            echo '<div class="no-postfound">' . esc_html__('No posts found.', 'notary-services') . '</div>';
          }
        }
      ?>
    </div>
  </div>
</section>
<?php 
} // End of the if statement for courses.
?>