<?php
/**
 * The Template Name: Home Page
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package Classic Fashion Blog
 */

get_header(); ?>

<div id="content" >
    
    <?php
    $classic_fashion_blog_banner = get_theme_mod('classic_fashion_blog_banner', true);
    $classic_fashion_blog_catData = get_theme_mod('classic_fashion_blog_trending_cat');
    $classic_fashion_blog_banner_pageboxes = get_theme_mod('classic_fashion_blog_banner_pageboxes', false);

    if ($classic_fashion_blog_banner && $classic_fashion_blog_banner_pageboxes) { ?>
        <div id="banner-cat" class="pb-md-5">
            <?php if (get_theme_mod('classic_fashion_blog_banner_side_content') != "") { ?>
                <div class="banner-side-main">
                    <p class="banner-side mb-0 text-capitalize"><?php echo esc_html(get_theme_mod('classic_fashion_blog_banner_side_content', 'classic-fashion-blog')); ?></p>
                </div>
            <?php } ?>
            <?php if (get_theme_mod('classic_fashion_blog_banner_alphabet') != "") { ?>
                <p class="banner-alpha mb-0 text-capitalize p-3"><?php echo esc_html(get_theme_mod('classic_fashion_blog_banner_alphabet', 'classic-fashion-blog')); ?></p>
            <?php } ?>
            <div class="container">
                <?php
                $classic_fashion_blog_querymed = new WP_Query(array(
                    'page_id' => esc_attr($classic_fashion_blog_banner_pageboxes)
                ));
                while ($classic_fashion_blog_querymed->have_posts()) : $classic_fashion_blog_querymed->the_post(); ?>
                    <div class="row">
                        <div class="col-lg-6 col-md-6 col-12 align-self-center mb-md-0 mb-2 banner-content">
                            <div class="bannerbox">
                                <?php if (get_theme_mod('classic_fashion_blog_banner_small_title') != '') { ?>
                                    <p class="banner-smalltitle text-capitalize"><?php echo esc_html(get_theme_mod('classic_fashion_blog_banner_small_title', 'classic-fashion-blog')); ?></p>
                                <?php } ?>
                                <h1 class="my-2"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                                <?php
                                $classic_fashion_blog_trimexcerpt = get_the_excerpt();
                                $classic_fashion_blog_shortexcerpt = wp_trim_words($classic_fashion_blog_trimexcerpt, 38);
                                echo '<p class="banner-content">' . esc_html($classic_fashion_blog_shortexcerpt) . '</p>';
                                ?>
                                <div class="bannerbtn mb-5">
                                    <?php 
                                        $classic_fashion_blog_button_text = get_theme_mod('classic_fashion_blog_button_text', 'Explore more');
                                        $classic_fashion_blog_icon = get_theme_mod('classic_fashion_blog_button_icon', 'fa-solid fa-chevron-right');
                                        $classic_fashion_blog_button_link_slider = get_theme_mod('classic_fashion_blog_button_link_slider', ''); 
                                        if (empty($classic_fashion_blog_button_link_slider)) {
                                            $classic_fashion_blog_button_link_slider = esc_url(get_permalink());
                                        }
                                        if ($classic_fashion_blog_button_text || !empty($classic_fashion_blog_button_link_slider)) { ?>
                                            <?php if(get_theme_mod('classic_fashion_blog_button_text', 'Explore more') != ''){ ?>
                                                <a href="<?php echo esc_url($classic_fashion_blog_button_link_slider); ?>" class="text-capitalize">
                                                    <?php echo esc_html($classic_fashion_blog_button_text); ?>
                                                    <span class="screen-reader-text"><?php echo esc_html($classic_fashion_blog_button_text); ?></span>
                                                    <i class="<?php echo esc_attr($classic_fashion_blog_icon); ?>"></i>
                                                </a>
                                            <?php } ?>
                                        <?php } ?>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-6 col-md-6 col-12 align-self-center text-center banner-right-img">
                            <div class="imagebox">
                                <?php if (has_post_thumbnail()) {
                                    the_post_thumbnail('full', array('class' => 'img-fluid'));
                                } else { ?>
                                    <img src="<?php echo esc_url(get_template_directory_uri()); ?>/images/banner-img.png" alt="Default Banner Image" />
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                <?php endwhile;
                wp_reset_postdata();
                ?>
            </div>
            <div class="container">
                <div class="banner-posts">
                    <div class="owl-carousel">
                        <?php
                        if (!empty($classic_fashion_blog_catData)) {
                            $classic_fashion_blog_page_query = new WP_Query(
                                array(
                                    'category_name' => esc_attr($classic_fashion_blog_catData),
                                )
                            );
                            if ($classic_fashion_blog_page_query->have_posts()) {
                                while ($classic_fashion_blog_page_query->have_posts()) : $classic_fashion_blog_page_query->the_post(); ?>
                                    <div class="trending-blog p-2 mb-5 wow zoomIn" data-wow-duration="2s">
                                        <div class="row">
                                            <div class="col-xl-5 col-lg-5 col-md-5 col-sm-4 col-12 align-self-center">
                                                <div class="trending-image">
                                                    <?php if (has_post_thumbnail()) {
                                                        the_post_thumbnail('full', array('class' => 'post-image'));
                                                    } else { ?>
                                                        <img src="<?php echo esc_url(get_template_directory_uri()); ?>/images/dummy-post.png" alt="Default Post Image" class="post-image"/>
                                                    <?php } ?>
                                                </div>
                                            </div>
                                            <div class="col-xl-7 col-lg-7 col-md-7 col-sm-8 col-12 align-self-center">
                                                <h3 class="mb-0"><a href="<?php the_permalink(); ?>" class="text-capitalize"><?php the_title(); ?></a></h3>
                                                <?php
                                                $classic_fashion_blog_trimexcerpt  = get_the_excerpt();
                                                $classic_fashion_blog_shortexcerpt = wp_trim_words($classic_fashion_blog_trimexcerpt, 10);
                                                echo '<p class="slider-content mb-1">' . esc_html($classic_fashion_blog_shortexcerpt) . '</p>';
                                                ?>
                                                <div class="row">
                                                    <div class="col-xl-9 col-lg-8 col-md-9 col-12 align-self-center">
                                                        <?php $classic_fashion_blog_latest_category = classic_fashion_blog_get_latest_category(get_the_ID());
                                                        if ($classic_fashion_blog_latest_category) {
                                                            echo '<a class="post-categories" href="' . esc_url(get_category_link($classic_fashion_blog_latest_category->term_id)) . '">' . esc_html($classic_fashion_blog_latest_category->name) . '</a>';
                                                        } ?>
                                                    </div>
                                                    <div class="col-xl-3 col-lg-4 col-md-3 col-12 align-self-center">
                                                        <a class="service-btn" href="<?php echo esc_url(get_permalink($post->ID)); ?>"><i class="fa-solid fa-arrow-right"></i></a>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                <?php endwhile;
                                wp_reset_postdata();
                            }
                        }
                        ?>
                    </div>
                    <?php if (get_theme_mod('classic_fashion_blog_trending_cat') != "") { ?>
                        <div class="image-bg1"></div>
                        <div class="image-bg2"></div>
                    <?php } ?>
                </div>
            </div>
        </div>
    <?php } ?>

    <!-- Products Section -->
    <?php
        $classic_fashion_blog_hide_trending_section = get_theme_mod('classic_fashion_blog_disabled_product_section', true);
        $classic_fashion_blog_post_cat = get_theme_mod('classic_fashion_blog_posts'); 
        if ($classic_fashion_blog_hide_trending_section){ ?>
        <section id="product-section" class="py-5">
            <div class="container">
                <div class="blog-bx mb-4">
                    <?php if (get_theme_mod('classic_fashion_blog_product_sec_text') != "") { ?>
                        <p class="product-text mb-1 text-capitalize text-start"><?php echo esc_html(get_theme_mod('classic_fashion_blog_product_sec_text', 'classic-fashion-blog')); ?></p>
                    <?php } ?>
                    <?php if (get_theme_mod('classic_fashion_blog_product_sec_title') != "") { ?>
                        <h2 class="product-title mb-1 text-capitalize text-start"><?php echo esc_html(get_theme_mod('classic_fashion_blog_product_sec_title', 'classic-fashion-blog')); ?></h2>
                    <?php } ?>
                </div> 
                <div class="owl-carousel">
                    <?php 
                        if (class_exists('woocommerce')) {
                            $classic_fashion_blog_selected_category = get_theme_mod('classic_fashion_blog_hot_products_cat');
                            
                            if ($classic_fashion_blog_selected_category && $classic_fashion_blog_selected_category !== 'select') {
                                $classic_fashion_blog_args = array(
                                    'post_type' => 'product',
                                    'product_cat' => $classic_fashion_blog_selected_category,
                                    'order' => 'ASC'
                                );
                                $classic_fashion_blog_loop = new WP_Query($classic_fashion_blog_args);
                                
                                if ($classic_fashion_blog_loop->have_posts()) {
                                    while ($classic_fashion_blog_loop->have_posts()) : $classic_fashion_blog_loop->the_post(); 
                                        global $product;
                                        $classic_fashion_blog_product_categories = wp_get_post_terms($product->get_id(), 'product_cat');
                                        if (!empty($classic_fashion_blog_product_categories) && !is_wp_error($classic_fashion_blog_product_categories)) {
                                            $classic_fashion_blog_product_category = $classic_fashion_blog_product_categories[0];
                                        }
                                    ?>
                                    <div class="product-content wow zoomIn delay-3000" data-wow-duration="2s">
                                        <div class="product-image">
                                            <?php
                                            if (has_post_thumbnail($classic_fashion_blog_loop->post->ID)) {
                                                the_post_thumbnail($classic_fashion_blog_loop->post->ID, 'shop_catalog');
                                            } else {
                                                echo '<img src="' . esc_url(wc_placeholder_img_src()) . '" alt="' . esc_attr__('product-image', 'classic-fashion-blog') . '" />';
                                            }
                                            ?>
                                            <div class="image-overlay"></div>
                                            <div class="price mb-2">
                                                <?php 
                                                if ($product->is_on_sale()) {
                                                    echo '<span class="sale-price">' . wc_price($product->get_sale_price()) . '</span>';
                                                }
                                                ?>
                                            </div>
                                            <div class="wishlist-main py-2">
                                                <?php if( $product->is_type( 'simple' ) ){ woocommerce_template_loop_add_to_cart( $classic_fashion_blog_loop->post, $product ); } ?>
                                                <?php if (defined('YITH_WCWL')) { ?>
                                                    <?php echo do_shortcode('[yith_wcwl_add_to_wishlist]'); ?>
                                                <?php } ?>
                                            </div>
                                        </div>
                                        <div class="box-content text-left mt-3 pb-2">
                                            <div class="row">
                                                <div class="col-xl-8 col-lg-8 col-md-8 col-8 align-self-center">
                                                    <h4 class="my-1 text-capitalize product-heading">
                                                        <a href="<?php echo esc_url(get_permalink($classic_fashion_blog_loop->post->ID)); ?>">
                                                            <?php the_title(); ?>
                                                        </a>
                                                    </h4>
                                                </div>
                                                <div class="col-xl-4 col-lg-4 col-md-4 col-4 align-self-center">
                                                    <?php if( $product->is_type( 'simple' ) ){ ?>
                                                        <div class="ratings">
                                                            <?php
                                                            $classic_fashion_blog_average_rating = $product->get_average_rating();
                                                            $classic_fashion_blog_rating_count = $product->get_rating_count();
                                                            if ($classic_fashion_blog_rating_count > 0) {
                                                                echo '<span class="star-rating"><i class="fa-solid fa-star"></i></span>';
                                                                echo '<span class="numeric-rating">' . esc_html($classic_fashion_blog_average_rating) . '</span>';
                                                            }
                                                            ?>
                                                        </div>
                                                    <?php } ?>
                                                </div>
                                            </div>
                                            <div class="row mt-3">
                                                <div class="col-xl-6 col-lg-5 col-md-5 col-5 align-self-center">
                                                    <div class="product-btn">
                                                        <a href="<?php the_permalink(); ?>"><?php esc_html_e('Read more','classic-fashion-blog'); ?></a>
                                                    </div>
                                                </div>
                                                <div class="col-xl-6 col-lg-7 col-md-7 col-7 align-self-center text-end">
                                                    <?php $classic_fashion_blog_checkout_url = wc_get_checkout_url();?>
                                                    <a href="<?php echo esc_url($classic_fashion_blog_checkout_url); ?>" class="checkout-button"><?php esc_html_e('Shop now','classic-fashion-blog'); ?><span class="ms-3"><i class="fa-solid fa-arrow-right"></i></span></a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <?php endwhile; 
                                    wp_reset_postdata();
                                }
                            }
                        }
                    ?>
                </div>
            </div>
        </section>
    <?php } ?>
</div>
<?php get_footer(); ?>