<?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 Gaming
 */

get_header(); ?>

<div id="content" >
    <?php
    $classic_gaming_svg_image = file_get_contents(get_template_directory_uri() . '/images/wave.svg');
    $classic_gaming_banner = get_theme_mod('classic_gaming_banner', true);
    $classic_gaming_banner_pageboxes = get_theme_mod('classic_gaming_banner_pageboxes', false);

    if ($classic_gaming_banner && $classic_gaming_banner_pageboxes) { ?>
        <div id="banner-cat">
            <div class="container">
                <div class="page-box">
                    <div class="row">
                        <div class="col-xl-8 col-lg-8 col-md-12 col-12 align-self-center">
                            <div class="banner-page">
                                <?php
                                $classic_gaming_querymed = new WP_Query(array(
                                    'page_id' => esc_attr($classic_gaming_banner_pageboxes)
                                ));
                                while ($classic_gaming_querymed->have_posts()) : $classic_gaming_querymed->the_post(); ?>
                                    <div class="banner-main">
                                        <div class="row">
                                            <div class="col-xl-6 col-lg-6 col-md-6 col-12 align-self-center">
                                                <div class="bannerbox wow zoomInUp delay-1000" data-wow-duration="2s">
                                                    <?php if (get_theme_mod('classic_gaming_banner_small_title') != '') { ?>
                                                        <p class="banner-smalltitle text-uppercase m-0"><?php echo esc_html(get_theme_mod('classic_gaming_banner_small_title')); ?></p>
                                                    <?php } ?>
                                                    <h1 class="my-3 text-capitalize banner-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                                                    <?php
                                                        $classic_gaming_trimexcerpt = get_the_excerpt();
                                                        $classic_gaming_shortexcerpt = wp_trim_words($classic_gaming_trimexcerpt, 38);
                                                        echo '<p class="banner-content mb-5">' . esc_html($classic_gaming_shortexcerpt) . '</p>';
                                                    ?>
                                                    <div class="bannerbtn">
                                                        <?php
                                                        $classic_gaming_button_text = get_theme_mod('classic_gaming_button_text', 'READ MORE');
                                                        $classic_gaming_button_link_banner = esc_url(get_theme_mod('classic_gaming_button_link_banner', get_permalink()));
                                                        if ($classic_gaming_button_text || !empty($classic_gaming_button_link_banner)) { ?>
                                                            <?php if ($classic_gaming_button_text != '') { ?>
                                                                <a href="<?php echo esc_url($classic_gaming_button_link_banner); ?>" class="button">
                                                                    <?php echo esc_html($classic_gaming_button_text); ?>
                                                                    <span class="screen-reader-text"><?php echo esc_html($classic_gaming_button_text); ?></span><i class="fa-solid fa-arrow-right-long ps-3"></i>
                                                                </a>
                                                            <?php } ?>
                                                        <?php } ?>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="col-xl-6 col-lg-6 col-md-6 col-12 align-self-center text-center banner-img wow zoomIn" data-wow-duration="2s">
                                            <?php 
                                                if (has_post_thumbnail()) { 
                                                    the_post_thumbnail('full'); 
                                                } 
                                                ?>
                                            </div>
                                        </div>
                                    </div>
                                <?php endwhile;
                                wp_reset_postdata();
                                ?>
                            </div>
                        </div>
                        <div class="col-xl-4 col-lg-4 col-md-12 col-12 align-self-center post-box">
                            <?php
                                $classic_gaming_featured_post = get_theme_mod('classic_gaming_posts_number');
                                for ($classic_gaming_i=1; $classic_gaming_i <= $classic_gaming_featured_post; $classic_gaming_i++) { 
                                $classic_gaming_postData=  get_theme_mod('classic_gaming_select_post'.$classic_gaming_i);
                                if($classic_gaming_postData){ ?>
                                <?php
                                    $classic_gaming_args = array(
                                        'p' => esc_html($classic_gaming_postData ,'classic-gaming'),
                                        'posts_per_page' => 2,
                                        'post_type' => 'post'
                                    );
                                    $classic_gaming_query = new WP_Query( $classic_gaming_args );
                                    if ( $classic_gaming_query->have_posts() ) :
                                    while ( $classic_gaming_query->have_posts() ) : $classic_gaming_query->the_post(); ?>
                                    <div class="game-box wow zoomInUp delay-1000" data-wow-duration="2s">
                                        <div class="row">
                                            <div class="col-xl-4 col-lg-4 col-md-6 col-12 align-self-center">
                                                <div class="post-thumb m-0 position-relative">
                                                    <?php if(has_post_thumbnail()){
                                                        the_post_thumbnail('full');
                                                    } else{?>
                                                        <img src="<?php echo esc_url(get_template_directory_uri()); ?>/images/post-image.png" alt="<?php echo esc_attr( 'post-image', 'classic-gaming'); ?>"/>
                                                    <?php } ?>
                                                </div>
                                            </div>
                                            <div class="col-xl-8 col-lg-8 col-md-6 col-12 align-self-center">
                                                <h3 class="text-capitalize mb-2 post-heading"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
                                                <div class="release-date">
                                                    <p class="mb-0"><?php echo esc_html__('Initial Release Date', 'classic-gaming'); ?></p><?php 
                                                        $classic_gaming_post_date = get_the_date('j F Y'); 
                                                        echo esc_html($classic_gaming_post_date); 
                                                    ?>
                                                </div>
                                                <div class="service-content pt-2">
                                                    <div class="row">
                                                        <div class="col-xl-6 col-lg-6 col-md-6 col-6 align-self-center">
                                                            <?php if (get_theme_mod('classic_gaming_add_steam_count'.$classic_gaming_i) != "") { ?>
                                                                <p class="steam-count mb-2"><i class="<?php echo esc_attr(get_theme_mod('classic_gaming_steam_icon'.$classic_gaming_i, 'fa-brands fa-steam')); ?> me-3"></i><?php echo esc_html(get_theme_mod('classic_gaming_add_steam_count'.$classic_gaming_i)); ?></p>
                                                            <?php } ?>
                                                        </div>
                                                        <div class="col-xl-6 col-lg-6 col-md-6 col-6 align-self-center">
                                                            <?php if (get_theme_mod('classic_gaming_add_score'.$classic_gaming_i) != "") { ?>
                                                                <p class="play-count mb-2"><i class="<?php echo esc_attr(get_theme_mod('classic_gaming_score_icon'.$classic_gaming_i, 'fa-brands fa-playstation')); ?> me-3"></i><?php echo esc_html(get_theme_mod('classic_gaming_add_score'.$classic_gaming_i)); ?></p>
                                                            <?php } ?>
                                                        </div>
                                                    </div>
                                                </div> 
                                                <div class="game-content"><?php echo esc_html( get_the_excerpt() ); ?></div>
                                            </div>
                                        </div>
                                    </div> 
                                <?php endwhile;
                                wp_reset_postdata();
                                endif; ?>
                            <?php }}?>
                        </div>
                    </div>
                </div>
                <div class="banner-mail">
                    <?php if ( get_theme_mod('classic_gaming_banner_email_address')) { ?>
                        <i class="fa-solid fa-envelope me-2"></i><a class="mail" href="mailto:<?php echo esc_attr( get_theme_mod('classic_gaming_banner_email_address','') ); ?>"><?php echo esc_html(get_theme_mod ('classic_gaming_banner_email_address','')); ?></a>
                    <?php } ?>
                </div>
                <div class="social-icons">
                    <?php if ( get_theme_mod('classic_gaming_fb_link') != "") { ?>
                        <a title="<?php echo esc_attr('facebook', 'classic-gaming'); ?>" target="_blank" href="<?php echo esc_url(get_theme_mod('classic_gaming_fb_link')); ?>"><i class="fa-brands fa-facebook-f"></i></a> 
                    <?php } ?>
                    <?php if ( get_theme_mod('classic_gaming_youtube_link') != "") { ?> 
                        <a title="<?php echo esc_attr('youtube', 'classic-gaming'); ?>" target="_blank" href="<?php echo esc_url(get_theme_mod('classic_gaming_youtube_link')); ?>"><i class="fa-brands fa-youtube"></i></a>
                    <?php } ?>
                    <?php if ( get_theme_mod('classic_gaming_twitter_link') != "") { ?> 
                        <a title="<?php echo esc_attr('twitter', 'classic-gaming'); ?>" target="_blank" href="<?php echo esc_url(get_theme_mod('classic_gaming_twitter_link')); ?>"><i class="fa-brands fa-twitter"></i></a>
                    <?php } ?>
                    <?php if ( get_theme_mod('classic_gaming_steam_link') != "") { ?>
                        <a title="<?php echo esc_attr('steam', 'classic-gaming'); ?>" target="_blank" href="<?php echo esc_url(get_theme_mod('classic_gaming_steam_link')); ?>"><i class="fa-brands fa-steam"></i></a>
                    <?php } ?>
                    <?php if ( get_theme_mod('classic_gaming_behance_link') != "") { ?>
                        <a title="<?php echo esc_attr('behance', 'classic-gaming'); ?>" target="_blank" href="<?php echo esc_url(get_theme_mod('classic_gaming_behance_link')); ?>"><i class="fa-brands fa-behance"></i></a>
                    <?php } ?>
                </div>
            </div>
            <div class="wave-img">
                <?php echo $classic_gaming_svg_image; ?>
            </div>
            <div class="banner-bg position-absolute">
                <?php if (get_theme_mod('classic_gaming_banner_bg_img')) { ?>
                    <img src="<?php echo esc_url(get_theme_mod('classic_gaming_banner_bg_img')); ?>" alt="<?php echo esc_attr( 'banner', 'classic-gaming'); ?>"/> 
                <?php } else { ?>
                    <img src="<?php echo esc_url(get_template_directory_uri()); ?>/images/image-bg.png" alt="<?php echo esc_attr( 'image-bg', 'classic-gaming'); ?>"/>
                <?php } ?>
            </div>
            <div class="left-box"></div>
            <div class="bottom-box"></div>
            <div class="right-box"></div>
            <div class="right-middle"></div>
            <div class="bottom-middle"></div>
        </div>
    <?php } ?>

    <!-- Products Section -->
    <?php
        $classic_gaming_hide_product_section = get_theme_mod('classic_gaming_disabled_product_section', true);
        if ($classic_gaming_hide_product_section){ ?>
        <section id="product-section" class="py-5">
            <div class="container">
                <div class="blog-bx">
                    <div class="row">
                        <div class="col-xl-5 col-lg-5 col-md-5 col-12 mb-2 align-self-center">
                            <?php if (get_theme_mod('classic_gaming_product_title') != "") { ?>
                                <h2 class="section-title text-capitalize"><?php echo esc_html(get_theme_mod('classic_gaming_product_title')); ?></h2>
                            <?php } ?>
                        </div>
                        <div class="col-xl-2 col-lg-2 col-md-2 col-12"></div>
                        <div class="col-xl-5 col-lg-5 col-md-5 col-12 mb-2 align-self-center">
                            <?php if (get_theme_mod('classic_gaming_product_text') != "") { ?>
                                <p class="section-text mb-0"><?php echo esc_html(get_theme_mod('classic_gaming_product_text')); ?></p>
                            <?php } ?>
                        </div>
                    </div>
                </div> 
                <div class="row">
                    <?php 
                        if (class_exists('woocommerce')) {
                            $classic_gaming_selected_category = get_theme_mod('classic_gaming_hot_products_cat');
                            
                            if ($classic_gaming_selected_category && $classic_gaming_selected_category !== 'select') {
                                $classic_gaming_args = array(
                                    'post_type' => 'product',
                                    'product_cat' => $classic_gaming_selected_category,
                                    'order' => 'ASC',
                                    'posts_per_page' => 4
                                );
                                $classic_gaming_loop = new WP_Query($classic_gaming_args);
                                
                                if ($classic_gaming_loop->have_posts()) {
                                    while ($classic_gaming_loop->have_posts()) : $classic_gaming_loop->the_post(); 
                                        global $product;
                                        $classic_gaming_product_categories = wp_get_post_terms($product->get_id(), 'product_cat');
                                        if (!empty($classic_gaming_product_categories) && !is_wp_error($classic_gaming_product_categories)) {
                                            $classic_gaming_product_category = $classic_gaming_product_categories[0];
                                        }
                                    ?>
                                    <div class="col-xl-3 col-lg-3 col-md-6 col-12 mb-4">
                                        <div class="product-content wow zoomIn delay-3000" data-wow-duration="2s">
                                            <div class="product-image text-center">
                                                <div class="main-tag">
                                                    <?php 
                                                        $classic_gaming_product_tags = get_the_terms(get_the_ID(), 'product_tag');
                                                        if ($classic_gaming_product_tags && !is_wp_error($classic_gaming_product_tags)){
                                                            foreach ($classic_gaming_product_tags as $classic_gaming_tag) {
                                                                echo '<div class="product-tag"><a href="' . esc_url(get_term_link($classic_gaming_tag)) . '">' . esc_html($classic_gaming_tag->name) . '</a></div>';
                                                            }
                                                        }
                                                    ?>
                                                </div>
                                                <?php
                                                if (has_post_thumbnail($classic_gaming_loop->post->ID)) {
                                                    the_post_thumbnail($classic_gaming_loop->post->ID, 'shop_catalog');
                                                } else {
                                                    echo '<img src="' . esc_url(wc_placeholder_img_src()) . '" alt="' . esc_attr__('image', 'classic-gaming') . '" />';

                                                }
                                                ?>
                                                <div class="addtocart">
                                                    <?php if( $product->is_type( 'simple' ) ){ woocommerce_template_loop_add_to_cart( $classic_gaming_loop->post, $product ); } ?><i class="fa-solid fa-arrow-right-long ms-1"></i>
                                                </div>
                                            </div>
                                            <?php 
                                                $classic_gaming_product_categories = get_the_terms( get_the_ID(), 'product_cat' );
                                                if ( $classic_gaming_product_categories ) {
                                                    foreach ( $classic_gaming_product_categories as $classic_gaming_category ) {
                                                        echo '<div class="product-cat"><a href="' . get_term_link( $classic_gaming_category ) . '">' . $classic_gaming_category->name . '</a></div>';
                                                    }
                                            }?>
                                            <h4 class="my-1 text-capitalize product-heading">
                                                <a href="<?php echo esc_url(get_permalink($classic_gaming_loop->post->ID)); ?>">
                                                    <?php the_title(); ?>
                                                </a>
                                            </h4>
                                            <div class="price">
                                                <?php 
                                                if ($product->is_on_sale()) {
                                                    echo '<span class="sale-price">' . wc_price($product->get_sale_price()) . '</span>';
                                                }
                                                ?>
                                            </div>
                                        </div>
                                    </div>
                                    <?php endwhile; 
                                    wp_reset_postdata();
                                }
                            }
                        }
                    ?>
                </div>
            </div>
        </section>
    <?php } ?>
</div>
<?php get_footer(); ?>
