<?php
/**
 * Template part for displaying slider section
 *
 * @package Gym Coach
 * @subpackage gym_coach
 */

$gym_coach_static_image = get_stylesheet_directory_uri() . '/assets/images/slider-img.png';

// Check if slider arrows are enabled
if ( get_theme_mod( 'gym_coach_slider_arrows', true ) ) : ?>

<section id="slider">
  <div class="owl-carousel">
    <?php
    $gym_coach_slide_pages = array();
    
    // Loop through 4 possible slides
    for ( $gym_coach_count = 1; $gym_coach_count <= 5; $gym_coach_count++ ) {
        $gym_coach_mod = intval( get_theme_mod( 'gym_coach_slider_page' . $gym_coach_count ) );
        if ( $gym_coach_mod && 'page-none-selected' !== $gym_coach_mod ) {
            $gym_coach_slide_pages[] = $gym_coach_mod;
        }
    }
    // Check if there are any pages for the slider
    if ( ! empty( $gym_coach_slide_pages ) ) :
        $gym_coach_args = array(
            'post_type'      => 'page',
            'post__in'       => $gym_coach_slide_pages,
            'orderby'        => 'post__in',
            'posts_per_page' => 5, // Limit to 4 posts
        );
        $gym_coach_query = new WP_Query( $gym_coach_args );

        if ( $gym_coach_query->have_posts() ) :
            while ( $gym_coach_query->have_posts() ) : $gym_coach_query->the_post(); ?>
                <div class="slide-box">
                    <div class="inner_content">
                        <?php if ( get_theme_mod( 'gym_coach_slider_short_heading' ) ) : ?>
                            <p class="slider-top text-uppercase"><?php echo esc_html( get_theme_mod( 'gym_coach_slider_short_heading' ) ); ?></p>
                        <?php endif; ?>
                        <?php if (get_theme_mod('gym_coach_show_slider_title', true)) : ?>
                            <h1>
                              <a href="<?php echo esc_url( get_permalink() ); ?>">
                                <?php
                                $gym_coach_title = get_the_title();
                                $gym_coach_words = explode(' ', $gym_coach_title);
                                if (count($gym_coach_words) > 1) {
                                    // Wrap the second word in a span
                                    $gym_coach_words[1] = '<span class="highlight-word">' . $gym_coach_words[1] . '</span>';
                                }
                                echo wp_kses_post(implode(' ', $gym_coach_words));
                                ?>
                              </a>
                            </h1>
                        <?php endif; ?>
                        <?php if (get_theme_mod('gym_coach_show_slider_content', true)) : ?>
                            <p class="slide-content"><?php $gym_coach_excerpt = get_the_excerpt(); echo esc_html( gym_coach_string_limit_words( $gym_coach_excerpt, esc_attr(get_theme_mod('gym_coach_slider_excerpt_length','15')))); ?></p>
                        <?php endif; ?>
                        <div class="more-btn mt-4">
                            <a class="btn-1" href="<?php echo esc_url( get_permalink() ); ?>" target="_blank">
                                <?php esc_html_e( 'Get Started', 'gym-coach' ); ?>
                            </a>
                        </div>
                    </div>
                    <div class="img-box">
                        <?php if ( has_post_thumbnail() ) : ?>
                            <img src="<?php echo esc_url( get_the_post_thumbnail_url( get_the_ID(), 'full' ) ); ?>" alt="<?php the_title_attribute(); ?>" />
                        <?php else : ?>
                            <img src="<?php echo esc_url( $gym_coach_static_image ); ?>" alt="<?php esc_attr_e( 'Placeholder', 'gym-coach' ); ?>" />
                        <?php endif; ?>
                    </div>
                </div>
            <?php endwhile;
            wp_reset_postdata();
        else : ?>
            <div class="no-postfound"><?php esc_html_e( 'No slides found', 'gym-coach' ); ?></div>
        <?php endif;
    endif;
    ?>
  </div>
</section>

<?php endif; // End of slider check ?>