<?php 
if(get_theme_mod('selling_area_disable') != 'on' ){
	?>
	<?php 
		if( get_theme_mod('selling_areaTpadding',true) ) {
			$selling_areaTpadding = 'padding-top:'.esc_attr(get_theme_mod('selling_areaTpadding')).';';
		}
		if( get_theme_mod('selling_areaBpadding',true) ) {
			$selling_areaBpadding = 'padding-bottom:'.esc_attr(get_theme_mod('selling_areaBpadding')).';';
		}
	?>			
	<div class="selling-area " id="selling">		  
		<div class="container" >
			<?php
				$showStatic = true;
				for( $i = 1; $i < 11; $i++ ){
					$sellingus_page_id = get_theme_mod('sellingus_page_icon'.$i); 
					if(!empty($sellingus_page_id)){
						$showStatic = false;
						break;
					}
				}
			?>
      <?php
          $selling_title = get_theme_mod('selling_title', 'Our Shop');  
          $selling_text = get_theme_mod('selling_text', 'Discover the Southern Flavors Within Our Wines');  
      ?>
        <?php if( $selling_title || $selling_text){ ?>
        	<div class="section-title" >
        		<?php if( $selling_title){ ?>
              <h2><?php echo ($selling_title);  ?></h2>
             <?php } ?>
            <?php if( $selling_text){ ?>
              <p><?php echo ($selling_text);  ?></p>
             <?php } ?>
        </div>   
        <?php } ?>
		<div class="sellingus-post-wrap">
			<div class="sellingus-post-boxes">
				<div class="swiper-container parent-slider">
					 <div class="swiper-wrapper">
				    <?php
				    $args = array(
				        'post_type' => 'product',
				        'meta_key' => 'total_sales',
				        'orderby' => 'meta_value_num',
				        'order' => 'DESC',
				        'posts_per_page' => 15,
				    );
				    $loop = new WP_Query($args);
				    while ($loop->have_posts()) : $loop->the_post();
				        global $product;
				        $discount_text = get_post_meta(get_the_ID(), '_custom_product_discount_field', true);
				        // Get gallery image IDs
				        $gallery_ids = $product->get_gallery_image_ids();

				        // Get product categories
				        $categories = get_the_terms($product->get_id(), 'product_cat');
				        if ($categories && !is_wp_error($categories)) {
				            $category_names = array();
				            foreach ($categories as $category) {
				                $category_names[] = $category->name;
				            }
				        }
				    ?>
			        <div class="swiper-slide sellingbx" data-wow-duration="1s">
			            <div class="sellingus-single">
			                <div class="selling-box">
			                    <div class="hi-icon">
				                  <div class="product-gallery">
				                    <div class="swiper-container child-slider">
				                    	<div class="swiper-wrapper">
					                    	<div class="swiper-slide">
					                    		<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
						                    		<?php
							                            if (has_post_thumbnail($loop->post->ID)) {
							                                echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
							                            } else {
							                                echo '<img src="' . get_template_directory_uri() . '/images/default.png" alt="selling products" />';
							                            }
						                            ?>
						                      </a> 
					                    	</div>
				                    		<?php
											    // Get the product gallery images
											    $gallery_ids = $product->get_gallery_image_ids();

											    if (!empty($gallery_ids)) :
											        foreach ($gallery_ids as $gallery_id) : ?>
											            <div class="swiper-slide">
											                <a id="id-<?php the_ID(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
											                    <?php
											                    // Display the gallery image thumbnail
											                    echo wp_get_attachment_image($gallery_id, 'shop_catalog');
											                    ?>
											                </a>
											            </div>
											        <?php endforeach;
											    endif;
											?>
						                </div>
						                <div class="swiper-pagination swiper-pagination-child"></div>
										<div class="swiper-button-prev"><i class="fa fa-caret-left"></i></div>
										<div class="swiper-button-next"><i class="fa fa-caret-right"></i></div>
				                    </div>
			                    </div>
			                    <?php  if (!empty($discount_text)) : ?>
				                    <div class="product-discount_label">
											<span class="product-discount"><?php echo ($discount_text);  ?></span>
									</div>
								<?php endif ?>
								<div class="selling-icons">
									<div class="selling-list tooltip-container">
										<?php 
											$user_id = get_current_user_id();
											$product_id = $product->get_id();
											$wishlist = get_user_meta($user_id, 'wishlist', true);
											$button_class = in_array($product_id, (array)$wishlist) ? 'added' : '';
											$nonce = wp_create_nonce('update_wishlist_' . $product_id);

											$wishlist_url = esc_url(add_query_arg(
											    array(
											        'update_wishlist' => $product_id,
											        '_wpnonce' => $nonce
											    )
											));

											echo '<a href="' . $wishlist_url . '" class="add-to-wishlist-button ' . $button_class . '">
											    <span class="spinner"></span>
											    <i class="fa fa-heart' . ($button_class ? '' : '-o') . '" aria-hidden="true"></i>
											    <span class="tooltip-text">' . ($button_class ? 'Added to Wishlist' : 'Add to Wishlist') . '</span>
											</a>'; 
										?>
									</div>
								    <div class="selling-list tooltip-container">
								        <a href="#quick-view-<?php the_id(); ?>" class="quick-view" data-product-id="<?php echo esc_attr( $product->get_id() ); ?>">
								            <span class="spinner"></span><i class="fa fa-eye"></i><span class="tooltip-text">Quick View</span>
								        </a>
								    </div>
								</div>
		                    </div>
		                </div>
		                <div class="selling-area-data">
		                    <div class="selling-btn">
		                        <?php
		                        	woocommerce_template_loop_add_to_cart($product);
		                        ?>
		                    </div>
		                    <?php if (!empty($category_names)) : ?>
		                        <div class="product-categories"><?php echo implode(', ', $category_names); ?></div>
		                    <?php endif; ?>
		                    <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
		                        <h4 class="inner-area-title"><?php the_title(); ?></h4>
		                    </a>
		                    <div class="price">
		                        <div class="selling-price"><?php echo $product->get_price_html(); ?></div>
		                    </div>
		                </div>
		            </div>
		        </div>
			<?php endwhile;
				wp_reset_query(); ?>
			</div>
			<div class="swiper-pagination swiper-pagination-parent"></div>
		</div>
	</div>
	</div>
</div>
</div>

 <script>
    new Swiper("#selling .parent-slider", {
    loop: true,
    slidesPerView: 3,
    noSwiping: true,
    //centeredSlides: true,
    spaceBetween: 9,
    pagination: {
        el: '.swiper-pagination.swiper-pagination-parent',
        type: 'bullets', // Dots pagination
        clickable: true, // Allow navigation by clicking on pagination dots
    },
    breakpoints: {
	        320: {
	            slidesPerView: 1,
	            spaceBetween: 7,
	            resistanceRatio: 0.85
	        },
	        475: {
	            slidesPerView: 2,
	            spaceBetween: 7,
	            resistanceRatio: 0.85
	        },
	        992: {
	            slidesPerView: 3
	        }
	    },
	});


    new Swiper(".child-slider", {
	    loop: true,
	    slidesPerView: 1,
	    noSwiping: false,
	    pagination: '.swiper-pagination-child',
	    navigation: {
	        nextEl: '.swiper-button-next',
	        prevEl: '.swiper-button-prev',
	      },
	});
  </script>
<?php }  