<?php
/**
 * Template part for displaying the services section
 *
 * @package Multipurpose Sports Club
 * @subpackage multipurpose_sports_club
 */

// Get setting to determine whether to display the section
$multipurpose_sports_club_aboutus = get_theme_mod('multipurpose_sports_club_our_products_show_hide_section', true);

if ($multipurpose_sports_club_aboutus == '1') : ?>
<section id="product-section" class="my-5 mx-md-0 mx-3">
  <div class="container">
    <div class="product-main-head">
      <div class="row">
        <div class="col-lg-8 col-md-8 text-md-start align-self-center">
          <?php
          // Heading for the section
          $multipurpose_sports_club_our_products_small_title_section = get_theme_mod('multipurpose_sports_club_our_products_small_title_section');
          if (!empty($multipurpose_sports_club_our_products_small_title_section)) : ?>
            <p class="product-small-heading text-uppercase mb-2">
              <?php echo esc_html(apply_filters('multipurpose_sports_club_topheader', $multipurpose_sports_club_our_products_small_title_section)); ?>
            </p>
          <?php endif; ?>
          <?php
        $multipurpose_sports_club_our_products_heading_section = get_theme_mod('multipurpose_sports_club_our_products_heading_section');
        if (!empty($multipurpose_sports_club_our_products_heading_section)) :

          // Get filtered heading text
          $multipurpose_sports_club_heading_text = apply_filters('multipurpose_sports_club_topheader', $multipurpose_sports_club_our_products_heading_section);

          // Split heading into words
          $multipurpose_sports_club_words = explode(' ', $multipurpose_sports_club_heading_text);

          if (count($multipurpose_sports_club_words) > 1) {
            // Extract last word and the rest
            $multipurpose_sports_club_last_word = array_pop($multipurpose_sports_club_words);
            $multipurpose_sports_club_first_part = implode(' ', $multipurpose_sports_club_words);

            // Output heading with last word wrapped in span for styling
            ?>
            <h2 class="product-heading text-uppercase mb-lg-4 mb-3">
              <?php echo esc_html($multipurpose_sports_club_first_part) . ' <span class="highlight-last-word">' . esc_html($multipurpose_sports_club_last_word) . '</span>'; ?>
            </h2>
          <?php
          } else {
            // If single word, just highlight whole heading
            ?>
            <h2 class="product-heading text-uppercase mb-3">
              <span class="highlight-last-word"><?php echo esc_html($multipurpose_sports_club_heading_text); ?></span>
            </h2>
          <?php
          }
        endif;
        ?>
        </div>
        <div class="col-lg-4 col-md-4 align-self-center">
          <div class="main-produ-btn">
            <?php
            // "View All" button
            $multipurpose_sports_club_product_btn_text = get_theme_mod('multipurpose_sports_club_product_section_btn_text1', __('View All', 'multipurpose-sports-club'));
            $multipurpose_sports_club_product_btn_link = get_theme_mod('multipurpose_sports_club_product_section_btn_link1');

            if (!empty($multipurpose_sports_club_product_btn_text) && !empty($multipurpose_sports_club_product_btn_link)) : ?>
              <a class="pe-2 prod-btn2 text-uppercase" href="<?php echo esc_url($multipurpose_sports_club_product_btn_link); ?>">
                <?php echo esc_html($multipurpose_sports_club_product_btn_text); ?>
                <span class="screen-reader-text"><?php echo esc_html($multipurpose_sports_club_product_btn_text); ?></span>
                <i class="fas fa-arrow-right ms-2 me-3" aria-hidden="true"></i>
              </a>
            <?php endif; ?>
          </div>
        </div>
      </div>
    </div>

    <?php if (class_exists('WooCommerce')) : ?>
  <div class="owl-carousel">
    <?php
    // Fetch and display products from selected category
    $multipurpose_sports_club_selected_category = get_theme_mod('multipurpose_sports_club_our_product_product_category');

    if (!empty($multipurpose_sports_club_selected_category)) {
      $multipurpose_sports_club_args = array(
        'post_type'      => 'product',
        'posts_per_page' => 50,
        'product_cat'    => $multipurpose_sports_club_selected_category,
        'order'          => 'ASC',
      );

      $multipurpose_sports_club_loop = new WP_Query($multipurpose_sports_club_args);

      while ($multipurpose_sports_club_loop->have_posts()) : $multipurpose_sports_club_loop->the_post();
        global $product;
        ?>
        <div class="product-box p-3 position-relative">
          <div class="product-image position-relative">
           <?php
            if ($product->is_on_sale()) {
                echo '<span class="onsale">' . __('SALE!', 'multipurpose-sports-club') . '</span>';
            }
            ?>
            <?php echo woocommerce_get_product_thumbnail(); ?>
            <div class="product-actions-overlay">
              <?php if (class_exists('YITH_WCWL')): ?>
                <a href="<?php echo esc_url(add_query_arg('add_to_wishlist', get_the_ID())); ?>" class="add_to_wishlist wishlist-icon" 
                   data-product-id="<?php echo get_the_ID(); ?>" 
                   title="Add to Wishlist">
                   <i class="fa fa-heart"></i>
                </a>
              <?php endif; ?>
              <a href="<?php echo esc_url( $product->add_to_cart_url() ); ?>"
                 data-quantity="1"
                 class="add-to-cart-icon add_to_cart_button ajax_add_to_cart"
                 data-product_id="<?php echo esc_attr( $product->get_id() ); ?>"
                 aria-label="<?php echo esc_attr( $product->add_to_cart_text() ); ?>"
                 rel="nofollow" title="Add to Cart">
                <i class="fa fa-shopping-cart"></i>
              </a>
              <a href="<?php the_permalink(); ?>" class="search-icon" title="View Product">
                <i class="fa fa-search"></i>
              </a>
            </div>
          </div>
          <div class="product-content text-center mt-3">
            <h3 class="my-3">
              <a class="text-uppercase" href="<?php the_permalink(); ?>"><?php echo esc_html(get_the_title()); ?></a>
            </h3>
           <p class="mb-2 product-price">
              <span class="price-label"><?php echo esc_html__('PRICE:', 'multipurpose-sports-club'); ?></span> <span class="price-reverse"><?php echo ' ' . $product->get_price_html(); ?></span>
            </p>
          </div>
        </div>
      <?php
      endwhile;
      wp_reset_postdata();
    }
    ?>
  </div>
<?php endif; ?>

  </div>
</section>
<?php endif; ?>