<?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 Toy Store
 */

get_header(); ?>

<div id="content" >

    <?php
        $classic_toy_store_svg_image1 = file_get_contents(get_template_directory_uri() . '/images/wave1.svg');
        $classic_toy_store_svg_image2 = file_get_contents(get_template_directory_uri() . '/images/wave2.svg');
        $classic_toy_store_svg_image3 = file_get_contents(get_template_directory_uri() . '/images/wave3.svg');
        $classic_toy_store_svg_image4 = file_get_contents(get_template_directory_uri() . '/images/wave4.svg');
        $classic_toy_store_image_bg = file_get_contents(get_template_directory_uri() . '/images/img-bg.svg');

        $classic_toy_store_banner = get_theme_mod('classic_toy_store_banner', false);
        $classic_toy_store_banner_pageboxes = get_theme_mod('classic_toy_store_banner_pageboxes', false);

        if ($classic_toy_store_banner && $classic_toy_store_banner_pageboxes) { ?>
        <div id="banner-cat">
            <div class="banner-page">
                <?php
                $classic_toy_store_querymed = new WP_Query(array(
                    'page_id' => esc_attr($classic_toy_store_banner_pageboxes)
                ));
                while ($classic_toy_store_querymed->have_posts()) : $classic_toy_store_querymed->the_post(); ?>
                    <div class="container">
                        <div class="banner-main">
                            <div class="row">
                                <div class="col-xl-8 col-lg-8 col-md-7 col-12 align-self-center">
                                    <div class="bannerbox">
                                        <?php if (get_theme_mod('classic_toy_store_banner_small_title') != '') { ?>
                                            <p class="banner-smalltitle text-capitalize m-0 text-center"><?php echo esc_html(get_theme_mod('classic_toy_store_banner_small_title')); ?></p>
                                        <?php } ?>
                                        <h1 class="my-1 text-capitalize banner-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                                        <?php
                                            $classic_toy_store_trimexcerpt = get_the_excerpt();
                                            $classic_toy_store_shortexcerpt = wp_trim_words($classic_toy_store_trimexcerpt, 38);
                                            echo '<p class="banner-content mb-4">' . esc_html($classic_toy_store_shortexcerpt) . '</p>';
                                        ?>
                                        <div class="bannerbtn">
                                            <?php
                                                $classic_toy_store_button_text = get_theme_mod('classic_toy_store_button_text', 'Explore More');
                                                $classic_toy_store_button_link_banner = esc_url(get_theme_mod('classic_toy_store_button_link_banner', get_permalink()));
                                                if ($classic_toy_store_button_text || !empty($classic_toy_store_button_link_banner)) { ?>
                                                <?php if ($classic_toy_store_button_text != '') { ?>
                                                    <a href="<?php echo esc_url($classic_toy_store_button_link_banner); ?>" class="button">
                                                        <?php echo esc_html($classic_toy_store_button_text); ?>
                                                        <span class="screen-reader-text"><?php echo esc_html($classic_toy_store_button_text); ?></span>
                                                    </a>
                                                <?php } ?>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-xl-4 col-lg-4 col-md-5 col-12 align-self-center text-end banner-img">
                                    <div class="img-bg position-relative">
                                        <?php echo $classic_toy_store_image_bg; ?>
                                        <div class="img-box position-absolute">
                                            <?php if (has_post_thumbnail()) { ?>
                                                <?php the_post_thumbnail('full'); ?>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endwhile;
                    wp_reset_postdata();
                ?>
            </div>
            <div class="wave-box">
                <div class="wave-img1"><?php echo $classic_toy_store_svg_image1; ?></div>
                <div class="wave-img2"><?php echo $classic_toy_store_svg_image2; ?></div>
                <div class="wave-img3"><?php echo $classic_toy_store_svg_image3; ?></div>
                <div class="wave-img4"><?php echo $classic_toy_store_svg_image4; ?></div>
            </div>
        </div>
    <?php } ?>

    <!-- Featured Products Section -->
    <?php 
        $classic_toy_store_hide_featured_products_section = get_theme_mod('classic_toy_store_disabled_featured_products_section', false);
        $classic_toy_store_catData = get_theme_mod('classic_toy_store_featured_products_cat', '0');
        if ($classic_toy_store_hide_featured_products_section) { ?>
        <section id="featured-pro-section" class="pb-5">
            <div class="container">
                <div class="blog-bx mb-2 text-center">
                    <?php if (get_theme_mod('classic_toy_store_featured_products_text')) { ?>
                        <p class="featured-pro-text mb-2 text-capitalize"><?php echo esc_html(get_theme_mod('classic_toy_store_featured_products_text')); ?></p>
                    <?php } ?>
                    <?php if (get_theme_mod('classic_toy_store_featured_products_title')) { ?>
                        <h2 class="featured-pro-title pb-3 mt-1 text-capitalize"><?php echo esc_html(get_theme_mod('classic_toy_store_featured_products_title')); ?></h2>
                    <?php } ?>
                </div>
                <div class="row">
                    <?php 
                        if (class_exists('woocommerce')) {
                            $classic_toy_store_selected_category = get_theme_mod('classic_toy_store_hot_products_cat');
                            
                            if ($classic_toy_store_selected_category && $classic_toy_store_selected_category !== 'select') {
                                $classic_toy_store_args = array(
                                    'post_type' => 'product',
                                    'product_cat' => $classic_toy_store_selected_category,
                                    'order' => 'ASC',
                                    'posts_per_page' => 4
                                );
                                $classic_toy_store_loop = new WP_Query($classic_toy_store_args);
                                
                                if ($classic_toy_store_loop->have_posts()) {
                                    while ($classic_toy_store_loop->have_posts()) : $classic_toy_store_loop->the_post(); 
                                        global $product;
                                        $classic_toy_store_product_categories = wp_get_post_terms($product->get_id(), 'product_cat');
                                        if (!empty($classic_toy_store_product_categories) && !is_wp_error($classic_toy_store_product_categories)) {
                                            $classic_toy_store_product_category = $classic_toy_store_product_categories[0];
                                        }
                                        $classic_toy_store_regular_price = $product->get_regular_price();
                                        $classic_toy_store_sale_price = $product->get_sale_price();
                                        $classic_toy_store_percentage_discount = 0;

                                        if ($classic_toy_store_regular_price && $classic_toy_store_sale_price) {
                                            $classic_toy_store_percentage_discount = 1 * round( ( ($classic_toy_store_regular_price - $classic_toy_store_sale_price) / $classic_toy_store_regular_price ) * 100 ); 
                                        }
                                    ?>
                                    <div class="col-xl-3 col-lg-3 col-md-6 col-12 mb-4">
                                        <div class="product-content position-relative">
                                            <div class="product-image position-relative">
                                                <?php if($classic_toy_store_percentage_discount != 0) { ?>
                                                    <div class="discount-badge position-absolute"><?php echo esc_html__('OFF' ,'classic-toy-store'); ?> <?php echo $classic_toy_store_percentage_discount; ?>%</div>
                                                <?php } ?>

                                                <?php
                                                if (has_post_thumbnail($classic_toy_store_loop->post->ID)) {
                                                    the_post_thumbnail($classic_toy_store_loop->post->ID, 'shop_catalog');
                                                } else {
                                                    echo '<img src="' . esc_url(wc_placeholder_img_src()) . '"/>';
                                                }
                                                ?>
                                            </div>
                                            <div class="row">
                                                <div class="col-xl-9 col-lg-9 col-md-9 col-12">
                                                    <?php 
                                                        $classic_toy_store_product_categories = get_the_terms( get_the_ID(), 'product_cat' );
                                                        if ( $classic_toy_store_product_categories ) {
                                                            foreach ( $classic_toy_store_product_categories as $classic_toy_store_category ) {
                                                                echo '<div class="product-cat text-capitalize"><a href="' . get_term_link( $classic_toy_store_category ) . '">' . $classic_toy_store_category->name . '</a></div>';
                                                            }
                                                    }?>
                                                </div>
                                                <div class="col-xl-3 col-lg-3 col-md-3 col-12">
                                                    <?php if( $product->is_type( 'simple' ) ){ ?>
                                                        <div class="ratings">
                                                            <?php
                                                            $classic_toy_store_average_rating = $product->get_average_rating();
                                                            $classic_toy_store_rating_count = $product->get_rating_count();
                                                            if ($classic_toy_store_rating_count > 0) {
                                                                echo '<span class="star-rating"><i class="fa-solid fa-star"></i></span>';
                                                                echo '<span class="numeric-rating">' . esc_html($classic_toy_store_average_rating) . '</span>';
                                                            }
                                                            ?>
                                                        </div>
                                                    <?php } ?>
                                                </div>
                                            </div>
                                            
                                            <h3 class="text-capitalize product-heading">
                                                <a href="<?php echo esc_url(get_permalink($classic_toy_store_loop->post->ID)); ?>">
                                                    <?php the_title(); ?>
                                                </a>
                                            </h3>
                                            <div class="row">
                                                <div class="col-xl-9 col-lg-9 col-md-9 col-12">
                                                    <div class="price">
                                                        <?php echo $product->get_price_html(); ?>
                                                    </div>
                                                </div>
                                                <div class="col-xl-3 col-lg-3 col-md-3 col-12">
                                                    <div class="product-btn">
                                                        <a href="<?php the_permalink(); ?>"><i class="fa-solid fa-plus"></i></a>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <?php endwhile; 
                                    wp_reset_postdata();
                                }
                            }
                        }
                    ?>
                </div>
            </div>
        </section>
    <?php } ?>
</div>
<?php get_footer(); ?>
