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

get_header();
?>

<main id="primary">

    <div class="slider-bg">
       <div class="container">
            <div class="slider-divide">
                <?php if (get_theme_mod('carpets_and_rugs_show_product_cat', 1)) : ?>
                    <div class="category-list wow fadeInUp" data-wow-duration="1.5s">
                        <?php if(class_exists('woocommerce')){ ?>
                            <div class="categry-title">
                                <strong><i class="fa fa-bars" aria-hidden="true"></i><?php echo esc_html_e('ALL CATEGORIES','carpets-and-rugs'); ?></strong>
                            </div>
                        <div class="product-cat" id="style-2">
                      <?php
                        $carpets_and_rugs_args = array(                  
                          'orderby'    => 'title',
                          'order'      => 'ASC',
                          'hide_empty' => 0,
                          'parent'  => 0
                        );
                        $product_categories = get_terms( 'product_cat', $carpets_and_rugs_args );
                        $carpets_and_rugs_count = count($product_categories);
                        if ( $carpets_and_rugs_count > 0 ){
                            foreach ( $product_categories as $product_category ) {
                              $carpets_and_rugs_kids_cat_id   = $product_category->term_id;
                              $cat_link = get_category_link( $carpets_and_rugs_kids_cat_id );
                              if ($product_category->category_parent == 0) { ?>
                            <li class="drp_dwn_menu"><a href="<?php echo esc_url(get_term_link( $product_category ) ); ?>">
                            <?php
                          }
                            echo esc_html( $product_category->name ); ?></a><i class="fas fa-caret-right"></i></li>
                            <?php
                            }
                          }
                      ?>
                        </div>
                        <?php }?> 
                    </div>
                <?php endif; ?>

                <?php 
                $carpets_and_rugs_main_slider_wrap = absint(get_theme_mod('carpets_and_rugs_enable_slider', 0));
                if($carpets_and_rugs_main_slider_wrap == 1){ 
                ?>
                <div class="slider-boxx">
                    <section id="main-slider-wrap">
                        <div class="owl-carousel">
                            <?php for ($carpets_and_rugs_i=1; $carpets_and_rugs_i <= 3; $carpets_and_rugs_i++) { ?>
                              <?php  if (
                                    get_theme_mod( 'carpets_and_rugs_slider_image'.$carpets_and_rugs_i ) != '' ||
                                    get_theme_mod( 'carpets_and_rugs_slider_xtra_heading'.$carpets_and_rugs_i ) != '' ||
                                    get_theme_mod( 'carpets_and_rugs_slider_heading'.$carpets_and_rugs_i ) != '' ||
                                    get_theme_mod( 'carpets_and_rugs_slider_text'.$carpets_and_rugs_i ) != '' ||
                                    get_theme_mod( 'carpets_and_rugs_slider_button1_link'.$carpets_and_rugs_i ) != '' ||
                                    get_theme_mod( 'carpets_and_rugs_slider_button1_text'.$carpets_and_rugs_i ) != ''
                                ) {  ?>
                                <div class="main-slider-inner-box wow fadeInDown position-relative" data-wow-duration="1.5s">
                                    <?php if ( get_theme_mod('carpets_and_rugs_slider_image'.$carpets_and_rugs_i) ) : ?>
                                        <img src="<?php echo esc_url( get_theme_mod('carpets_and_rugs_slider_image'.$carpets_and_rugs_i) ); ?>">
                                        <div class="main-slider-content-box">
                                            <?php if ( get_theme_mod('carpets_and_rugs_slider_xtra_heading'.$carpets_and_rugs_i) ) : ?><p class="xtra-head"><?php echo esc_html( get_theme_mod('carpets_and_rugs_slider_xtra_heading'.$carpets_and_rugs_i) ); ?></p><?php endif; ?>
                                            <?php if ( get_theme_mod('carpets_and_rugs_slider_heading'.$carpets_and_rugs_i) ) : ?><h1><?php echo esc_html( get_theme_mod('carpets_and_rugs_slider_heading'.$carpets_and_rugs_i) ); ?></h1><?php endif; ?>
                                            <hr>
                                            <?php if ( get_theme_mod('carpets_and_rugs_slider_text'.$carpets_and_rugs_i) ) : ?><p class="slider-content"><?php echo esc_html( get_theme_mod('carpets_and_rugs_slider_text'.$carpets_and_rugs_i) ); ?></p><?php endif; ?>
                                            <div class="main-slider-button">
                                                <?php if ( get_theme_mod('carpets_and_rugs_slider_button1_link'.$carpets_and_rugs_i) ||  get_theme_mod('carpets_and_rugs_slider_button1_text'.$carpets_and_rugs_i )) : ?><a class="btn-1" href="<?php echo esc_url( get_theme_mod('carpets_and_rugs_slider_button1_link'.$carpets_and_rugs_i) ); ?>"><?php echo esc_html( get_theme_mod('carpets_and_rugs_slider_button1_text'.$carpets_and_rugs_i) ); ?></a><?php endif; ?>
                                            </div>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            <?php }?>
                            <?php } ?>
                        </div>
                    </section>
                </div>
                <?php } ?>
            </div>
        </div>  
    </div> 
   <?php 
        $carpets_and_rugs_main_expert_wrap = absint(get_theme_mod('carpets_and_rugs_enable_product_section', 0));
        if ($carpets_and_rugs_main_expert_wrap == 1) { 
        ?>
        <section id="main-expert-wrap">
            <div class="container">
                <?php 
                // Check if WooCommerce is active
                if (class_exists('WooCommerce')) {
                    $args = array( 
                        'post_type' => 'product',
                        'product_cat' => esc_html(get_theme_mod('carpets_and_rugs_product_category','product_cat1')), // Escaped for security
                        'order' => 'ASC',
                        'posts_per_page' => -1,
                    );
                    $loop = new WP_Query($args);
                    
                    // Check if there are products
                    if ($loop->have_posts()) : 
                ?>
                    <div class="owl-carousel product-carousel">
                        <?php 
                        // Start the loop to display products
                        while ($loop->have_posts()) : $loop->the_post(); 
                            global $product;

                            // Calculate the sale percentage if applicable
                            $carpets_and_rugs_regular_price = (float) $product->get_regular_price();
                            $carpets_and_rugs_sale_price = (float) $product->get_sale_price();
                            $carpets_and_rugs_discount_percentage = ($carpets_and_rugs_regular_price > 0 && $carpets_and_rugs_sale_price > 0) 
                                ? round((($carpets_and_rugs_regular_price - $carpets_and_rugs_sale_price) / $carpets_and_rugs_regular_price) * 100) 
                                : 0;
                        ?>
                            <div class="product-box">  
                                <div class="product-box-content">
                                    <!-- Product Image with Content -->
                                    <div class="product-image-wrapper wow zoomIn" data-wow-duration="2s" style="position: relative;">
                                        <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', 'carpets-and-rugs') . '" />';
                                            }
                                            ?>
                                        </div>
                                        <div class="content-over-image">
                                            <!-- Sale Tag -->
                                            <?php if ($product->is_on_sale()) : ?>
                                                <div class="sale-tag">
                                                    <?php 
                                                    if ($product->is_on_sale()) {
                                                        echo esc_html__('Sale', 'carpets-and-rugs'); 
                                                    }
                                                    ?>
                                                </div>

                                                <div class="sale-percentage">
                                                    <?php echo esc_html__('UP TO ', 'carpets-and-rugs') . esc_html($carpets_and_rugs_discount_percentage) . esc_html__('% OFF', 'carpets-and-rugs'); ?>
                                                </div>
                                            <?php endif; ?>
                                            <!-- Product Title -->
                                            <h2 class="product-heading-text">
                                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                            </h2>

                                            <!-- 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>

                                            <div class="main-shop-now">
                                            <!-- Show Now Button -->
                                                <a href="<?php echo esc_url(wc_get_cart_url()); ?>" class="button show-now">
                                                    <?php esc_html_e('Show Now', 'carpets-and-rugs'); ?>
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div> 
                        <?php 
                        endwhile; 
                        wp_reset_postdata(); 
                        ?>
                    </div>
                <?php 
                    endif; 
                } 
                ?>
            </div>
        </section>
    <?php } ?>
</main>

<?php
get_footer();
?>