<?php 
/**
 * Template part for displaying Featured Classes Section
 *
 * @package OTT TV Shows
 */

$ott_tv_shows_about = get_theme_mod('ott_tv_shows_about_setting', false);
$ott_tv_shows_number_of_featured_mission_items = get_theme_mod('ott_tv_shows_number_of_featured_mission_items', 0);

if ($ott_tv_shows_about) { ?>
    <div id="about-section" class="section-content py-5">
        <?php
        $ott_tv_shows_featured_mission_section_sub_title = get_theme_mod('ott_tv_shows_featured_mission_section_sub_title');
        $ott_tv_shows_featured_mission_section_title = get_theme_mod('ott_tv_shows_featured_mission_section_title'); ?>
        <div class="section-title mb-5 text-center">
            <?php if ($ott_tv_shows_featured_mission_section_sub_title) { ?>
                <p><?php echo esc_html($ott_tv_shows_featured_mission_section_sub_title); ?></p>
            <?php } ?>
            <?php if ($ott_tv_shows_featured_mission_section_title) { ?>
                <h3><?php echo esc_html($ott_tv_shows_featured_mission_section_title); ?></h3>
            <?php } ?>
        </div>
       
        <?php if(class_exists('woocommerce')){ ?>
            <div class="tabs">
                <ul class="tabs-nav">
                    <?php for ($i = 1; $i <= $ott_tv_shows_number_of_featured_mission_items; $i++) : 
                        $tab_title = get_theme_mod('ott_tv_shows_featured_mission_section_tab_' . $i);
                        if ($tab_title) { ?>
                            <li class="tabs-nav-box">
                                <a href="#tab-<?php echo esc_attr($i); ?>"><?php echo esc_html($tab_title); ?></a>
                            </li>
                        <?php } 
                    endfor; ?>
                </ul>
                <div class="tabs-stage">
                    <?php for ($i = 1; $i <= $ott_tv_shows_number_of_featured_mission_items; $i++) : 
                        $category_name = get_theme_mod('ott_tv_shows_trending_post_slider_args_' . $i);
                        if ($category_name) {
                            $args = array(
                                'post_type'           => 'product',
                                'ignore_sticky_posts' => true,
                                'posts_per_page'      => 4,
                                'tax_query'           => array(
                                    array(
                                        'taxonomy' => 'product_cat',
                                        'field'    => 'slug',
                                        'terms'    => $category_name,
                                    ),
                                ),
                            );
                            $loop = new WP_Query($args); ?>
                            <div id="tab-<?php echo esc_attr($i); ?>" class="featured-mission-box">
                                <div class="container">
                                    <div class="row m-0">
                                        <?php if ($loop->have_posts()) : 
                                            while ($loop->have_posts()) : $loop->the_post(); 
                                                $product = wc_get_product(get_the_ID()); ?>
                                                <div class="col-lg-3 col-md-6 align-self-center my-2">
                                                    <div class="box">
                                                        <?php if (has_post_thumbnail()) : 
                                                            the_post_thumbnail();
                                                        else : ?>
                                                            <div class="image-container">
                                                                <img src="<?php echo esc_url(get_stylesheet_directory_uri() . '/images/default-header.png'); ?>" alt="Default">
                                                            </div>
                                                        <?php endif; ?>
                                                        <div class="box-content">
                                                            <div class="cat-price">
                                                               <h3 class="pt-2 mb-0">
                                                                   <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                                               </h3>
                                                                <div class="category">
                                                                    <?php 
                                                                    $selected_category_slug = get_theme_mod('ott_tv_shows_trending_post_slider_args_' . $i); 
                                                                    $product_categories = wp_get_post_terms($product->get_id(), 'product_cat'); 
                                                                    
                                                                    if (!empty($product_categories)) {
                                                                        foreach ($product_categories as $category) {
                                                                            if ($category->slug === $selected_category_slug) {
                                                                                echo '<p class="product-category">' . esc_html($category->name) . '</p>';
                                                                                break;
                                                                            }
                                                                        }
                                                                    } else {
                                                                        echo '<p class="product-category">' . esc_html__('No category found', 'ott-tv-shows') . '</p>';
                                                                    }
                                                                    ?>
                                                                </div>
                                                            </div>
                                                            <div class="price">
                                                                <?php echo $product ? $product->get_price_html() : ''; ?>
                                                            </div>                                                            
                                                        </div>
                                                    </div>
                                                </div>
                                            <?php endwhile; 
                                        else : ?>
                                            <p class="no-products"><?php esc_html_e('No products found.', 'ott-tv-shows'); ?></p>
                                        <?php endif; ?>
                                        <?php wp_reset_postdata(); ?>
                                    </div>
                                </div>
                            </div>
                        <?php }
                    endfor; ?>
                </div>
            </div>
        <?php }?>
    </div>
<?php } ?>
