<?php
/**
 * Template part for displaying slider section
 *
 * @package Multipurpose Sports Club
 * @subpackage multipurpose_sports_club
 */

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

if (get_theme_mod('multipurpose_sports_club_slider_arrows', true) != '') : ?>

<section id="slider">
  <div id="owl-carousel" class="owl-carousel">
    <?php
    $multipurpose_sports_club_slide_pages = array();
    for ($multipurpose_sports_club_count = 1; $multipurpose_sports_club_count <= 4; $multipurpose_sports_club_count++) {
        $multipurpose_sports_club_mod = intval(get_theme_mod('multipurpose_sports_club_slider_page' . $multipurpose_sports_club_count, 0));
        if ($multipurpose_sports_club_mod > 0) {
            $multipurpose_sports_club_slide_pages[] = $multipurpose_sports_club_mod;
        }
    }

    if (!empty($multipurpose_sports_club_slide_pages)) :
        $multipurpose_sports_club_args = array(
            'post_type' => 'page',
            'post__in' => $multipurpose_sports_club_slide_pages,
            'orderby' => 'post__in'
        );
        $multipurpose_sports_club_query = new WP_Query($multipurpose_sports_club_args);
        if ($multipurpose_sports_club_query->have_posts()) :
            while ($multipurpose_sports_club_query->have_posts()) : $multipurpose_sports_club_query->the_post(); ?>
                <div class="item">
                    <div class="slider-border">
                        <?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($multipurpose_sports_club_static_image); ?>" alt="<?php esc_attr_e('Default Image', 'multipurpose-sports-club'); ?>" />
                        <?php } ?>
                    </div>
                    <div class="carousel-caption">
                        <div class="inner_carousel">
                            <?php $multipurpose_sports_club_short_heading = get_theme_mod('multipurpose_sports_club_slider_short_heading', '');
                            if (!empty($multipurpose_sports_club_short_heading)) { ?>
                                <p class="slidetop-text mb-lg-3 mb-2 text-uppercase"><?php echo esc_html($multipurpose_sports_club_short_heading); ?></p>
                            <?php } ?>
                            <?php
                            $multipurpose_sports_club_title = get_the_title();
                            $multipurpose_sports_club_title_words = explode(' ', $multipurpose_sports_club_title);
                            $multipurpose_sports_club_last_word = array_pop($multipurpose_sports_club_title_words);
                            $multipurpose_sports_club_rest_of_title = implode(' ', $multipurpose_sports_club_title_words);
                            ?>
                            <?php if (get_theme_mod('multipurpose_sports_club_show_slider_title', true)) : ?>
                                <h1 class="custom-title text-uppercase"><a href="<?php the_permalink(); ?>"><?php echo esc_html($multipurpose_sports_club_rest_of_title) . ' <span class="bordered-word">' . esc_html($multipurpose_sports_club_last_word) . '</span>'; ?></a></h1>
                            <?php endif; ?>
                            <?php if (get_theme_mod('multipurpose_sports_club_show_slider_content', true)) : ?>
                                <p class="mb-0 slider-content"><?php $multipurpose_sports_club_excerpt = get_the_excerpt(); echo esc_html( multipurpose_sports_club_string_limit_words( $multipurpose_sports_club_excerpt, esc_attr(get_theme_mod('multipurpose_sports_club_slider_excerpt_length','35')))); ?></p>
                            <?php endif; ?>
                            <div class="more-btn mt-4">
                                <?php $multipurpose_sports_club_btn_text1 = get_theme_mod('multipurpose_sports_club_product_btn_text1', __('Get Started', 'multipurpose-sports-club'));
                                $multipurpose_sports_club_btn_link1 = get_theme_mod('multipurpose_sports_club_product_btn_link1', get_permalink());
                                if (!empty($multipurpose_sports_club_btn_text1) || !empty($multipurpose_sports_club_btn_link1)) { ?>
                                    <a target="_blank" class="text-uppercase me-2 mb-3 slider-btn1" href="<?php echo esc_url($multipurpose_sports_club_btn_link1); ?>">
                                        <?php echo esc_html($multipurpose_sports_club_btn_text1); ?><i class="fas fa-arrow-right ms-3" aria-hidden="true"></i>
                                    </a>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                    <div class="social-box">
                    <div class="text-lg-end text-md-center text-center align-self-center social-area mb-md-0 ps-lg-2">
                      <?php
                      // Retrieve social media links from theme modifications.
                      $multipurpose_sports_club_social_media_facebook = get_theme_mod('multipurpose_sports_club_social_media_facebook');
                      $multipurpose_sports_club_social_media_instagram = get_theme_mod('multipurpose_sports_club_social_media_instagram');
                      $multipurpose_sports_club_social_media_twitter = get_theme_mod('multipurpose_sports_club_social_media_twitter');
                      $multipurpose_sports_club_social_media_youtube = get_theme_mod('multipurpose_sports_club_social_media_youtube');

                      // Output social media links while keeping the original structure.
                      if ($multipurpose_sports_club_social_media_twitter != '') { ?>
                        <a target="_blank" href="<?php echo esc_url($multipurpose_sports_club_social_media_twitter); ?>">
                          <i class="fab fa-twitter"></i>
                          <span class="screen-reader-text"><?php esc_html_e('Twitter', 'multipurpose-sports-club'); ?></span>
                        </a>
                      <?php }

                      if ($multipurpose_sports_club_social_media_facebook != '') { ?>
                        <a target="_blank" href="<?php echo esc_url($multipurpose_sports_club_social_media_facebook); ?>">
                          <i class="fab fa-facebook-f"></i>
                          <span class="screen-reader-text"><?php esc_html_e('Facebook', 'multipurpose-sports-club'); ?></span>
                        </a>
                      <?php }
                      if ($multipurpose_sports_club_social_media_instagram != '') { ?>
                        <a target="_blank" href="<?php echo esc_url($multipurpose_sports_club_social_media_instagram); ?>">
                          <i class="fab fa-instagram"></i>
                          <span class="screen-reader-text"><?php esc_html_e('Instagram', 'multipurpose-sports-club'); ?></span>
                        </a>
                      <?php }
                      
                      if ($multipurpose_sports_club_social_media_youtube != '') { ?>
                        <a target="_blank" href="<?php echo esc_url($multipurpose_sports_club_social_media_youtube); ?>">
                          <i class="fab fa-youtube"></i>
                          <span class="screen-reader-text"><?php esc_html_e('Youtube', 'multipurpose-sports-club'); ?></span>
                        </a>
                      <?php } ?>
                    </div>
                  </div>
                </div>
            <?php endwhile;
            wp_reset_postdata();
        else : ?>
            <div class="no-postfound"><?php esc_html_e('No slides found.', 'multipurpose-sports-club'); ?></div>
        <?php endif;
    endif; ?>
  </div>
</section>
<?php endif; ?>