<?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')); ?>">
    <?php if(get_theme_mod('digital_books_top_slider_section_setting') != ''){ ?>
    <?php $digital_books_slide_pages = array();
      for ( $author_book_publisher_count = 1; $author_book_publisher_count <= 3; $author_book_publisher_count++ ) {
        $author_book_publisher_mod = intval( get_theme_mod( 'digital_books_top_slider_page' . $author_book_publisher_count ));
        if ( 'page-none-selected' != $author_book_publisher_mod ) {
          $digital_books_slide_pages[] = $author_book_publisher_mod;
        }
      }
      if( !empty($digital_books_slide_pages) ) :
        $author_book_publisher_args = array(
          'post_type' => 'page',
          'post__in' => $digital_books_slide_pages,
          'orderby' => 'post__in'
        );
        $author_book_publisher_query = new WP_Query( $author_book_publisher_args );
        if ( $author_book_publisher_query->have_posts() ) :
          $i = 1;
    ?>
    <div class="owl-carousel" role="listbox">
      <?php  while ( $author_book_publisher_query->have_posts() ) : $author_book_publisher_query->the_post(); ?>
        <div class="slider-box">
          <?php if(has_post_thumbnail()){
            the_post_thumbnail();
            } else{?>
            <img src="<?php echo esc_url(get_theme_file_uri()); ?>/assets/img/slider.png"/>
          <?php } ?>
          <div class="slider-inner-box">
            <?php if ( get_theme_mod('author_book_publisher_slider_extra_title_setting') ) : ?>
              <h5><?php echo esc_html(get_theme_mod('author_book_publisher_slider_extra_title_setting'));?></h5>
            <?php endif; ?>
            <?php if(get_theme_mod('digital_books_slider_title_setting',1) == 1){ ?>
              <h2><?php the_title(); ?></h2>
            <?php }?>
            <?php if(get_theme_mod('digital_books_slider_content_setting',1) == 1){ ?>
              <p ><?php echo esc_html( wp_trim_words( get_the_content(), 30 )); ?><p>
            <?php }?>
            <div class="slider-btns">
                <?php 
                if ( get_theme_mod('digital_books_slider_button_setting', 1) == 1 
                     && get_theme_mod('digital_books_slider_button_text', 'READ MORE') != '' ) { ?>
                    <div class="slide-btn-1">
                        <a href="<?php the_permalink(); ?>">
                            <?php esc_html_e( get_theme_mod('digital_books_slider_button_text','READ MORE') ); ?>
                        </a>
                    </div>
                <?php } ?>
                <?php 
                $author_book_publisher_button2_text = get_theme_mod('digital_books_slider_button2_text', '');
                $author_book_publisher_button2_url  = get_theme_mod('digital_books_slider_button2_url', '');
                if ( $author_book_publisher_button2_text != '' ) { ?>
                    <div class="slide-btn-2">
                        <a href="<?php echo esc_url( $author_book_publisher_button2_url ); ?>">
                            <?php echo esc_html( $author_book_publisher_button2_text ); ?>
                        </a>
                    </div>
                <?php } ?>
            </div>
          </div>
        </div>
      <?php $i++; endwhile;
      wp_reset_postdata();?>
    </div>
    <?php else : ?>
      <div class="no-postfound"></div>
    <?php endif;
    endif;?>
    <?php }?>
  </section>

