<?php
/**
 * Template part for displaying slider section
 *
 * @package Industrial Hub
 * @subpackage industrial_hub
 */

$industrial_hub_static_image = get_template_directory_uri() . '/assets/images/sliderimage.png';
?>
<?php if (get_theme_mod('industrial_hub_slider_arrows', true) != '') : ?>
  <div id="slider" class="mb-md-0 mb-3">
    <div id="owl-carousel" class="owl-carousel">
      <?php
      $industrial_hub_slide_pages = array();
      for ($industrial_hub_count = 1; $industrial_hub_count <= 4; $industrial_hub_count++) {
          $industrial_hub_mod = intval(get_theme_mod('industrial_hub_slider_page' . $industrial_hub_count, 0));
          if ($industrial_hub_mod > 0) {
              $industrial_hub_slide_pages[] = $industrial_hub_mod;
          }
      }
      if (!empty($industrial_hub_slide_pages)) :
          $industrial_hub_args = array(
              'post_type' => 'page',
              'post__in' => $industrial_hub_slide_pages,
              'orderby' => 'post__in'
          );
          $industrial_hub_query = new WP_Query($industrial_hub_args);
          if ($industrial_hub_query->have_posts()) :
              while ($industrial_hub_query->have_posts()) : $industrial_hub_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($industrial_hub_static_image); ?>" alt="<?php esc_attr_e('Default Image', 'industrial-hub'); ?>" />
                          <?php } ?>
                      </div>
                      <div class="carousel-caption">
                          <div class="inner_carousel">
                              <?php $industrial_hub_short_heading = get_theme_mod('industrial_hub_slider_short_heading', '');
                              if (!empty($industrial_hub_short_heading)) { ?>
                                  <p class="slidetop-text mb-2"><?php echo esc_html($industrial_hub_short_heading); ?></p>
                              <?php } ?>
                              <?php if (get_theme_mod('industrial_hub_show_slider_title', true)) : ?>
                                <h1 class="my-2">
                                   <a href="<?php the_permalink(); ?>" class="text-capitalize"><?php the_title();?></a>
                                </h1>
                              <?php endif; ?>
                              <?php if (get_theme_mod('industrial_hub_show_slider_content', true)) : ?>
                                <p class="mt-3 slider-content"><?php $industrial_hub_excerpt = get_the_excerpt(); echo esc_html( industrial_hub_string_limit_words( $industrial_hub_excerpt, esc_attr(get_theme_mod('industrial_hub_slider_excerpt_length','20')))); ?></p>
                              <?php endif; ?>
                              <div class="more-btn mt-lg-4 mt-md-4 mt-3">
                                <?php 
                                    // Get the button text and link from the theme settings.
                                    $industrial_hub_btn_text1 = get_theme_mod('industrial_hub_btn_text1', __('Explore More', 'industrial-hub'));
                                    $industrial_hub_btn_link1 = get_theme_mod('industrial_hub_btn_link1', '');

                                    // Fallback to the permalink if no link is provided.
                                    $Industrial_Hub_Button_link = !empty($industrial_hub_btn_link1) ? $industrial_hub_btn_link1 : get_permalink();

                                    if (!empty($industrial_hub_btn_text1)) { ?>
                                        <a href="<?php echo esc_url($Industrial_Hub_Button_link); ?>" target="_blank" class="text-capitalize me-2 mb-2 slider-btn1">
                                            <span class="slider-btn-read"><?php echo esc_html($industrial_hub_btn_text1); ?></span><i class="fas fa-arrow-right"></i>
                                            <span class="screen-reader-text"><?php echo esc_html($industrial_hub_btn_text1); ?></span>
                                        </a>
                                <?php } ?>
                            </div>
                          </div>
                      </div>
                  </div>
              <?php endwhile;
              wp_reset_postdata();
          else : ?>
              <div class="no-postfound"><?php esc_html_e('No slides found.', 'industrial-hub'); ?></div>
          <?php endif;
      endif; ?>
    </div>
  </div>
<?php endif; ?>