<?php 
// Check if the section should be displayed
$expert_fitness_trainer_show_product_section = get_theme_mod('expert_fitness_trainer_our_products_show_hide_section', '1');

if ($expert_fitness_trainer_show_product_section === '1') :
?>
<section id="product-section" class="py-5 px-md-0 px-3">
  <div class="container">
    <?php 
    // Get the short heading for the section
    $expert_fitness_trainer_short_heading = get_theme_mod('expert_fitness_trainer_our_products_short_heading_section');
    if (!empty($expert_fitness_trainer_short_heading)) : ?>
      <p class="mb-2 text-center text-uppercase short-product-heading">
        <?php echo esc_html($expert_fitness_trainer_short_heading); ?>
      </p>
    <?php endif; ?>

    <?php 
    // Get the main heading for the section
    $expert_fitness_trainer_main_heading = get_theme_mod('expert_fitness_trainer_our_products_heading_section');
    if (!empty($expert_fitness_trainer_main_heading)) : ?>
      <h2 class="mb-4 text-center text-uppercase product-heading">
        <?php echo esc_html($expert_fitness_trainer_main_heading); ?>
      </h2>
    <?php endif; ?>

    <?php 
    // Check if WooCommerce is active
    if (class_exists('WooCommerce')) : ?>
      <div class="row">
        <?php 
        // Get the selected product category
        $expert_fitness_trainer_selected_category = get_theme_mod('expert_fitness_trainer_our_product_product_category','product_cat1');
        if (!empty($expert_fitness_trainer_selected_category)) :
          // Set up the query arguments
          $query_args = array(
            'post_type'      => 'product',
            'posts_per_page' => 50,
            'product_cat'    => $expert_fitness_trainer_selected_category,
            'order'          => 'ASC'
          );

          // Create a new WP_Query instance
          $product_query = new WP_Query($query_args);
          
          // Start the loop
          while ($product_query->have_posts()) : $product_query->the_post();
            global $product;
        ?>
          <div class="col-lg-3 col-md-4 mb-3 wow fadeInUp" data-wow-delay="0.5s">
            <div class="product-box">
              <div class="product-image">
                <?php echo woocommerce_get_product_thumbnail(); ?>
                <div class="cart-button text-center">
                  <?php 
                  // Display the add to cart button for simple products
                  if ($product->is_type('simple')) {
                    woocommerce_template_loop_add_to_cart();
                  }
                  ?>
                </div>
              </div>
              <div class="product-content text-start">
                <div class="product-cat">
                  <?php 
                  // Display product categories
                  $terms = get_the_terms(get_the_ID(), 'product_cat');
                  if ($terms && !is_wp_error($terms)) {
                    $term_links = array();
                    foreach ($terms as $term) {
                      $term_links[] = '<a class="mt-2" href="' . esc_url(get_term_link($term)) . '">' . esc_html($term->name) . '</a>';
                    }
                    echo implode(', ', $term_links);
                  }
                  ?>
                </div>
                <h3 class="mb-1"><a href="<?php the_permalink(); ?>"><?php echo esc_html(get_the_title()); ?></a></h3>
                <p class="mb-2 product-price"><?php echo wp_kses_post($product->get_price_html()); ?></p>
              </div>
            </div>
          </div>
        <?php 
          endwhile;
          wp_reset_postdata();
        endif;
        ?>
      </div>
    <?php endif; ?>

    <div class="read-btn mt-2 text-center">
      <?php 
      // Get the button text and link
      $expert_fitness_trainer_button_text = get_theme_mod('expert_fitness_trainer_product_btn_text', '');
      $expert_fitness_trainer_button_link = get_theme_mod('expert_fitness_trainer_product_btn_link', '');

      if (!empty($expert_fitness_trainer_button_text) || !empty($expert_fitness_trainer_button_link)) : ?>
        <a target="_blank" class="text-capitalize mb-3 slider-btn1" href="<?php echo esc_url($expert_fitness_trainer_button_link); ?>">
          <?php echo esc_html(!empty($expert_fitness_trainer_button_text) ? $expert_fitness_trainer_button_text : __('', 'expert-fitness-trainer')); ?>
          <i class="fas fa-arrow-right ms-1"></i>
        </a>
      <?php endif; ?>
    </div>
  </div>
</section>
<?php 
endif; 
?>