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

?>
<!-- banner section -->
<main id="maincontent" role="main">
  <?php if( get_theme_mod( 'vw_wholesaler_show_hide_banner', false) == 1 || get_theme_mod( 'vw_wholesaler_resp_slider_hide_show', false) == 1) { ?>
    <section id="banner" class="position-relative wow bounceInDown delay-1000" data-wow-duration="3s">
      <div class="container">
        <div class="row">
          <div class="col-xl-6 col-lg-6 col-md-6 col-12 align-self-center">
            <div class="inner_carousel text-start">
              <div class="banner-content">
                <div class="position-relative">
                  <?php if(get_theme_mod('vw_wholesaler_banner_small_text') != '') {?>
                    <p class="small-title text-capitalize"><?php echo esc_html(get_theme_mod('vw_wholesaler_banner_small_text')) ?></p>
                  <?php }?>
                  <?php if(get_theme_mod('vw_wholesaler_banner_bg_text') != '') {?>
                    <p class="banner-text text-capitalize position-absolute mb-0"><?php echo esc_html(get_theme_mod('vw_wholesaler_banner_bg_text')) ?></p>
                  <?php }?>
                </div>
                <?php if(get_theme_mod('vw_wholesaler_banner_title') != '') {?>
                  <h1 class="banner-title mb-3 text-capitalize"><?php echo esc_html(get_theme_mod('vw_wholesaler_banner_title')) ?></h1>
                <?php }?>
                <?php if(get_theme_mod('vw_wholesaler_banner_para_text') != '') {?>
                  <p class="banner-para"><?php echo esc_html(get_theme_mod('vw_wholesaler_banner_para_text')) ?></p>
                <?php }?>
                <?php if ( get_theme_mod('vw_wholesaler_explore_button_label','') != '' ) {?>
                  <div class ="explore-btn">
                    <a href="<?php echo esc_url(get_theme_mod('vw_wholesaler_explore_button_url',false));?>" class="text-capitalize"><?php echo esc_html(get_theme_mod('vw_wholesaler_explore_button_label',''));?><span class="screen-reader-text"><?php echo esc_html(get_theme_mod('vw_wholesaler_explore_button_label',''));?></span><i class="<?php echo esc_attr(get_theme_mod('vw_wholesaler_explore_btn_icon','fa-solid fa-angle-right')); ?>"></i>
                    </a>
                  </div>
                <?php }?>
                <div class="row">
                  <div class="col-xl-10 col-lg-10 col-md-9 col-sm-9 col-12 align-self-center pe-0">
                    <?php if( get_theme_mod( 'vw_wholesaler_display_search', true) == true ) { ?>
                      <?php if(class_exists('woocommerce')):?>
                        <?php get_product_search_form(); ?>
                      <?php else : ?>
                        <?php get_search_form(); ?>
                      <?php endif; ?>
                    <?php } ?>
                  </div>
                  <div class="col-xl-2 col-lg-2 col-md-3 col-sm-3 col-12 align-self-center">
                    <?php if(class_exists('woocommerce')){ ?>
                      <div class=" position-relative">
                        <button class="product-btn"><?php esc_html_e('Category','vw-wholesaler'); ?><i class="fa-solid fa-angle-right"></i></button>
                          <div class="product-cat">
                            <?php
                              $vw_wholesaler_args = array(
                                'orderby'    => 'title',
                                'order'      => 'ASC',
                                'hide_empty' => 0,
                                'parent'  => 0
                              );
                              $vw_wholesaler_product_categories = get_terms( 'product_cat', $vw_wholesaler_args );
                              $vw_wholesaler_count = count($vw_wholesaler_product_categories);
                              if ( $vw_wholesaler_count > 0 ){
                                  foreach ( $vw_wholesaler_product_categories as $vw_wholesaler_product_category ) {
                                    $vw_wholesaler_product_cat_id = $vw_wholesaler_product_category->term_id;
                                    $vw_wholesaler_cat_link = get_category_link( $vw_wholesaler_product_cat_id );
                                    if ($vw_wholesaler_product_category->category_parent == 0) { ?>
                                  <li class="drp_category"><a href="<?php echo esc_url(get_term_link( $vw_wholesaler_product_category ) ); ?>">
                                  <?php
                                }
                                echo esc_html( $vw_wholesaler_product_category->name ); ?></a><i class="fas fa-chevron-right"></i></li>
                            <?php } } ?>
                          </div>
                      </div>
                    <?php }?>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="col-xl-6 col-lg-6 col-md-6 col-12 align-self-center">
            <div class="featured-images-wrapper">
              <?php for ($vw_wholesaler_j = 1; $vw_wholesaler_j <= 9; $vw_wholesaler_j++) { ?>
                <?php if (get_theme_mod('vw_wholesaler_featured_image_sec' . $vw_wholesaler_j) != "") { ?>
                  <?php if ($vw_wholesaler_j == 1 || $vw_wholesaler_j == 4 || $vw_wholesaler_j == 7) { ?>
                    <div class="featured-row row-<?php echo $vw_wholesaler_j; ?>">
                  <?php } ?>
                  <div class="featured-image-wrapper">
                    <img class="featured-img" src="<?php echo esc_url(get_theme_mod('vw_wholesaler_featured_image_sec' . $vw_wholesaler_j)); ?>" alt="<?php echo esc_attr(get_theme_mod('vw_wholesaler_featured_title' . $vw_wholesaler_j, true)); ?>" title="#slidecaption<?php echo esc_html($vw_wholesaler_j); ?>">
                  </div>
                  <?php if ($vw_wholesaler_j % 3 == 0) { ?>
                      </div>
                  <?php } ?>
                <?php } ?>
              <?php } ?>
            </div>
          </div>
        </div>
      </div>
      <div class="clearfix"></div>
    </section>
  <?php }?>
  <?php do_action( 'vw_wholesaler_after_banner' ); ?>

  <!-- Category Section -->
  <?php if (get_theme_mod('vw_wholesaler_hide_show_category_section', false)) {?>
    <section id="category-section" class="py-5 wow bounceInRight delay-1000" data-wow-duration="3s">
      <div class="container">
        <div class="about-text text-left mb-4">
          <div class="row">
            <div class="col-xl-9 col-lg-9 col-md-8 col-12 align-self-center">
              <?php if(get_theme_mod('vw_wholesaler_section_text') != ''){ ?>
                <p class="category-text mb-1 text-capitalize"><?php echo esc_html(get_theme_mod('vw_wholesaler_section_text')); ?></p>
              <?php } ?>
              <?php if(get_theme_mod('vw_wholesaler_section_heading') != ''){ ?>
                <h2 class="category-title"><?php echo esc_html(get_theme_mod('vw_wholesaler_section_heading')); ?></h2>
              <?php } ?>
            </div>
          </div>
        </div>
        <?php if (class_exists('woocommerce')) { ?>
          <div class="row">
            <div class="col-xxl-3 col-xl-3 col-lg-3 col-md-4 col-12 text-lg-start text-md-start text-center">
              <div class="category-box">
                <button class="category-btn"><?php esc_html_e('Category', 'vw-wholesaler'); ?></button>
                <div class="product-category">
                  <?php
                    $vw_wholesaler_args = array(
                      'orderby'    => 'title',
                      'order'      => 'ASC',
                      'hide_empty' => 0,
                      'parent'     => 0
                    );
                    $vw_wholesaler_product_categories = get_terms('product_cat', $vw_wholesaler_args);
                    $vw_wholesaler_count = count($vw_wholesaler_product_categories);

                    if ($vw_wholesaler_count > 0) {
                      foreach ($vw_wholesaler_product_categories as $vw_wholesaler_product_category) {
                        $vw_wholesaler_product_cat_id = $vw_wholesaler_product_category->term_id;
                        $vw_wholesaler_cat_link = get_term_link($vw_wholesaler_product_category);
                        $vw_wholesaler_category_icon_html = get_term_meta($vw_wholesaler_product_cat_id, 'category_icon_html', true);
                        if ($vw_wholesaler_category_icon_html) {
                          $vw_wholesaler_icon_html = $vw_wholesaler_category_icon_html;
                        } else {
                          $vw_wholesaler_icon_html = '';
                        }
                        if ($vw_wholesaler_product_category->category_parent == 0) { ?>
                          <li class="drp_dwn_menu" data-category-id="<?php echo esc_attr($vw_wholesaler_product_cat_id); ?>">
                            <a href="<?php echo esc_url($vw_wholesaler_cat_link); ?>">
                              <?php echo $vw_wholesaler_icon_html; ?>
                              <?php echo esc_html($vw_wholesaler_product_category->name); ?>
                            </a>
                          </li>
                        <?php }
                      }
                    }
                  ?>
                </div>
              </div>
            </div>
            <div class="col-xxl-9 col-xl-9 col-lg-9 col-md-8 col-12">
              <div id="category-products">
                  <?php
                  $vw_wholesaler_args = array(
                    'post_type' => 'product',
                    'posts_per_page' => -1,
                    'post_status' => 'publish',
                    'tax_query' => array(
                      array(
                        'taxonomy' => 'product_cat',
                        'field' => 'id',
                        'terms' => array_map(function($vw_wholesaler_term) { return $vw_wholesaler_term->term_id; }, $vw_wholesaler_product_categories),
                        'operator' => 'IN'
                      )
                    )
                  );
                  $vw_wholesaler_product_query = new WP_Query($vw_wholesaler_args);
                  if ($vw_wholesaler_product_query->have_posts()) {
                    echo '<div class="row">';
                    while ($vw_wholesaler_product_query->have_posts()) {
                      $vw_wholesaler_product_query->the_post();
                      global $product;
                      ?>
                      <div class="col-xl-4 col-lg-4 col-md-6 col-12 mb-4 cat-product category-<?php echo get_the_terms(get_the_ID(), 'product_cat')[0]->term_id; ?>">
                        <div class="product-card">
                          <div class="product-image">
                            <?php 
                              if (has_post_thumbnail()) {
                                echo get_the_post_thumbnail(get_the_ID(), 'shop_catalog');
                              } else {
                                echo '<img src="' . esc_url(woocommerce_placeholder_img_src()) . '" alt="Placeholder" />';
                              }
                            ?>
                          </div>
                          <div class="product-info px-3 pt-2 pb-3">
                            <h3 class="product-title mb-0">
                              <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            </h3>
                            <span>
                              <?php 
                                if( $product->is_type( 'simple' ) ) { 
                                  woocommerce_template_loop_rating( $vw_wholesaler_product_query->post, $product ); 
                                } 
                              ?>
                            </span>
                            <div class="row">
                              <div class="col-xxl-6 col-xl-6 col-lg-5 col-md-5 col-6 align-self-center">
                                <p class="product-price mb-0">
                                  <?php echo wc_price( $product->get_regular_price() ); ?>
                                </p>
                              </div>
                              <div class="col-xxl-6 col-xl-6 col-lg-7 col-md-7 col-6 align-self-center text-end">
                                <a href="<?php the_permalink(); ?>" class="product-link-button"><?php esc_html_e( 'Order Now', 'vw-wholesaler' ); ?></a>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                      <?php
                    }
                    echo '</div>';
                  } else {
                    echo '<p>No products found in this category.</p>';
                  }
                  wp_reset_postdata();
                  ?>
              </div>
            </div>
          </div>
        <?php } ?>
      </div>
    </section>
  <?php }?>

  <div id="content-vw" class="entry-content">
    <div class="container">
      <?php while (have_posts()) : the_post(); ?>
        <?php the_content(); ?>
      <?php endwhile; // end of the loop. 
      ?>
    </div>
  </div>
  
</main>

<?php get_footer(); ?> 