<?php 
$pet_house_product = get_theme_mod('pet_house_our_products_show_hide_section', false);

if ($pet_house_product == true) : ?>
<section id="product-section" class="py-5 mx-md-0 mx-3">
  <div class="container">
    <?php
    // Section Heading
    $pet_house_our_products_heading_section = get_theme_mod('pet_house_our_products_heading_section');
    if (!empty($pet_house_our_products_heading_section)) : ?>
      <h2 class="mb-2 text-center product-heading">
        <?php echo esc_html($pet_house_our_products_heading_section); ?>
      </h2>
    <?php endif; ?>

    <?php
    // Short Heading for the section
    $pet_house_short_heading = get_theme_mod('pet_house_our_products_short_heading_section');
    if (!empty($pet_house_short_heading)) : ?>
      <p class="mb-3 text-center short-product-heading">
        <?php echo esc_html($pet_house_short_heading); ?>
      </p>
    <?php endif; ?>
    
    <?php if (class_exists('woocommerce')) : ?>
      <div class="row">
        <?php
        // Query Products by Selected Category
        $pet_house_selected_category = get_theme_mod('pet_house_our_product_product_category','product_cat1');
        if ($pet_house_selected_category) {
            $pet_house_args = array(
                'post_type'      => 'product',
                'posts_per_page' => 50,
                'product_cat'    => $pet_house_selected_category,
                'order'          => 'ASC'
            );
            $pet_house_loop = new WP_Query($pet_house_args);
            if ($pet_house_loop->have_posts()) : 
                while ($pet_house_loop->have_posts()) : $pet_house_loop->the_post();
                global $product;
        ?>
          <div class="col-lg-3 col-md-4 mb-5 wow bounceIn" data-wow-delay="0.5s">
            <div class="product-box">
              <div class="product-image position-relative">
                <?php echo woocommerce_get_product_thumbnail(); ?>
                
                <!-- Show Sale Badge for Products on Sale -->
                <?php if ($product->is_on_sale()) : ?>
                  <span class="sale-badge position-absolute top-0 start-0 bg-danger text-white px-2 py-1"><?php esc_html_e('Sale', 'pet-house'); ?></span>
                <?php endif; ?>
              </div>
              <div class="product-content text-center">
                <div class="product-cat">
                  <?php 
                  // Display Product Categories
                  $pet_house_terms = get_the_terms(get_the_ID(), 'product_cat');
                  if ($pet_house_terms && !is_wp_error($pet_house_terms)) {
                    $pet_house_term_links = array();
                    foreach ($pet_house_terms as $term) {
                      $pet_house_term_links[] = '<a class="mt-2" href="' . esc_url(get_term_link($term)) . '">' . esc_html($term->name) . '</a>';
                    }
                    echo implode(', ', $pet_house_term_links);
                  }
                  ?>
                </div>
                <h3 class="my-1"><a href="<?php the_permalink(); ?>"><?php echo esc_html(get_the_title()); ?></a></h3>
                
                <p class="mb-2 product-price" style="color: <?php echo $product->is_on_sale() ? 'black' : 'gray'; ?>">
                  <?php echo $product->get_price_html(); ?>
                </p>

                <div class="product-rating">
                  <?php
                  if ($product->get_rating_count()) {
                      woocommerce_template_loop_rating();
                  }
                  ?>
                </div>
              </div>
            </div>
          </div>
        <?php
                endwhile;
                wp_reset_postdata();
            endif;
        }
        ?>
      </div>
    <?php endif; ?>
  </div>
</section>
<?php endif; ?>
