<?php
/**
 * Template part for displaying slider section
 *
 * @package Mobile App Hub
 * @subpackage mobile_app_hub
 */

$mobile_app_hub_static_image = get_stylesheet_directory_uri() . '/assets/images/slider.png';
$mobile_app_hub_slider_arrows = get_theme_mod('mobile_app_hub_slider_arrows', true);
?>

<?php if (!empty($mobile_app_hub_slider_arrows)) { ?>
  <section id="slider">
    <div class="container slider-content">
      <div class="owl-carousel owl-theme">
        <?php 
        $mobile_app_hub_slide_pages = array();
        for ($mobile_app_hub_count = 1; $mobile_app_hub_count <= 4; $mobile_app_hub_count++) {
          $mod = intval(get_theme_mod('mobile_app_hub_slider_page' . $mobile_app_hub_count));
          if ('page-none-selected' != $mod) {
            $mobile_app_hub_slide_pages[] = $mod;
          }
        }

        if (!empty($mobile_app_hub_slide_pages)) :
          $mobile_app_hub_args = array(
            'post_type' => 'page',
            'post__in' => $mobile_app_hub_slide_pages,
            'orderby' => 'post__in'
          );
          $mobile_app_hub_query = new WP_Query($mobile_app_hub_args);
          if ($mobile_app_hub_query->have_posts()) :
            while ($mobile_app_hub_query->have_posts()) : $mobile_app_hub_query->the_post(); ?>
              <div class="item">
                <div class="row">
                  <div class="col-lg-6 col-md-6 col-12 slider-content-col align-self-center">
                    <div class="carousel-caption">
                      <div class="inner_carousel">
                        <?php
                        $mobile_app_hub_title = get_the_title();
                        $mobile_app_hub_title_words = explode(' ', $mobile_app_hub_title);
                        $mobile_app_hub_word_count = count($mobile_app_hub_title_words);

                        if ($mobile_app_hub_word_count > 1) {
                            // Get the last word
                            $mobile_app_hub_last_word = array_pop($mobile_app_hub_title_words);

                            // Reconstruct the title without the last word
                            $mobile_app_hub_rest_of_title = implode(' ', $mobile_app_hub_title_words);

                            // Highlight the last word
                            $mobile_app_hub_highlighted_title = $mobile_app_hub_rest_of_title . ' <span class="highlighted-word">' . esc_html($mobile_app_hub_last_word) . '</span>';
                        } else {
                            // If there's only one word, just highlight it
                            $mobile_app_hub_highlighted_title = '<span class="highlighted-word">' . esc_html($mobile_app_hub_title) . '</span>';
                        }
                        ?>
                        <?php if (get_theme_mod('mobile_app_hub_show_slider_title', true)) : ?>
                          <h1 class="mb-2">
                              <a href="<?php the_permalink(); ?>" class="text-capitalize"><?php echo $mobile_app_hub_highlighted_title; ?></a>
                          </h1>
                        <?php endif; ?>
                        <?php if (get_theme_mod('mobile_app_hub_show_slider_content', true)) : ?>
                          <p class="mb-2"><?php $mobile_app_hub_excerpt = get_the_excerpt(); echo esc_html( mobile_app_hub_string_limit_words( $mobile_app_hub_excerpt, esc_attr(get_theme_mod('mobile_app_hub_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', 'mobile-app-hub'); ?></a>
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="col-lg-6 col-md-6 col-12 slider-img-col align-self-center">
                    <?php if (has_post_thumbnail()) { ?>
                      <img src="<?php the_post_thumbnail_url('full'); ?>" alt="<?php esc_attr_e('Slider Image', 'mobile-app-hub'); ?>" />
                    <?php } else { ?>
                      <img src="<?php echo esc_url($mobile_app_hub_static_image); ?>" alt="<?php esc_attr_e('Slider Image', 'mobile-app-hub'); ?>" />
                    <?php } ?>
                  </div>
                </div>
              </div>
            <?php endwhile;
            wp_reset_postdata();
          else : ?>
            <div class="no-postfound"><?php esc_html_e('No posts found', 'mobile-app-hub'); ?></div>
          <?php endif;
        endif; ?>
      </div>
    </div>
    <div class="clearfix"></div>
  </section>
<?php } ?>