<?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('lights_store_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','lights-store'); ?></strong>
                            </div>
                        <div class="product-cat" id="style-2">
                      <?php
                        $lights_store_args = array(                  
                          'orderby'    => 'title',
                          'order'      => 'ASC',
                          'hide_empty' => 0,
                          'parent'  => 0
                        );
                        $product_categories = get_terms( 'product_cat', $lights_store_args );
                        $lights_store_count = count($product_categories);
                        if ( $lights_store_count > 0 ){
                            foreach ( $product_categories as $product_category ) {
                              $lights_store_kids_cat_id   = $product_category->term_id;
                              $cat_link = get_category_link( $lights_store_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 
                $lights_store_main_slider_wrap = absint(get_theme_mod('lights_store_enable_slider', 0));
                if($lights_store_main_slider_wrap == 1){ 
                ?>
                <div class="slider-boxx">
                    <section id="main-slider-wrap">
                        <div class="owl-carousel">
                            <?php for ($lights_store_i=1; $lights_store_i <= 3; $lights_store_i++) { ?>
                              <?php  if (
                                    get_theme_mod( 'lights_store_slider_image'.$lights_store_i ) != '' ||
                                    get_theme_mod( 'lights_store_slider_xtra_heading'.$lights_store_i ) != '' ||
                                    get_theme_mod( 'lights_store_slider_heading'.$lights_store_i ) != '' ||
                                    get_theme_mod( 'lights_store_slider_text'.$lights_store_i ) != '' ||
                                    get_theme_mod( 'lights_store_slider_button1_link'.$lights_store_i ) != '' ||
                                    get_theme_mod( 'lights_store_slider_button1_text'.$lights_store_i ) != ''
                                ) {  ?>
                                <div class="main-slider-inner-box wow fadeInDown" data-wow-duration="1.5s">
                                    <?php if ( get_theme_mod('lights_store_slider_image'.$lights_store_i) ) : ?>
                                        <img src="<?php echo esc_url( get_theme_mod('lights_store_slider_image'.$lights_store_i) ); ?>">
                                        <div class="main-slider-content-box">
                                            <?php if ( get_theme_mod('lights_store_slider_xtra_heading'.$lights_store_i) ) : ?><p class="xtra-head"><?php echo esc_html( get_theme_mod('lights_store_slider_xtra_heading'.$lights_store_i) ); ?></p><?php endif; ?>
                                            <?php if ( get_theme_mod('lights_store_slider_heading'.$lights_store_i) ) : ?><h1><?php echo esc_html( get_theme_mod('lights_store_slider_heading'.$lights_store_i) ); ?></h1><?php endif; ?>
                                            <hr>
                                            <?php if ( get_theme_mod('lights_store_slider_text'.$lights_store_i) ) : ?><p class="slider-content"><?php echo esc_html( get_theme_mod('lights_store_slider_text'.$lights_store_i) ); ?></p><?php endif; ?>
                                            <div class="main-slider-button">
                                                <?php if ( get_theme_mod('lights_store_slider_button1_link'.$lights_store_i) ||  get_theme_mod('lights_store_slider_button1_text'.$lights_store_i )) : ?><a class="btn-1" href="<?php echo esc_url( get_theme_mod('lights_store_slider_button1_link'.$lights_store_i) ); ?>"><?php echo esc_html( get_theme_mod('lights_store_slider_button1_text'.$lights_store_i) ); ?></a><?php endif; ?>
                                            </div>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            <?php }?>
                            <?php } ?>
                        </div>
                    </section>
                </div>
                <?php } ?>
            </div>
        </div>  
    </div> 
   <?php 
        $lights_store_main_expert_wrap = absint(get_theme_mod('lights_store_enable_product_section', 0));
        if ($lights_store_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('lights_store_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
                            $lights_store_regular_price = (float) $product->get_regular_price();
                            $lights_store_sale_price = (float) $product->get_sale_price();
                            $lights_store_discount_percentage = ($lights_store_regular_price > 0 && $lights_store_sale_price > 0) 
                                ? round((($lights_store_regular_price - $lights_store_sale_price) / $lights_store_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', 'lights-store') . '" />';
                                            }
                                            ?>
                                        </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', 'lights-store'); 
                                                    }
                                                    ?>
                                                </div>

                                                <div class="sale-percentage">
                                                    <?php echo esc_html__('UP TO ', 'lights-store') . esc_html($lights_store_discount_percentage) . esc_html__('% OFF', 'lights-store'); ?>
                                                </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', 'lights-store'); ?>
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div> 
                        <?php 
                        endwhile; 
                        wp_reset_postdata(); 
                        ?>
                    </div>
                <?php 
                    endif; 
                } 
                ?>
            </div>
        </section>
    <?php } ?>
</main>

<?php
get_footer();
?>