<?php if ( get_theme_mod('author_book_publisher_activities_section_setting', false ) ) { ?>
<section class="featured py-5" id="author-product-tabs">
  <div class="container">

    <?php if ( get_theme_mod('author_book_publisher_services_heading') ) { ?>
      <h3 class="main-heading text-center mb-4">
        <?php echo esc_html( get_theme_mod('author_book_publisher_services_heading') ); ?>
      </h3>
    <?php } ?>

    <div class="row mb-3">

      <!-- Left Sidebar Tabs -->
      <div class="col-lg-3 col-md-4 bg-color align-self-center">

        <?php if ( get_theme_mod('author_book_publisher_services_category_heading') ) { ?>
          <p class="cat-head text-center mt-5">
            <?php echo esc_html( get_theme_mod('author_book_publisher_services_category_heading') ); ?>
          </p>
        <?php } ?>

        <div class="men-tabs">
          <ul class="nav nav-tabs" role="tablist">
            <?php for ( $author_book_publisher_i = 1; $author_book_publisher_i <= 5; $author_book_publisher_i++ ) { 
              $tab_title = get_theme_mod('author_book_publisher_slideproduct_tab1title' . $author_book_publisher_i);
              if ( $tab_title ) { ?>
                <li class="nav-item" role="presentation">
                  <button
                    class="nav-link <?php echo $author_book_publisher_i == 1 ? 'active' : ''; ?>"
                    data-bs-toggle="tab"
                    data-bs-target="#tab<?php echo esc_attr( $author_book_publisher_i ); ?>"
                    type="button"
                    role="tab">
                    <?php echo esc_html( $tab_title ); ?>
                  </button>
                </li>
            <?php } } ?>
          </ul>
        </div>

      </div>

      <!-- Right Content -->
      <div class="col-lg-9 col-md-8 align-self-center">
        <div id="mens-product-wrap">
          <div class="tab-content">

            <?php for ( $author_book_publisher_i = 1; $author_book_publisher_i <= 5; $author_book_publisher_i++ ) { ?>
              <div
                class="tab-pane fade <?php echo $author_book_publisher_i == 1 ? 'show active' : ''; ?>"
                id="tab<?php echo esc_attr( $author_book_publisher_i ); ?>"
                role="tabpanel">

                <?php if ( class_exists('WooCommerce') ) { ?>
                  <div class="owl-carousel">

                    <?php
                    $args = array(
                      'post_type' => 'product',
                      'posts_per_page' => -1,
                      'tax_query' => array(
                        array(
                          'taxonomy' => 'product_cat',
                          'field'    => 'slug',
                          'terms'    => get_theme_mod( 'author_book_publisher_cate_tab' . $author_book_publisher_i )
                        )
                      )
                    );

                    $loop = new WP_Query( $args );

                    while ( $loop->have_posts() ) :
                      $loop->the_post();
                      global $product;
                    ?>

                    <div class="sells-product">
                      <div class="mask1">
                        <div class="prodimg_box">
                          <a href="<?php the_permalink(); ?>">
                            <?php
                              if ( has_post_thumbnail() ) {
                                the_post_thumbnail( 'our_product' );
                              } else {
                                echo '<img src="' . esc_url( wc_placeholder_img_src() ) . '" alt="Placeholder">';
                              }
                            ?>
                          </a>
                        </div>

                        <div class="text_box">
                          <h4 class="p-0 mb-2">
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                          </h4>

                          <p class="price mb-0">
                            <?php echo wp_kses_post( $product->get_price_html() ); ?>
                          </p>

                          <?php
                            $short_desc = wp_trim_words( $product->get_short_description(), 3 );
                            if ( $short_desc ) {
                              echo '<p class="product-short-desc">' . esc_html( $short_desc ) . '</p>';
                            }
                          ?>
                        </div>
                      </div>
                    </div>

                    <?php endwhile; wp_reset_postdata(); ?>

                  </div>
                <?php } ?>

              </div>
            <?php } ?>

          </div>
        </div>
      </div>

    </div>
  </div>
</section>
<?php } ?>



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

<?php if(get_theme_mod('digital_books_product_section_setting') != ''){ ?>
  <section id="homepage-product">
    <div class="container">
      <div class="row product-home-box">
        <?php
        if ( class_exists( 'WooCommerce' ) ) {
          $author_book_publisher_args = array(
            'post_type' => 'product',
            'posts_per_page' => 1,
            'product_cat' => get_theme_mod('digital_books_home_product'),
            'order' => 'ASC'
          );
          $loop = new WP_Query( $author_book_publisher_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(), 60 )); ?><p>
              <p><?php if( $product->is_type( 'simple' ) ){ woocommerce_template_loop_rating( $loop->post, $product ); } ?><h6 class="review-box"><?php esc_html_e('Reviews','author-book-publisher'); ?></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 }?>
</main>

<?php get_footer(); ?>