<?php
/**
 * Template Name: Custom Home Page
 */
get_header(); ?>

<main id="content">
  <?php if( get_option('boutique_designer_shop_slider_arrows', false) !== 'off'){ ?>
    <section id="slider">
      <div class="container-md px-md-0 px-sm-5 px-3">
        <div id="carouselExampleAutoplaying" class="carousel slide" data-bs-ride="carousel"> 
          <?php $boutique_designer_shop_slider_count = get_theme_mod('boutique_designer_shop_slider_count'); ?>
              <?php
                for ( $i = 1; $i <= $boutique_designer_shop_slider_count; $i++ ) {
            $mod =  get_theme_mod( 'boutique_designer_shop_post_setting' . $i );
            if ( 'page-none-selected' != $mod ) {
              $boutique_designer_shop_slide_post[] = $mod;
            }
          }
          if (!empty($boutique_designer_shop_slide_post)) :
              $args = array(
                  'post_type'            => array('post'),
                  'post__in'             => $boutique_designer_shop_slide_post,
                  'ignore_sticky_posts'  => true, // Exclude sticky posts by default
              );

            // Check if specific posts are selected
            if (empty($boutique_designer_shop_slide_post) && is_sticky()) {
                $args['post__in'] = get_option('sticky_posts');
            }

            $query = new WP_Query($args);
            if ($query->have_posts()) :
                $i = 1; ?>
              <div class="carousel-inner" role="listbox">
                <?php  while ( $query->have_posts() ) : $query->the_post(); ?>
                <div <?php if($i == 1){echo 'class="carousel-item active"';} else{ echo 'class="carousel-item"';}?>>
                  <?php if(has_post_thumbnail()){ ?>
                    <img class="my-5" src="<?php the_post_thumbnail_url('full'); ?>"/>
                  <?php }else{?>
                    <img class="my-5" src="<?php echo esc_url(get_template_directory_uri()); ?>/assets/images/slider.png" alt="" />
                  <?php } ?>
                  <div class="carousel-caption slide-inner">
                    <?php if( get_theme_mod('boutique_designer_shop_slide_heading') != ''){ ?>
                      <h3><?php echo esc_html(get_theme_mod('boutique_designer_shop_slide_heading','')); ?></h3>
                    <?php }?>
                    <h2 class="slider-title"><?php the_title();?></h2>
                    <?php if( get_option('boutique_designer_shop_slider_excerpt_show_hide',false) != 'off'){ ?>
                      <p class="slider-excerpt mb-0"><?php echo wp_trim_words(get_the_content(), get_theme_mod('boutique_designer_shop_slider_excerpt_count',30) );?></p>
                    <?php } ?>
                    <div class="home-btn my-lg-4 my-md-4 my-3">
                      <a class="py-md-3 px-md-4 py-1 px-3" href="<?php the_permalink(); ?>"><?php echo esc_html(get_theme_mod('boutique_designer_shop_slider_read_more',__('READ MORE','boutique-designer-shop'))); ?></a>
                    </div>
                  </div>
                </div>
                <?php $i++; endwhile;
                wp_reset_postdata();?>
              </div>
            <?php else : ?>
              <div class="no-postfound"></div>
            <?php endif;
          endif;?>
          <a class="carousel-control-prev" type="button" data-bs-target="#carouselExampleAutoplaying" data-bs-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"><i class="fas fa-long-arrow-alt-left"></i></span>
          </a>
          <a class="carousel-control-next" type="button" data-bs-target="#carouselExampleAutoplaying" data-bs-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"><i class="fas fa-long-arrow-alt-right"></i></span>
          </a>
        </div>
      </div>
      <div class="clearfix"></div>
    </section>
  <?php }?>
  <?php if( get_option('boutique_designer_shop_product_enable', false) !== 'off'){ ?>
    <section id="product-section" class="py-5">
      <div class="container">
          <div class="product-heading text-center wow fadeInUpBig">
            <?php if( get_theme_mod('boutique_designer_shop_product_section_sub_heading') != '' ){ ?>
              <h6><?php echo esc_html(get_theme_mod('boutique_designer_shop_product_section_sub_heading')); ?></h6>
            <?php }?>
            <?php if( get_theme_mod('boutique_designer_shop_product_section_heading') != '' ){ ?>
              <h3><?php echo esc_html(get_theme_mod('boutique_designer_shop_product_section_heading')); ?></h3>
            <?php }?>
          </div>
          <div class="row mt-5">
            <?php
            $boutique_designer_shop_catData = get_theme_mod('boutique_designer_shop_product_category');
            $boutique_designer_shop_count_catData = get_theme_mod('boutique_designer_shop_product_category_number');
            $boutique_designer_shop_product_order = get_theme_mod('boutique_designer_shop_product_order_type','ascending');
            if ( class_exists( 'WooCommerce' ) ) {
            $boutique_designer_shop_args = array(
              'post_type' => 'product',
              'posts_per_page' => $boutique_designer_shop_count_catData,
              'product_cat' => $boutique_designer_shop_catData,
              'order'          => 'ASC', // Default order
            );
            // Adjust ordering based on user selection
              if ($boutique_designer_shop_product_order == 'descending') {
                $boutique_designer_shop_args['order'] = 'DESC';
              } else if ($boutique_designer_shop_product_order == 'a-to-z') {
                $boutique_designer_shop_args['orderby'] = 'title';
                $boutique_designer_shop_args['order'] = 'ASC';
              } else if ($boutique_designer_shop_product_order == 'z-to-a') {
                $boutique_designer_shop_args['orderby'] = 'title';
                $boutique_designer_shop_args['order'] = 'DESC';
              }
            $loop = new WP_Query( $boutique_designer_shop_args );
            while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
              <div class="col-lg-3 col-md-6 col-sm-6 text-center">
                <div class="product-img wrapper mb-3 wow zoomIn">
                  <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, ''); else echo '<img src="'.esc_url(wc_placeholder_img_src()).'" />'; ?>
                  <div class="box-content">
                    <?php if( $product->is_type( 'simple' ) ) { woocommerce_template_loop_add_to_cart(  $loop->post, $product );} ?>
                  </div>
                  <div class="sale-tag">
                    <span><?php woocommerce_show_product_sale_flash( $post, $product ); ?></span>
                  </div>
                  <div class="product-details text-center py-2">
                    <h4><a href="<?php echo esc_url(get_permalink( $loop->post->ID )); ?>"><?php the_title(); ?></a></h4>
                    <span><?php esc_attr( apply_filters( 'woocommerce_product_price_class', '' ) ); ?><?php echo ( $product->get_price_html()); ?></span>
                  </div>
                </div>
              </div>
            <?php endwhile; wp_reset_query(); ?>
            <?php } ?>
          </div>
        
      </div>
    </section>
  <?php } ?>
  <section id="custom-page-content" <?php if ( have_posts() && trim( get_the_content() ) !== '' ) echo 'class="pt-3"'; ?>>
    <div class="container">
      <?php while ( have_posts() ) : the_post(); ?>
        <?php the_content(); ?>
      <?php endwhile; ?>
    </div>
  </section>
</main>

<?php get_footer(); ?>