<?php
/**
 * Template part for displaying slider section
 *
 * @package Business Accountant
 * @subpackage business_accountant
 */

$business_accountant_static_image = get_stylesheet_directory_uri() . '/assets/images/header_img.png';
$business_accountant_slider_arrows = get_theme_mod('business_accountant_slider_arrows', true);
?>

<?php if (!empty($business_accountant_slider_arrows)) { ?>
  <section id="slider">
      <div class="owl-carousel owl-theme">
        <?php 
        $business_accountant_slide_pages = array();
        for ($business_accountant_count = 1; $business_accountant_count <= 4; $business_accountant_count++) {
          $mod = intval(get_theme_mod('business_accountant_slider_page' . $business_accountant_count));
          if ('page-none-selected' != $mod) {
            $business_accountant_slide_pages[] = $mod;
          }
        }

        if (!empty($business_accountant_slide_pages)) :
          $business_accountant_args = array(
            'post_type' => 'page',
            'post__in' => $business_accountant_slide_pages,
            'orderby' => 'post__in'
          );
          $business_accountant_query = new WP_Query($business_accountant_args);
          if ($business_accountant_query->have_posts()) :
            while ($business_accountant_query->have_posts()) : $business_accountant_query->the_post(); ?>
              <div class="item">
                <div class="row">
                  <div class="col-lg-6 col-md-7 col-12 slider-content-col align-self-center">
                    <div class="carousel-caption">
                      <div class="inner_carousel">
                        <?php
                        $business_accountant_title = get_the_title();
                        $business_accountant_title_words = explode(' ', $business_accountant_title);
                        $business_accountant_word_count = count($business_accountant_title_words);

                        if ($business_accountant_word_count > 3) {
                          $business_accountant_start_index = floor(($business_accountant_word_count - 3) / 2);
                          $business_accountant_highlighted_title = '';

                          foreach ($business_accountant_title_words as $business_accountant_index => $business_accountant_word) {
                            if ($business_accountant_index >= $business_accountant_start_index && $business_accountant_index < $business_accountant_start_index + 3) {
                              $business_accountant_highlighted_title .= '<span class="highlighted-word">' . esc_html($business_accountant_word) . '</span>';
                            } else {
                              $business_accountant_highlighted_title .= esc_html($business_accountant_word);
                            }

                            if ($business_accountant_index < $business_accountant_word_count - 1) {
                              $business_accountant_highlighted_title .= ' ';
                            }
                          }
                        } else {
                          $business_accountant_highlighted_title = esc_html($business_accountant_title);
                        }
                        ?>

                        <?php if (get_theme_mod('business_accountant_show_slider_title', true)) : ?>
                          <h1 class="mb-2"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                        <?php endif; ?>
                        <?php if (get_theme_mod('business_accountant_show_slider_content', true)) : ?>
                          <p class="mb-2"><?php $business_accountant_excerpt = get_the_excerpt(); echo esc_html( business_accountant_string_limit_words( $business_accountant_excerpt, esc_attr(get_theme_mod('business_accountant_slider_excerpt_length','50')))); ?></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', 'business-accountant'); ?></a>
                          <?php 
                          $business_accountant_product_btn_text1 = get_theme_mod('business_accountant_product_btn_text1', 'Contact Us');
                          $business_accountant_product_btn_link1 = get_theme_mod('business_accountant_product_btn_link1');
                          if (!empty($business_accountant_product_btn_text1) || !empty($business_accountant_product_btn_link1)) { ?>
                            <a class="text-capitalize slider-btn2" href="<?php echo esc_url($business_accountant_product_btn_link1); ?>"><?php echo esc_html($business_accountant_product_btn_text1); ?><span class="screen-reader-text"><?php echo esc_html($business_accountant_product_btn_text1); ?></span></a>
                          <?php } ?>
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="col-lg-6 col-md-5 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', 'business-accountant'); ?>" />
                    <?php } else { ?>
                      <img src="<?php echo esc_url($business_accountant_static_image); ?>" alt="<?php esc_attr_e('Slider Image', 'business-accountant'); ?>" />
                    <?php } ?>
                  </div>
                </div>
              </div>
            <?php endwhile;
            wp_reset_postdata();
          else : ?>
            <div class="no-postfound"><?php esc_html_e('No posts found', 'business-accountant'); ?></div>
          <?php endif;
        endif; ?>
      </div>
      <div class="clearfix"></div>
  </section>
<?php } ?>