<?php
/**
 * Template part for displaying slider section
 *
 * @package Home Interior Decor
 * @subpackage home_interior_decor
 */

?>
<?php $home_interior_decor_static_image = get_stylesheet_directory_uri() . '/assets/images/sliderimage.png'; ?>
<section id="main-slider" class="p-md-3 p-2 mt-md-3">
  <div class="container-fluid">
    <div class="row">
      <div class="col-lg-3 col-md-3">
        <div class="slide-banner">
          <?php if( get_theme_mod( 'home_interior_decor_discount_sale_img1') != '') { ?>
            <div class="banner-1 mb-3 position-relative">
              <div class="product-img">
                <img src="<?php echo esc_url(get_theme_mod('home_interior_decor_discount_sale_img1')); ?>" alt="<?php echo esc_attr(get_theme_mod('home_interior_decor_topproduct_title1', 'Product Image')); ?>" />
                <div class="product-content first">
                  <?php if (get_theme_mod('home_interior_decor_product_sale_discount_title1') !== '') : ?>
                    <h2 class="my-2"><?php echo esc_html(get_theme_mod('home_interior_decor_product_sale_discount_title1')); ?></h2>
                  <?php endif; ?>
                  <div class="product-btn mt-3" data-wow-duration="2s">
                    <?php if(get_theme_mod('home_interior_decor_product_btn_text1') != ''){ ?>
                      <a href="<?php echo esc_url(get_theme_mod('home_interior_decor_product_btn_link1')); ?>"><?php echo esc_html(get_theme_mod('home_interior_decor_product_btn_text1')); ?><span class="screen-reader-text"><?php echo esc_html(get_theme_mod('home_interior_decor_product_btn_text1')); ?></span></a>
                    <?php }?>
                  </div>
                </div>
                <?php if (get_theme_mod('home_interior_decor_product_sale_discount') !== '') : ?>
                <div class="top-discount">
                  <p class="discount-text mb-0"><?php echo esc_html(get_theme_mod('home_interior_decor_product_sale_discount')); ?></p>
                </div>
                <?php endif; ?>
              </div>
            </div>
          <?php } ?>
        </div>
      </div>
      <div class="col-lg-6 col-md-6 px-md-0 px-lg-2">
        <?php if (get_theme_mod('home_interior_decor_slider_arrows', true) != '') : ?>
          <div id="slider" class="mb-md-0 mb-3">
            <div id="owl-carousel" class="owl-carousel">
              <?php
              $home_interior_decor_slide_pages = array();
              for ($home_interior_decor_count = 1; $home_interior_decor_count <= 4; $home_interior_decor_count++) {
                  $home_interior_decor_mod = intval(get_theme_mod('home_interior_decor_slider_page' . $home_interior_decor_count, 0));
                  if ($home_interior_decor_mod > 0) {
                      $home_interior_decor_slide_pages[] = $home_interior_decor_mod;
                  }
              }

              if (!empty($home_interior_decor_slide_pages)) :
                  $home_interior_decor_args = array(
                      'post_type' => 'page',
                      'post__in' => $home_interior_decor_slide_pages,
                      'orderby' => 'post__in'
                  );
                  $home_interior_decor_query = new WP_Query($home_interior_decor_args);
                  if ($home_interior_decor_query->have_posts()) :
                      while ($home_interior_decor_query->have_posts()) : $home_interior_decor_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($home_interior_decor_static_image); ?>" alt="<?php esc_attr_e('Default Image', 'home-interior-decor'); ?>" />
                                  <?php } ?>
                              </div>
                              <div class="carousel-caption">
                                  <div class="inner_carousel">
                                      <?php $home_interior_decor_short_heading = get_theme_mod('home_interior_decor_slider_short_heading', '');
                                      if (!empty($home_interior_decor_short_heading)) { ?>
                                          <p class="slidetop-text mb-2"><?php echo esc_html($home_interior_decor_short_heading); ?></p>
                                      <?php } ?>
                                      <?php if (get_theme_mod('home_interior_decor_show_slider_title', true)) : ?>
                                        <?php
                                        // Get the post title
                                        $home_interior_decor_title = get_the_title();
                                        // Split the title into words
                                        $home_interior_decor_words = explode(' ', $home_interior_decor_title);
                                        // Get the last two words
                                        $home_interior_decor_last_two_words = array_slice($home_interior_decor_words, -2, 2);
                                        // Get the rest of the words
                                        $home_interior_decor_remaining_words = array_slice($home_interior_decor_words, 0, -2);
                                        ?>
                                        <h1 class="my-2">
                                            <a href="<?php the_permalink(); ?>">
                                                <?php echo esc_html(implode(' ', $home_interior_decor_remaining_words)); ?> 
                                                <span class="highlight">
                                                    <?php echo esc_html(implode(' ', $home_interior_decor_last_two_words)); ?>
                                                </span>
                                            </a>
                                        </h1>
                                      <?php endif; ?>
                                      <?php if (get_theme_mod('home_interior_decor_show_slider_content', true)) : ?>
                                        <p class="my-1 slider-content"><?php $home_interior_decor_excerpt = get_the_excerpt(); echo esc_html( home_interior_decor_string_limit_words( $home_interior_decor_excerpt, esc_attr(get_theme_mod('home_interior_decor_slider_excerpt_length','20')))); ?></p>
                                      <?php endif; ?>
                                      <div class="more-btn mt-md-4 mt-2 pb-3">
                                        <?php 
                                            // Get the button text and link from the theme settings.
                                            $home_interior_decor_btn_text1 = get_theme_mod('home_interior_decor_btn_text1', __('EXPLORE MORE', 'home-interior-decor'));
                                            $home_interior_decor_btn_link1 = get_theme_mod('home_interior_decor_btn_link1', '');

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

                                            // Display the button.
                                            if (!empty($home_interior_decor_btn_text1)) { ?>
                                                <a href="<?php echo esc_url($home_interior_decor_button_link); ?>" target="_blank" class="text-capitalize me-2 mb-2 slider-btn1">
                                                    <?php echo esc_html($home_interior_decor_btn_text1); ?>
                                                    <span class="screen-reader-text"><?php echo esc_html($home_interior_decor_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.', 'home-interior-decor'); ?></div>
                  <?php endif;
              endif; ?>
            </div>
          </div>
        <?php endif; ?>
      </div>
      <div class="col-lg-3 col-md-3">
        <?php if( get_theme_mod( 'home_interior_decor_discount_sale_img2') != '') { ?>
          <div class="banner-2">
            <div class="product-img">
              <img src="<?php echo esc_url(get_theme_mod('home_interior_decor_discount_sale_img2')); ?>" alt="<?php echo esc_attr(get_theme_mod('home_interior_decor_topproduct_title2', 'Product Image')); ?>" />
              <div class="product-content second">
                <?php
                // Get the discount title from the theme mod
                $discount_title = get_theme_mod('home_interior_decor_product_sale_discount_title2');

                // Split the title into the first word and the rest
                $home_interior_decor_words = explode(' ', $discount_title, 2);
                $first_word = $home_interior_decor_words[0];
                $remaining_text = isset($home_interior_decor_words[1]) ? $home_interior_decor_words[1] : '';
                ?>
                <h2 class="discount-text mb-2 mb-lg-3 text-capitalize">
                    <span class="first-word" style="color: black;"><?php echo esc_html($first_word); ?></span>
                    <?php if ($remaining_text): ?>
                        <span class="remaining-text"><?php echo esc_html($remaining_text); ?></span>
                    <?php endif; ?>
                </h2>
                <p class="prod-content m-0"><?php echo esc_html(get_theme_mod('home_interior_decor_topproduct_title2')); ?></p>
                <div class="product-btn mt-3" data-wow-duration="2s">
                  <?php if(get_theme_mod('home_interior_decor_product_btn_text2') != ''){ ?>
                    <a href="<?php echo esc_url(get_theme_mod('home_interior_decor_product_btn_link2')); ?>"><?php echo esc_html(get_theme_mod('home_interior_decor_product_btn_text2')); ?><span class="screen-reader-text"><?php echo esc_html(get_theme_mod('home_interior_decor_product_btn_text2')); ?></span></a>
                  <?php }?>
                </div>
              </div>
            </div>
          </div>
        <?php } ?>
      </div>
    </div>
  </div>
</section>