<?php
/**
 * Template Name: Home Template
 */

get_header(); ?>

<main id="skip-content">
  <?php if (get_theme_mod('store_eshop_slider_section_setting', true) != '') { ?>
  <section id="top-slider">
    <?php $store_eshop_slide_pages = array();
      for ( $store_eshop_count = 1; $store_eshop_count <= 3; $store_eshop_count++ ) {
        $store_eshop_mod = intval( get_theme_mod( 'store_eshop_top_slider_page' . $store_eshop_count ));
        if ( 'page-none-selected' != $store_eshop_mod ) {
          $store_eshop_slide_pages[] = $store_eshop_mod;
        }
      }
      if( !empty($store_eshop_slide_pages) ) :
        $store_eshop_args = array(
          'post_type' => 'page',
          'post__in' => $store_eshop_slide_pages,
          'orderby' => 'post__in'
        );
        $store_eshop_query = new WP_Query( $store_eshop_args );
        if ( $store_eshop_query->have_posts() ) :
          $i = 1;
    ?>
    <div class="owl-carousel" role="listbox">
      <?php  while ( $store_eshop_query->have_posts() ) : $store_eshop_query->the_post(); ?>
      <div class="slide-box">
        <div class="slider-image">
          <?php if(has_post_thumbnail()){
              the_post_thumbnail();
              } else{?>
          <img src="<?php echo esc_url(get_template_directory_uri()); ?>/assets/img/slider.png" alt="" />
          <?php } ?>
        </div>
        <div class="slider-inner-box">
          <?php if(get_theme_mod('store_eshop_slider_short_heading') != ''){ ?>
          <h5 class="main-heading mb-4">
            <?php echo esc_html(get_theme_mod('store_eshop_slider_short_heading')); ?></h5>
          <?php }?>
          <h3 class="m-0"><?php the_title(); ?></h3>
          <p class="content mt-3"><?php echo esc_html( wp_trim_words( get_the_content(),70 )); ?><p>
              <div class="slide-btn mt-4">
                <a href="<?php the_permalink(); ?>"><?php esc_html_e('Shop Now','store-eshop'); ?></a>
              </div>
        </div>
      </div>
      <?php $i++; endwhile;
      wp_reset_postdata();?>
    </div>
    <?php else : ?>
    <div class="no-postfound"></div>
    <?php endif;
    endif;?>
  </section>
  <?php }?>

  <?php if (get_theme_mod('store_eshop_activities_section_setting', true) != '') { ?>
  <section class="featured py-5">
    <div class="container">
      <div class="row mb-3">
        <div class="col-lg-5 col-md-4 align-self-center">
          <?php if(get_theme_mod('store_eshop_services_heading') != ''){ ?>
          <h3 class="main-heading"><?php echo esc_html(get_theme_mod('store_eshop_services_heading')); ?></h3>
          <?php }?>
        </div>
        <div class="col-lg-5 col-md-5 align-self-center">
          <div class=" men-tabs">
            <ul class="nav-tabs nav justify-content-end" role="tablist">
              <?php 
                  for($i=1; $i<=4; $i++ ) {?>
              <?php if ( get_theme_mod('store_eshop_slideproduct_tab1title'.$i) != "" ) {?>
              <li class="nav-item">
                <a class="nav-link <?php if($i == 1){echo 'active';} ?>" href="#tab<?php echo esc_attr($i);?>"
                  role="tab"
                  data-bs-toggle="tab"><?php echo esc_html(get_theme_mod('store_eshop_slideproduct_tab1title'.$i)); ?></a>
              </li>
              <?php }?>
              <?php }?>
            </ul>
          </div>
        </div>
        <div class="col-lg-2 col-md-3 align-self-center text-lg-end text-center">
          <?php if(get_theme_mod('store_eshop_product_btn_url') != '' || get_theme_mod('store_eshop_product_btn_text') != ''){ ?>
          <a class="product-btn"
            href="<?php echo esc_url(get_theme_mod('store_eshop_product_btn_url')); ?>"><?php echo esc_html(get_theme_mod('store_eshop_product_btn_text')); ?></a>
          <?php }?>
        </div>
      </div>
      <div id="mens-product-wrap">
        <div class="tab-content">
          <!--tab 1 -->
          <?php 
              for($i=1; $i<= 4; $i++ ) {?>
          <div role="tabpanel" class="tab-pane <?php if($i == 1){echo 'active';} ?>" id="tab<?php echo esc_attr($i);?>">
            <?php if(class_exists('woocommerce')){ ?>
            <div class="owl-carousel">
              <?php 
                    $args = array(
                    'post_type' => 'product',
                    'product_cat' =>  get_theme_mod('store_eshop_cate_tab'.$i),
                    'orderby' =>'date','order' => 'DESC' );
                    $loop = new WP_Query( $args );           
                    while ( $loop->have_posts() ){
                    $loop->the_post(); 
                    global $product; ?>
              <div class="sells-product">
                <div class="mask1">
                  <div class="prodimg_box">
                    <a href="<?php echo esc_url(get_permalink( $loop->post->ID )); ?>"
                      title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
                      <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'our_product'); else echo '<img src="'.esc_url(wc_placeholder_img_src()).'" alt="Placeholder" width="300px" height="300px" />'; ?>
                    </a>
                    <?php if ( $product->is_on_sale() ) { ?>
                    <span class="onsale"><?php echo esc_html__('Sale', 'store-eshop'); ?></span>
                    <?php } ?>
                    <?php if(class_exists('YITH_WCWL')){ ?>
                    <span class="wishlist"><?php echo do_shortcode('[yith_wcwl_add_to_wishlist]'); ?></span>
                    <?php }?>
                  </div>
                  <div class="text_box">
                    <h4 class="hidedesktop p-0 mb-2"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                    <div class="row mb-2">
                      <div class="col-lg-8 col-md-8 col-sm-12 col-6 align-self-center">
                        <p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?> mb-0">
                          <?php echo $product->get_price_html(); ?></p>
                      </div>
                      <div class="col-lg-4 col-md-4 col-sm-12 col-6 align-self-center text-end">
                        <?php 
                          if ( $product->is_type( 'simple' ) ) { 
                              $average_rating = $product->get_average_rating();
                              if ( $average_rating > 0 ) {
                                  echo '<span class="woocommerce-average-rating"><i class="fas fa-star"></i>' . number_format($average_rating, 1) . '</span>';
                              } else {
                                  echo '<span class="woocommerce-average-rating">No rating</span>';
                              }
                          }
                          ?>
                      </div>
                    </div>
                    <span class="sale_cart">
                      <?php if( $product->is_type( 'simple' ) ){ woocommerce_template_loop_add_to_cart( $loop->post, $product ); } ?>
                    </span>
                  </div>
                </div>

              </div>
              <?php  } wp_reset_postdata(); ?>
            </div>
            <?php }?>
          </div>
          <?php }?>
        </div>
      </div>
    </div>
  </section>
  <?php }?>
  <section id="page-content">
    <div class="container">
      <div class="py-5">
        <?php
          if ( have_posts() ) :
            while ( have_posts() ) : the_post();
              the_content();
            endwhile;
          endif;
        ?>
      </div>
    </div>
  </section>
</main>

<?php get_footer(); ?>