<?php
/**
 * Template part for displaying the services section.
 *
 * @package Crowdfunding Nonprofit
 * @subpackage crowdfunding_nonprofit
 */

// Check if the services section is enabled.
$crowdfunding_nonprofit_static_image = get_template_directory_uri() . '/assets/images/post-img1.png';
$crowdfunding_nonprofit_our_servs = get_theme_mod('crowdfunding_nonprofit_our_servs_setting', true);

if ($crowdfunding_nonprofit_our_servs == '1') {
?>
<section id="our-services" class="py-5 px-md-0 px-3">
    <div class="container overlap-container">
      <div class="text-center">
        <div class="match-heading position-relative text-center">
          <?php if (get_theme_mod('crowdfunding_nonprofit_service_sub_heading') != '') : ?>
            <h2 class="serv-short mt-0 mb-2 text-capitalize"><i class="fas fa-leaf me-2"></i> <?php echo esc_html(get_theme_mod('crowdfunding_nonprofit_service_sub_heading')); ?></h2>
          <?php endif; ?>
        </div>
        <?php
        $crowdfunding_nonprofit_title = get_theme_mod( 'crowdfunding_nonprofit_featured_section_title', '' );
        if ( $crowdfunding_nonprofit_title ) :
            $crowdfunding_nonprofit_words = explode( ' ', $crowdfunding_nonprofit_title );
            if ( isset( $crowdfunding_nonprofit_words[1] ) ) {
                // Highlight 2nd word (index 1)
                $crowdfunding_nonprofit_words[1] = '<span class="featured-title-highlight">' . esc_html( $crowdfunding_nonprofit_words[1] ) . '</span>';
            }
            $crowdfunding_nonprofit_modified_title = implode( ' ', $crowdfunding_nonprofit_words );
            ?>
            <h2 class="text-center mb-2 text-capitalize">
                <?php echo wp_kses_post( $crowdfunding_nonprofit_modified_title ); ?>
            </h2>
        <?php endif; ?>
          <?php if (get_theme_mod('crowdfunding_nonprofit_serv_content') != '') { ?>
            <p class="serv-content mb-3 text-center d-inline-block text-capitalize">
              <?php echo esc_html(get_theme_mod('crowdfunding_nonprofit_serv_content', '')); ?>
            </p>
          <?php } ?>
        </div>
        <div class="owl-carousel mt-4">
            <?php
            // Fetch selected category and the number of posts to show.
            $crowdfunding_nonprofit_post_category = get_theme_mod('crowdfunding_nonprofit_offer_section_category', 'select');
            $crowdfunding_nonprofit_posts_to_show = absint(get_theme_mod('crowdfunding_nonprofit_posts_to_show', 4));

            // Validate category selection.
            if ($crowdfunding_nonprofit_post_category !== 'select') {
                // Query posts from the selected category.
                $crowdfunding_nonprofit_page_query = new WP_Query(array(
                    'category_name'  => esc_html($crowdfunding_nonprofit_post_category),
                    'posts_per_page' => $crowdfunding_nonprofit_posts_to_show,
                ));

                if ($crowdfunding_nonprofit_page_query->have_posts()) {
            $crowdfunding_nonprofit_post_count = 0;

            // Loop through the posts.
            while ($crowdfunding_nonprofit_page_query->have_posts()) : $crowdfunding_nonprofit_page_query->the_post();
              $crowdfunding_nonprofit_post_count++;
            ?>
                <div class="mb-4">
                    <div class="cat-inner-box overlap-item">
                        <?php if (has_post_thumbnail()) { ?>
                            <img src="<?php echo esc_url(get_the_post_thumbnail_url()); ?>" alt="<?php the_title_attribute(); ?>" />
                        <?php } else { ?>
                            <img src="<?php echo esc_url($crowdfunding_nonprofit_static_image); ?>" alt="<?php esc_attr_e('Default Image', 'crowdfunding-nonprofit'); ?>" class="post-color" />
                        <?php } ?>
                        <div class="mainserv-content text-start">
                            <?php if ($crowdfunding_nonprofit_add_record_for_post = get_theme_mod('crowdfunding_nonprofit_add_record_for_post' . $crowdfunding_nonprofit_post_count)) { ?>
                                <p class="records-total my-0"><?php echo esc_html($crowdfunding_nonprofit_add_record_for_post); ?></p>
                            <?php } ?>
                            <h3 class="mt-0 mb-3">
                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            </h3>
                            <p class="post-content text-start"><?php echo wp_trim_words(get_the_content(), 35); ?></p>
                        </div>
                    </div>
                </div>
                    <?php endwhile;
                    wp_reset_postdata(); // Reset post data after custom query.
                } else {
                    // No posts found.
                    echo '<div class="no-postfound">' . esc_html__('No Post found.', 'crowdfunding-nonprofit') . '</div>';
                }
            } else {
                // No category selected.
                echo '<div class="no-postfound">' . esc_html__('No category selected.', 'crowdfunding-nonprofit') . '</div>';
            }
            ?>
        </div>
    </div>
</section>
<?php
}
?>