<?php
/**
 * Template part for displaying slider section
 *
 * @package Church Services
 * @subpackage church_services
 */

?>

<?php $church_services_static_image= get_stylesheet_directory_uri() . '/assets/images/slider-img.png'; ?>

<?php 
$church_services_slider_arrows = get_theme_mod('church_services_slider_arrows', true);
?>
<?php if ($church_services_slider_arrows) : ?>
  <section id="slider">
    <div class="owl-carousel owl-theme">
      <?php 
      $church_services_slide_pages = array();
      for ($church_services_count = 1; $church_services_count <= 4; $church_services_count++) {
        $mod = absint(get_theme_mod('church_services_slider_page' . $church_services_count));
        if ($mod != 0) {
          $church_services_slide_pages[] = $mod;
        }
      }
      if (!empty($church_services_slide_pages)) :
        $church_services_args = array(
          'post_type' => 'page',
          'post__in' => $church_services_slide_pages,
          'orderby' => 'post__in'
        );
        $church_services_query = new WP_Query($church_services_args);
        if ($church_services_query->have_posts()) :
          while ($church_services_query->have_posts()) : $church_services_query->the_post(); ?>
              <div class="item">
                <div class="slider-content-col">
                  <div class="carousel-caption">
                    <div class="inner_carousel">
                      <?php if ( get_theme_mod( 'church_services_slider_short_heading' ) ) : ?>
                        <p class="slider-top mb-1"><?php echo esc_html( get_theme_mod( 'church_services_slider_short_heading' ) ); ?></p>
                      <?php endif; ?>
                      <?php if (get_theme_mod('church_services_show_slider_title', true)) : ?>
                        <h1 class="mb-2">
                          <a href="<?php the_permalink(); ?>">
                            <?php 
                              $church_services_title = get_the_title(); 
                              $church_services_words = explode(' ', $church_services_title); 

                              if (count($church_services_words) >= 3) {
                                // Extract the third word
                                $church_services_third_word = $church_services_words[2]; 
                                // Remove the third word from the original array
                                $church_services_words[2] = '<span class="highlight">' . esc_html($church_services_third_word) . '</span>'; 
                              }

                              // Reconstruct the title with the highlighted third word
                              $church_services_modified_title = implode(' ', $church_services_words);
                            ?>
                            <?php echo wp_kses_post($church_services_modified_title); ?>
                          </a>
                        </h1>
                      <?php endif; ?>
                      <?php if (get_theme_mod('church_services_show_slider_content', true)) : ?>
                        <p class="slider-content"><?php $church_services_excerpt = get_the_excerpt(); echo esc_html( church_services_string_limit_words( $church_services_excerpt, esc_attr(get_theme_mod('church_services_slider_excerpt_length','32')))); ?></p>
                      <?php endif; ?>
                      <div class="more-btn mt-4">
                        <a href="<?php the_permalink(); ?>" class="text-capitalize me-lg-4 me-md-3 me-2 mb-3 slider-btn1"><?php esc_html_e('Get Started', 'church-services'); ?></a>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="slider-img-col align-self-center">
                  <?php if (get_theme_mod('church_services_total_yr_completed') != '') { ?>
                    <div class="counter-box">
                        <div class="text-center no-center">
                          <i class="fas fa-synagogue mb-2"></i>
                          <div class="total_yrs">
                            <?php echo esc_html(get_theme_mod('church_services_total_yr_completed')) . ' ' . esc_html__('Years', 'church-services'); ?>
                          </div>
                          <div class="total_yrs_text">
                            <?php esc_html_e('We Are Growing And Praying', 'church-services'); ?>
                          </div>
                        </div>
                    </div>
                  <?php } ?>
                  <?php if (has_post_thumbnail()) : ?>
                    <img src="<?php the_post_thumbnail_url('full'); ?>" alt="<?php the_title_attribute(); ?>" />
                  <?php else : ?>
                    <img src="<?php echo esc_url(get_stylesheet_directory_uri() . '/assets/images/slider-img.png'); ?>" alt="Fallback Image" />
                  <?php endif; ?>
                </div>
              </div>
          <?php endwhile;
          wp_reset_postdata();
        else : ?>
          <div class="no-postfound"><?php esc_html_e('No posts found', 'church-services'); ?></div>
        <?php endif;
      endif; ?>
    </div>
    <div class="clearfix"></div>
  </section>
<?php endif; ?>

