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

get_header(); ?>

<main id="skip-content">
  <?php if (get_theme_mod('gym_nutrition_shop_slider_section_setting', false) != '') { ?>
    <section id="top-slider">
      
      <div class="slider-main">
        <?php $gym_nutrition_shop_slide_pages = array();
          for ( $gym_nutrition_shop_count = 1; $gym_nutrition_shop_count <= 3; $gym_nutrition_shop_count++ ) {
            $gym_nutrition_shop_mod = intval( get_theme_mod( 'gym_nutrition_shop_top_slider_page' . $gym_nutrition_shop_count ));
            if ( 'page-none-selected' != $gym_nutrition_shop_mod ) {
              $gym_nutrition_shop_slide_pages[] = $gym_nutrition_shop_mod;
            }
          }
          if( !empty($gym_nutrition_shop_slide_pages) ) :
            $gym_nutrition_shop_args = array(
              'post_type' => 'page',
              'post__in' => $gym_nutrition_shop_slide_pages,
              'orderby' => 'post__in'
            );
            $gym_nutrition_shop_query = new WP_Query( $gym_nutrition_shop_args );
            if ( $gym_nutrition_shop_query->have_posts() ) :
              $i = 1;
        ?>
        <div class="owl-carousel" role="listbox">
          <?php  while ( $gym_nutrition_shop_query->have_posts() ) : $gym_nutrition_shop_query->the_post(); ?>
            <div class="slide-box">
              <div class="row">
                <div class="col-lg-8 col-md-6 align-self-center">
                  <div class="slider-inner-box">
                    <h2 class="m-0"><?php echo esc_html( wp_trim_words( get_the_title(),10 )); ?></h2>
                    <div class="slide-btn mt-4">
                      <a href="<?php the_permalink(); ?>"><?php esc_html_e('View Collection','gym-nutrition-shop'); ?><i class="fas fa-arrow-right ms-3"></i></a>
                    </div>
                  </div>
                </div>
                <div class="col-lg-4 col-md-6 align-self-center">
                  <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>
              </div>
            </div>
          <?php $i++; endwhile;
          wp_reset_postdata();?>
        </div>
        <?php else : ?>
        <div class="no-postfound"></div>
        <?php endif;
        endif;?>
      </div>
    </section>
  <?php }?>

  <?php if (get_theme_mod('gym_nutrition_shop_activities_section_setting', false) != '') { ?>
  <section class="featured py-5">
    <?php if(get_theme_mod('gym_nutrition_shop_services_heading') != ''){ ?>
      <h3 class="main-heading mb-5"><?php echo esc_html(get_theme_mod('gym_nutrition_shop_services_heading')); ?></h3>
    <?php }?>
    <div class="row">
      <?php if(class_exists('woocommerce')){ ?>
        <?php 
        $args = array(
          'post_type' => 'product',
          'product_cat' =>  get_theme_mod('gym_nutrition_shop_product_category'),
          'orderby' =>'date','order' => 'DESC' 
        );
        // If store filter is selected, add taxonomy query
        if (isset($_GET['Productstore']) && !empty($_GET['Productstore'])) {
          $args['tax_query'] = array(
            array(
              'taxonomy' => 'product_store', // replace with your actual taxonomy
              'field'    => 'slug',
              'terms'    => sanitize_text_field($_GET['Productstore']),
            ),
          );
        }
        $loop = new WP_Query( $args );           
        while ( $loop->have_posts() ){
          $loop->the_post(); 
          global $product; ?>
          <div class="col-lg-3 col-md-6">
            <div class="sells-product">
              <div class="mask1">
                <div class="prodimg_box">
                  <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.esc_url(woocommerce_placeholder_img_src()).'" />'; ?>
                  <?php gym_nutrition_shop_show_sale_percentage(); ?>
                  <div class="wishlist-quick">
                    <?php if(class_exists('YITH_WCWL')){ ?>
                      <span class="wishlist"><?php echo do_shortcode('[yith_wcwl_add_to_wishlist]'); ?></span>
                    <?php }?>
                    <?php if(class_exists('YITH_WCQV_Frontend')){ ?>
                      <span class="quick-view"><?php echo do_shortcode('[yith_quick_view]'); ?></span>
                    <?php }?>
                  </div>
                </div>
                <div class="text_box">
                  <h4 class="mb-2"><a href="<?php echo esc_url(get_permalink( $loop->post->ID )); ?>"><?php the_title(); ?></a></h4>
                  <?php if( $product->is_type( 'simple' ) ){ woocommerce_template_loop_rating( $loop->post, $product ); } ?>
                  <p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>"><?php echo $product->get_price_html(); ?></p>
                  <div class="buy-btn">
                    <a href="<?php the_permalink(); ?>"><?php esc_html_e('Buy Now','gym-nutrition-shop'); ?><i class="fas fa-arrow-right ms-3"></i></a>
                  </div>
                </div>
              </div>
            </div>
          </div>
        <?php  } wp_reset_postdata(); ?>
      <?php }?>
    </div>
  </section>
  <?php }?>

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

<?php get_footer(); ?>