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

get_header();
?>

<main id="primary">

    <?php if (get_theme_mod('grocery_supermarket_chain_banner_section_on_off_setting', false)) { ?>
        <section id="main-banner-wrap">
            <div id="banner-area">
                <div class="owl-carousel owl-theme">
                    <?php
                        $grocery_supermarket_chain_banner = get_theme_mod( 'grocery_supermarket_chain_slider');
                        for ( $i = 1; $i <= $grocery_supermarket_chain_banner; $i++ ){ ?>
                        <div class="item">
                            <div class="main-banner-inner-box">
                                <?php
                                $grocery_supermarket_chain_banner_image = get_theme_mod( 'grocery_supermarket_chain_banner_image'.$i, '' );
                                if ( ! empty( $grocery_supermarket_chain_banner_image ) ) { ?>
                                    <img src="<?php echo esc_url( $grocery_supermarket_chain_banner_image ); ?>">
                                <?php } ?>
                            </div>
                            <div class="main-banner-content-box">
                                <?php
                                    $grocery_supermarket_chain_banner_short_heading = get_theme_mod( 'grocery_supermarket_chain_banner_short_heading'.$i, '' );
                                    if ( ! empty( $grocery_supermarket_chain_banner_short_heading ) ) { ?>
                                    <h6><?php echo esc_html( $grocery_supermarket_chain_banner_short_heading ); ?></h6>
                                <?php } ?>
                                <?php
                                    $grocery_supermarket_chain_banner_heading = get_theme_mod( 'grocery_supermarket_chain_banner_heading'.$i, '' );
                                    if ( ! empty( $grocery_supermarket_chain_banner_heading ) ) { ?>
                                    <h3><?php echo esc_html( $grocery_supermarket_chain_banner_heading ); ?></h3>
                                <?php } ?>
                                <?php
                                    $grocery_supermarket_chain_banner_btn = get_theme_mod( 'grocery_supermarket_chain_banner_btn'.$i, '' );
                                    $grocery_supermarket_chain_banner_button_link = get_theme_mod( 'grocery_supermarket_chain_banner_button_link'.$i, '' );
                                    if ( ! empty( $grocery_supermarket_chain_banner_btn ) ) { ?>
                                    <a href="<?php echo esc_url( $grocery_supermarket_chain_banner_button_link ); ?>"><?php echo esc_html( $grocery_supermarket_chain_banner_btn ); ?></a>
                                <?php } ?>
                            </div>
                        </div>
                    <?php }?>
                </div>
            </div>
        </section>
    <?php } ?>

    <?php if (get_theme_mod('grocery_supermarket_chain_product_section_on_off_setting', false)) { ?>
        <section class="product py-5">
            <div class="container">
                <?php
                    $grocery_supermarket_chain_product_heading = get_theme_mod( 'grocery_supermarket_chain_product_heading', '' );
                    if ( ! empty( $grocery_supermarket_chain_product_heading ) ) { ?>
                    <div class="main-heading mb-4">
                        <h3 class="mb-0"><?php echo esc_html( $grocery_supermarket_chain_product_heading ); ?></h3>
                        <hr>
                        <i class="fas fa-leaf"></i>
                    </div>
                <?php } ?>
                <div class="tabs-stage">
                    <?php 
                    $category_name = get_theme_mod('grocery_supermarket_chain_trending_post_slider_args','product_cat1');

                    if (!empty($category_name)) {
                        // Set query arguments
                        $args = array(
                            'post_type'           => 'product',
                            'ignore_sticky_posts' => true,
                            'posts_per_page'      => 15,
                            'tax_query'           => array(
                                array(
                                    'taxonomy' => 'product_cat',
                                    'field'    => 'slug',
                                    'terms'    => $category_name,
                                    'operator' => 'IN',
                                ),
                            ),
                        );

                        $loop = new WP_Query($args); ?>

                        <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-4 col-md-6 align-self-center">
                                        <div class="box mb-4">
                                            <div class="row">
                                                <div class="col-lg-5 col-md-6 align-self-center">
                                                    <?php if (has_post_thumbnail()) : ?>
                                                        <?php the_post_thumbnail(); ?>
                                                    <?php endif; ?>
                                                </div>
                                                <div class="col-lg-7 col-md-6 align-self-center">
                                                    <div class="box-content">
                                                        <h3 class="mb-0">
                                                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                                        </h3>
                                                        <div class="cat-price">
                                                            <div class="category">
                                                                <?php 
                                                                $product_categories = wp_get_post_terms($product->get_id(), 'product_cat');

                                                                if (!empty($product_categories)) {
                                                                    foreach ($product_categories as $category) {
                                                                        if ($category->slug === $category_name) {
                                                                            echo '<p class="product-category mb-0">' . esc_html($category->name) . '</p>';
                                                                            break;
                                                                        }
                                                                    }
                                                                }
                                                                ?>
                                                            </div>
                                                            <div class="price">
                                                                <?php echo $product ? $product->get_price_html() : ''; ?>
                                                            </div>
                                                        </div>
                                                        <!-- Add to Cart Button -->
                                                        <div class="add-to-cart mt-3">
                                                            <?php if ($product->is_in_stock()) : ?>
                                                                <?php woocommerce_template_loop_add_to_cart(); ?>
                                                            <?php else : ?>
                                                                <button class="button out-of-stock" disabled><?php esc_html_e('Out of Stock', 'grocery-supermarket-chain'); ?></button>
                                                            <?php endif; ?>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                <?php endwhile; 
                            else : ?>
                                <p class="no-products"><?php esc_html_e('No products found.', 'grocery-supermarket-chain'); ?></p>
                            <?php endif; ?>

                            <?php wp_reset_postdata(); // Reset post data to avoid conflicts ?>
                        </div>
                    <?php }
                    ?>
                </div>
            </div>
        </section>
    <?php } ?>

</main>

<?php
get_footer();