<?php
/**
 * Template part for displaying slider section
 *
 * @package SEO Enterprise
 * @subpackage seo_enterprise
 */

$seo_enterprise_static_image = get_template_directory_uri() . '/assets/images/slider-img.png';
$seo_enterprise_slider_arrows = get_theme_mod('seo_enterprise_slider_arrows', true);

?>
<?php if ($seo_enterprise_slider_arrows) : ?>
  <section id="slider" style="background-image: url('<?php echo get_stylesheet_directory_uri(); ?>/assets/images/sliderbg.png');">
    <div class="container">
      <div class="owl-carousel owl-theme">
        <?php 
        $seo_enterprise_slide_pages = array();
        for ($seo_enterprise_count = 1; $seo_enterprise_count <= 4; $seo_enterprise_count++) {
          $mod = absint(get_theme_mod('seo_enterprise_slider_page' . $seo_enterprise_count));
          if ($mod != 0) {
            $seo_enterprise_slide_pages[] = $mod;
          }
        }

        if (!empty($seo_enterprise_slide_pages)) :
          $seo_enterprise_args = array(
            'post_type' => 'page',
            'post__in' => $seo_enterprise_slide_pages,
            'orderby' => 'post__in'
          );
          $seo_enterprise_query = new WP_Query($seo_enterprise_args);
          if ($seo_enterprise_query->have_posts()) :
            while ($seo_enterprise_query->have_posts()) : $seo_enterprise_query->the_post(); ?>
              <div class="item">
                <div class="row m-0">
                  <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 if (get_theme_mod('seo_enterprise_slider_short_heading')) : ?>
                          <p class="slidetop-text mb-2 text-uppercase"><?php echo esc_html(get_theme_mod('seo_enterprise_slider_short_heading')); ?></p>
                        <?php endif; ?>
                        <?php if (get_theme_mod('seo_enterprise_show_slider_title', true)) : ?>
                          <h1 class="mb-md-3 mb-0">
                            <?php 
                                $seo_enterprise_title = explode(' ', get_the_title()); // Split title into words
                                if (count($seo_enterprise_title) >= 4) {
                                    $seo_enterprise_title[0] = '<span class="slider-highlight">' . esc_html($seo_enterprise_title[0]) . '</span>'; // Highlight 1st word
                                    $seo_enterprise_title[3] = '<span class="slider-highlight">' . esc_html($seo_enterprise_title[3]) . '</span>'; // Highlight 4th word
                                }
                                $seo_enterprise_final_title = implode(' ', $seo_enterprise_title); // Rebuild title
                            ?>
                            <a href="<?php echo esc_url(get_permalink()); ?>" class="text-capitalize"><?php echo wp_kses_post($seo_enterprise_final_title); ?></a>
                          </h1>
                        <?php endif; ?>
                        <?php if (get_theme_mod('seo_enterprise_show_slider_content', true)) : ?>
                          <p class="mb-0 slide-content"><?php $seo_enterprise_excerpt = get_the_excerpt(); echo esc_html( seo_enterprise_string_limit_words( $seo_enterprise_excerpt, esc_attr(get_theme_mod('seo_enterprise_slider_excerpt_length','20')))); ?></p>
                        <?php endif; ?>
                        <div class="more-btn mt-4">
                          <a class="text-capitalize me-2 mb-3 slider-btn1" href="<?php the_permalink(); ?>" target="_blank">
                              <?php esc_html_e( 'see case studies', 'seo-enterprise' ); ?>
                          </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 the_title_attribute(); ?>" />
                    <?php else : ?>
                      <img src="<?php echo esc_url($seo_enterprise_static_image); ?>" alt="<?php esc_attr_e('Slider Image', 'seo-enterprise'); ?>" />
                    <?php endif; ?>
                  </div>
                </div>
              </div>
            <?php endwhile;
            wp_reset_postdata();
          else : ?>
            <div class="no-postfound"><?php esc_html_e('No posts found', 'seo-enterprise'); ?></div>
          <?php endif;
        endif; ?>
      </div>
    </div>
    <div class="clearfix"></div>
  </section>
<?php endif; ?>