<?php
if ( ! get_theme_mod( 'jeweler_shop_enable_product_section', false ) ) {
	return;
}

$jeweler_shop_args = '';
jeweler_shop_render_product_section( $jeweler_shop_args );

/**
 * Render Product Section.
 */
function jeweler_shop_render_product_section( $jeweler_shop_args ) { ?>
		<section id="jeweler_shop_trending_section" class="asterthemes-frontpage-section trending-section trending-style-1 wow zoomInUp" data-wow-duration="1.5s">
		<?php
		if ( is_customize_preview() ) :
			jeweler_shop_section_link( 'jeweler_shop_product_section' );
		endif;

		$jeweler_shop_trending_product_heading = get_theme_mod( 'jeweler_shop_trending_product_heading');
    $jeweler_shop_product_button_label = get_theme_mod( 'jeweler_shop_product_button_label_');
    $jeweler_shop_product_button_link  = get_theme_mod( 'jeweler_shop_product_button_link_');
		?>
		<div class="asterthemes-wrapper">
      <div class="product-section-header">
        <?php if ( ! empty( $jeweler_shop_trending_product_heading ) ) { ?>
          <h3><?php echo esc_html( $jeweler_shop_trending_product_heading ); ?></h3>
        <?php } ?>
        <?php if ( ! empty( $jeweler_shop_product_button_label ) ) { ?>
          <div class="banner-slider-btn">
            <a href="<?php echo esc_url( $jeweler_shop_product_button_link ); ?>" class="asterthemes-button"><?php echo esc_html( $jeweler_shop_product_button_label ); ?><i class="fas fa-arrow-circle-right"></i></a>
          </div>
        <?php } ?>
      </div>
      <div class="product-section">
          <?php $jeweler_shop_catData = get_theme_mod('jeweler_shop_trending_product_category','');
          if ( class_exists( 'WooCommerce' ) ) {
            $jeweler_shop_args = array(
            'post_type' => 'product',
            'posts_per_page' => -1,
            'product_cat' => $jeweler_shop_catData,
            'order' => 'ASC'
            );?>
            <div class="product-box"> 
              <div class="owl-carousel">
              <?php $jeweler_shop_loop = new WP_Query( $jeweler_shop_args );
              while ($jeweler_shop_loop->have_posts()) : $jeweler_shop_loop->the_post(); 
                global $product;
                $jeweler_shop_product_categories = wp_get_post_terms($product->get_id(), 'product_cat');
                if (!empty($jeweler_shop_product_categories) && !is_wp_error($jeweler_shop_product_categories)) {
                    $jeweler_shop_product_category = $jeweler_shop_product_categories[0];
                }
                $jeweler_shop_regular_price = $product->get_regular_price();
                $jeweler_shop_sale_price = $product->get_sale_price();
                $jeweler_shop_percentage_discount = 0;

                if ($jeweler_shop_regular_price && $jeweler_shop_sale_price) {
                    $jeweler_shop_percentage_discount = -1 * round( ( ($jeweler_shop_regular_price - $jeweler_shop_sale_price) / $jeweler_shop_regular_price ) * 100 ); 
                }
              ?>
                <div class="tab-product">
                  <?php if($jeweler_shop_percentage_discount != 0) { ?>
                    <div class="discount-badge">
                      <?php echo esc_html( $jeweler_shop_percentage_discount ); ?>% <?php esc_html_e( 'Off', 'jeweler-shop' ); ?>
                    </div>
                  <?php } ?>
                  <figure>
                    <?php if (has_post_thumbnail( $jeweler_shop_loop->post->ID )) echo get_the_post_thumbnail($jeweler_shop_loop->post->ID, 'shop_catalog'); else echo '<img src="'.esc_url(wc_placeholder_img_src()).'" />'; ?>
                  </figure>
                  <div class="box-content intro-button">
                    <?php if( $product->is_type( 'simple' ) ) { woocommerce_template_loop_add_to_cart(  $jeweler_shop_loop->post, $product );} ?>
                  </div>
                  <div class="product-content-box">
                    <h5 class="product-text"><a href="<?php echo esc_url(get_permalink( $jeweler_shop_loop->post->ID )); ?>"><?php the_title(); ?></a></h5>
                    <div class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>">
                        <?php echo esc_html__( 'From ', 'jeweler-shop' ) . ' ' . wp_kses_post( $product->get_price_html() ); ?>
                    </div>

                  </div>
                </div>
              <?php endwhile; wp_reset_postdata(); ?>
            </div>
          </div>
        <?php } ?>
      </div>
		</div>
	</section>
	<?php
}