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

get_header(); ?>

<main id="skip-content">
  <section id="top-slider" slider-loop="<?php echo esc_html(get_theme_mod('digital_books_slider_loop', true)); ?>">
    <div class="row m-0">
      <div class="col-lg-7 col-md-7 col-sm-12 col-12 pl-0 slide-box">
        <div class="owl-carousel">
          <?php
            $bookseller_publication_slider_cat = get_theme_mod('bookseller_publication_slider_sec_category','');
            $bookseller_publication_slider_per_page = get_theme_mod('bookseller_publication_slider_per_page',3);
            if($bookseller_publication_slider_cat){
              $bookseller_publication_page_query5 = new WP_Query(array( 'category_name' => esc_html($bookseller_publication_slider_cat,'bookseller-publication'),'posts_per_page' => esc_attr( $bookseller_publication_slider_per_page )));
              $i=1;
              while( $bookseller_publication_page_query5->have_posts() ) : $bookseller_publication_page_query5->the_post(); ?>
                <div class="slider-box">
                   <?php if(has_post_thumbnail()){ ?><img src="<?php the_post_thumbnail_url('full'); ?>"/><?php }else { ?><div class="slide-bg"></div> <?php } ?>
                    <div class="slider-inner-box">
                      <?php if(get_theme_mod('bookseller_publication_slider_title_setting',1) == 1){ ?>
                        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                      <?php }?>
                    </div>
                </div>
              <?php $i++; endwhile;
            wp_reset_postdata();
          } ?>
        </div>
      </div>
      <div class="col-lg-5 col-md-5 col-sm-12 col-12 ">
        <div class="slider-post">
          <?php
            $bookseller_publication_slider_2_cat = get_theme_mod('bookseller_publication_slider_2_category','');
            $bookseller_publication_slider_2_per_page = get_theme_mod('bookseller_publication_slider_2_per_page',4);
            if($bookseller_publication_slider_2_cat){
              $bookseller_publication_page_query5 = new WP_Query(array( 'category_name' => esc_html($bookseller_publication_slider_2_cat,'bookseller-publication'),'posts_per_page' => esc_attr( $bookseller_publication_slider_2_per_page )));
              $i=1;
              while( $bookseller_publication_page_query5->have_posts() ) : $bookseller_publication_page_query5->the_post(); ?>
                  <div class="main-box">
                    <div class="content-box">
                      <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                      <p class="content mt-3 mb-2"><?php echo esc_html( wp_trim_words( get_the_content(), 30 )); ?><p>
                      <a class="slider-btn" href="<?php the_permalink(); ?>"><?php esc_html_e('Read More','bookseller-publication'); ?></a>
                    </div>
                     <hr>
                </div>
              <?php $i++; endwhile;
            wp_reset_postdata();
          } ?>
        </div>
        <?php if(get_theme_mod('bookseller_publication_slider_title','') != '' || get_theme_mod('bookseller_publication_slider_time','') != '' ){ ?>
          <div class="time-box">
            <?php if(get_theme_mod('bookseller_publication_slider_title','') != ''){ ?>
              <h6 class="text-start "><?php echo esc_html(get_theme_mod('bookseller_publication_slider_title','')); ?></h>
            <?php }?>
            <?php if(get_theme_mod('bookseller_publication_slider_time','') != ''){ ?>
              <h3 class="text-start "><?php echo esc_html(get_theme_mod('bookseller_publication_slider_time','')); ?></h3>
            <?php }?>
          </div>
        <?php }?>
      </div>
    </div>
  </section>

  <?php if(get_theme_mod('digital_books_product_section_setting', true) != ''){ ?>
    <section id="homepage-product">
      <div class="container">
        <div class="row product-home-box">
          <?php
          if ( class_exists( 'WooCommerce' ) ) {
            $args = array( 
              'post_type' => 'product',
              'posts_per_page' => 1,
              'product_cat' => get_theme_mod('digital_books_home_product'),
              'order' => 'ASC'
            );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
              <div class="col-lg-6 col-md-6">
                <div class="product-image">
                  <span class="product-sale-tag">
                    <?php woocommerce_show_product_sale_flash( $post, $product ); ?>
                  </span>
                  <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.esc_url(wc_placeholder_img_src()).'" />'; ?>
                </div>
              </div>
              <div class="col-lg-6 col-md-6">
                <h3><a href="<?php echo esc_url(get_permalink( $loop->post->ID )); ?>"><?php the_title(); ?></a></h3>
                <h4><?php echo esc_url(wc_get_product_category_list($product->get_id())); ?></h4>
                <p><?php echo esc_html( wp_trim_words( get_the_content(), 20 )); ?><p>
                <p><?php if( $product->is_type( 'simple' ) ){ woocommerce_template_loop_rating( $loop->post, $product ); } ?><h6 class="review-box"><?php esc_html_e('Reviews','bookseller-publication'); ?></h6></p>              
                  <div class="counter_info">
                    <div class="row">
                      <?php for ( $i = 1; $i <= 4; $i++ ) { ?>
                        <div class="col-lg-3 col-md-6 col-6">
                          <?php if(get_theme_mod('digital_books_home_product_number'.$i) != '' || get_theme_mod('digital_books_home_product_text'.$i)){ ?>
                            <div class="<?php echo('counter_box').$i ?>">
                              <h4><?php echo esc_html(get_theme_mod('digital_books_home_product_number'. $i)); ?></h4>
                              <h5><?php echo esc_html(get_theme_mod('digital_books_home_product_text'. $i)); ?></h5>
                            </div>
                          <?php }?>
                        </div>
                      <?php }?>
                    </div>
                  </div>
                <div class="pro-button">
                  <?php if( $product->is_type( 'simple' ) ){ woocommerce_template_loop_add_to_cart( $loop->post, $product ); } ?>
                </div>
              </div>
            <?php endwhile; wp_reset_query(); ?>
          <?php } ?>
        </div>
      </div>
    </section>
  <?php } ?>

  <section id="latest-product" class="py-5">
    <div class="container">
      <?php if(get_theme_mod('bookseller_publication_latest_product_title','') != ''){ ?>
        <h3 class="text-center mb-4"><?php echo esc_html(get_theme_mod('bookseller_publication_latest_product_title','')); ?></h3>
      <?php }?>
      <?php if(get_theme_mod('bookseller_publication_latest_product_sub_title','') != ''){ ?>
        <p class="text-center"><?php echo esc_html(get_theme_mod('bookseller_publication_latest_product_sub_title','')); ?></p>
      <?php }?>
      <div class="owl-carousel">
        <?php
        if ( class_exists( 'WooCommerce' ) ) {
          $args = array( 
            'post_type' => 'product',
            'posts_per_page' => get_theme_mod('bookseller_publication_latest_number'),
            'product_cat' => get_theme_mod('bookseller_publication_latest_product'),
            'order' => 'ASC'
          );
          $loop = new WP_Query( $args );
          while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
            <div class="">
              <div class="product-box mb-4">
                <div class="product-image">
                  <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.esc_url(wc_placeholder_img_src()).'" />'; ?>
                </div>
                <div class="product-content">
                  <h5 class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>"><?php echo $product->get_price_html(); ?></h5>
                  <h3 class="pt-4 mb-0"><a href="<?php echo esc_url(get_permalink( $loop->post->ID )); ?>"><?php the_title(); ?></a></h3>
                  <p class=" mb-0"><?php echo esc_html( wp_trim_words( get_the_content(), 8 )); ?></p>
                </div>
              </div>
            </div>
          <?php endwhile; wp_reset_query(); ?>
        <?php } ?>
      </div>
    </div>
  </section>

  <section id="content-section" class="container">
    <?php
      if ( have_posts() ) : 
        while ( have_posts() ) : the_post();
          the_content();
        endwhile; 
      endif; 
    ?>
  </section>
</main>

<?php get_footer(); ?>