<?php
/**
 * Template Name: Home Page
 */

get_header();
?>

<main id="primary">

    <?php 
    $health_coach_services_main_slider_wrap = absint(get_theme_mod('health_coach_services_enable_slider', 0));
    if($health_coach_services_main_slider_wrap == 1): 
    ?>
        <section id="main-slider-wrap">
            <div class="owl-carousel">
                <?php for ($health_coach_services_main_i=1; $health_coach_services_main_i <= 3; $health_coach_services_main_i++): ?>
                    <?php if ($health_coach_services_slider_image = get_theme_mod('health_coach_services_slider_image'.$health_coach_services_main_i)): ?>
                        <div class="main-slider-inner-box">
                            <img src="<?php echo esc_url($health_coach_services_slider_image); ?>" alt="<?php echo esc_attr( get_theme_mod('health_coach_services_slider_heading'.$health_coach_services_main_i) ); ?>">
                            <div class="main-slider-content-box">
                                <?php if ($health_coach_services_top_text = get_theme_mod('health_coach_services_slider_top_text'.$health_coach_services_main_i)): ?>
                                    <p class="slider-top"><?php echo esc_html($health_coach_services_top_text); ?></p>
                                <?php endif; ?>
                                <?php if ($health_coach_services_heading = get_theme_mod('health_coach_services_slider_heading' . $health_coach_services_main_i)): ?>
                                    <h1><?php echo esc_html($health_coach_services_heading); ?></h1>
                                <?php endif; ?>

                                <?php if ($health_coach_services_text = get_theme_mod('health_coach_services_slider_text'.$health_coach_services_main_i)): ?>
                                    <p><?php echo esc_html($health_coach_services_text); ?></p>
                                <?php endif; ?>
                                <div class="main-slider-button">
                                    <?php if ( get_theme_mod('health_coach_services_slider_button1_link'.$health_coach_services_main_i) ||  get_theme_mod('health_coach_services_slider_button1_text'.$health_coach_services_main_i )) : ?><a class="slide-btn-1" href="<?php echo esc_url( get_theme_mod('health_coach_services_slider_button1_link'.$health_coach_services_main_i) ); ?>"><?php echo esc_html( get_theme_mod('health_coach_services_slider_button1_text'.$health_coach_services_main_i) ); ?></a><?php endif; ?>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>
                <?php endfor; ?>
            </div>
        </section>
    <?php endif; ?>

   <?php 
    $health_coach_services_main_expert_wrap = absint(get_theme_mod('health_coach_services_enable_product_section', 0));
    if ($health_coach_services_main_expert_wrap === 1) { 
    ?>
        <section id="main-expert-wrap">
            <div class="container">
                <div class="feature-left">
                    <?php 
                    // Display the product section heading if set in the Customizer
                    $health_coach_services_product_section_heading = get_theme_mod('health_coach_services_product_section_heading');
                    if (!empty($health_coach_services_product_section_heading)) : ?>
                        <h3><?php echo esc_html($health_coach_services_product_section_heading); ?></h3>
                        <hr>
                    <?php endif; ?>
                </div>

                <div class="flex-row">
                    <?php 
                    // Check if WooCommerce is active
                    if (class_exists('WooCommerce')) {
                        $args = array( 
                            'post_type' => 'product',
                            'product_cat' => esc_html(get_theme_mod('health_coach_services_product_category')), // Escaped for security
                            'order' => 'ASC',
                            'posts_per_page' => 10
                        );
                        $health_coach_services_loop = new WP_Query($args);
                        
                        // Start the loop to display products
                        while ($health_coach_services_loop->have_posts()) : $health_coach_services_loop->the_post(); 
                            global $product;
                            ?>
                            <div class="product-box">  
                                <div class="product-box-content">
                                    <!-- Product Image -->
                                    <div class="product-image">
                                        <?php 
                                        if (has_post_thumbnail()) {
                                            the_post_thumbnail('shop_catalog');
                                        } else {
                                            echo '<img src="' . esc_url(wc_placeholder_img_src()) . '" alt="' . esc_attr__('Placeholder', 'health-coach-services') . '" />';
                                        }
                                        ?>
                                    </div>

                                    <!-- Sale Tag -->
                                    <div class="sale-tag">
                                        <?php woocommerce_show_product_sale_flash(); // Updated to use global WooCommerce function ?>
                                    </div>

                                    <!-- Product Title -->
                                    <h6 class="product-heading-text">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </h6>

                                    <!-- Product Rating -->
                                    <div class="product-rating">
                                        <?php 
                                        if ($product->is_type('simple')) {
                                            woocommerce_template_loop_rating(); 
                                        }
                                        ?>
                                    </div>

                                    <!-- Product Price -->
                                    <p class="product-price d-flex <?php echo esc_attr(apply_filters('woocommerce_product_price_class', 'price')); ?>">
                                        <?php echo wp_kses_post($product->get_price_html()); ?>
                                    </p>

                                    <!-- Add to Cart Button -->
                                    <div class="cart-button align-items-center justify-content-center">
                                        <?php 
                                        if ($product->is_type('simple')) {
                                            woocommerce_template_loop_add_to_cart(); 
                                        }
                                        ?>  
                                    </div>
                                </div>
                            </div> 
                        <?php 
                        endwhile; 
                        wp_reset_postdata(); 
                    } 
                    ?>
                </div>
            </div>
        </section>
    <?php } ?>
  

</main>

<?php
get_footer();
?>