<?php
/**
 * Template Name: Custom Front Page
 */

get_header(); ?>

<div class="container">
  <div class="row">
    <div class="col-lg-3 col-md-3">
      <div class="sidebar">
        <div class="sidebar">
          <?php if ( is_active_sidebar( 'sidebar-2' ) ) : ?>

              <?php dynamic_sidebar( 'sidebar-2' ); ?>

          <?php else : ?>

              <!-- Search Widget -->
              <section id="search" class="widget widget_search">
                  <h5 class="widget-title"><?php esc_html_e( 'Search', 'ecommerce-zone' ); ?></h5>
                  <?php get_search_form(); ?>
              </section>

              <!-- Archives Widget -->
              <section id="archives" class="widget widget_archive">
                  <h5 class="widget-title"><?php esc_html_e( 'Archives List', 'ecommerce-zone' ); ?></h5>
                  <ul>
                      <?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>
                  </ul>
              </section>

              <!-- Recent Posts Widget -->
              <section id="recent-posts-widget" class="widget widget_recent_posts">
                  <h5 class="widget-title"><?php esc_html_e( 'Recent Posts', 'ecommerce-zone' ); ?></h5>
                  <ul>
                      <?php
                      $recent_posts = wp_get_recent_posts( array(
                          'numberposts' => 5,
                          'post_status' => 'publish',
                      ) );
                      foreach ( $recent_posts as $post ) :
                      ?>
                          <li>
                              <a href="<?php echo esc_url( get_permalink( $post['ID'] ) ); ?>">
                                  <?php echo esc_html( $post['post_title'] ); ?>
                              </a>
                          </li>
                      <?php endforeach; ?>
                  </ul>
              </section>
              
              <!-- Meta Widget -->
              <section id="meta" class="widget widget_meta">
                  <h5 class="widget-title"><?php esc_html_e( 'Meta', 'ecommerce-zone' ); ?></h5>
                  <ul>
                      <?php wp_register(); ?>
                      <li><?php wp_loginout(); ?></li>
                      <?php wp_meta(); ?>
                  </ul>
              </section>

              <!-- Categories Widget -->
              <section id="categories" class="widget widget_categories">
                  <h5 class="widget-title"><?php esc_html_e( 'Categories', 'ecommerce-zone' ); ?></h5>
                  <ul>
                      <?php wp_list_categories( array( 'title_li' => '' ) ); ?>
                  </ul>
              </section>

              <!-- Tag Cloud Widget -->
              <section id="tags" class="widget widget_tag_cloud">
                  <h5 class="widget-title"><?php esc_html_e( 'Tags', 'ecommerce-zone' ); ?></h5>
                  <div class="tagcloud">
                      <?php wp_tag_cloud( array(
                          'smallest' => 10,
                          'largest'  => 22,
                          'unit'     => 'px',
                          'number'   => 20
                      ) ); ?>
                  </div>
              </section>
          <?php endif; ?>
      </div>
      </div>
    </div>
    <div class="col-lg-9 col-md-9">
    <?php if(get_theme_mod('ecommerce_zone_top_slider_setting',true) != ''){ ?>
      <section id="top-slider" slider-loop="<?php echo esc_html(get_theme_mod('ecommerce_zone_slider_loop')); ?>">
        <?php $ecommerce_zone_slide_pages = array();
          for ( $ecommerce_zone_count = 1; $ecommerce_zone_count <= 3; $ecommerce_zone_count++ ) {
            $ecommerce_zone_mod = intval( get_theme_mod( 'ecommerce_zone_top_slider_page' . $ecommerce_zone_count ));
            if ( 'page-none-selected' != $ecommerce_zone_mod ) {
              $ecommerce_zone_slide_pages[] = $ecommerce_zone_mod;
            }
          }
          if( !empty($ecommerce_zone_slide_pages) ) :
            $ecommerce_zone_args = array(
              'post_type' => 'page',
              'post__in' => $ecommerce_zone_slide_pages,
              'orderby' => 'post__in'
            );
            $ecommerce_zone_query = new WP_Query( $ecommerce_zone_args );
            if ( $ecommerce_zone_query->have_posts() ) :
              $i = 1;
        ?>
        <div class="owl-carousel" role="listbox">
          <?php  while ( $ecommerce_zone_query->have_posts() ) : $ecommerce_zone_query->the_post(); ?>
            <div class="slider-box">
              <?php if(has_post_thumbnail()){
            the_post_thumbnail();
            } else{?>
            <img src="<?php echo esc_url(get_template_directory_uri()); ?>/assets/img/slider.png" alt="" />
          <?php } ?>
              <div class="slider-inner-box">
                <?php if(get_theme_mod('ecommerce_zone_slider_title_setting',1) == 1){ ?>
                  <h2><?php the_title(); ?></h2>
                <?php }?>
                <?php if(get_theme_mod('ecommerce_zone_slider_button_setting',1) == 1 && get_theme_mod('ecommerce_zone_slider_button_text','SHOP NOW') != ''){ ?>
                  <div class="more-btn">
                    <a href="<?php the_permalink(); ?>"><?php esc_html_e(get_theme_mod('ecommerce_zone_slider_button_text','SHOP NOW')); ?><i class="fas fa-caret-right"></i></a>
                  </div>
                <?php }?>
              </div>
            </div>
          <?php $i++; endwhile;
          wp_reset_postdata();?>
        </div>
        <?php else : ?>
          <div class="no-postfound"></div>
        <?php endif;
        endif;?>
      </section>
      <?php } ?>

      <?php if(get_theme_mod('ecommerce_zone_category_product_setting',true)){ ?>
      <section id="cat-slider">
        <?php $ecommerce_zone_slide_pages = array();
          for ( $ecommerce_zone_count = 1; $ecommerce_zone_count <= 3; $ecommerce_zone_count++ ) {
            $ecommerce_zone_mod = intval( get_theme_mod( 'ecommerce_zone_category_product_page' . $ecommerce_zone_count ));
            if ( 'page-none-selected' != $ecommerce_zone_mod ) {
              $ecommerce_zone_slide_pages[] = $ecommerce_zone_mod;
            }
          }
          if( !empty($ecommerce_zone_slide_pages) ) :
            $ecommerce_zone_args = array(
              'post_type' => 'page',
              'post__in' => $ecommerce_zone_slide_pages,
              'orderby' => 'post__in'
            );
            $ecommerce_zone_query = new WP_Query( $ecommerce_zone_args );
            if ( $ecommerce_zone_query->have_posts() ) :
              $i = 1;
        ?>
        <div class="row">
          <?php while ( $ecommerce_zone_query->have_posts() ) : $ecommerce_zone_query->the_post(); ?>
            <div class="col-lg-4 col-md-4">
              <div class="cat-product-box">
                <img src="<?php the_post_thumbnail_url('full'); ?>"/>
                <div class="cat-pro-inner-box">
                  <h3><?php the_title(); ?></h3>
                  <div class="more-btn">
                    <a href="<?php the_permalink(); ?>"><?php esc_html_e('SHOP NOW','ecommerce-zone'); ?><i class="fas fa-caret-right"></i></a>
                  </div>
                </div>
              </div>
            </div>
          <?php $i++; endwhile;
          wp_reset_postdata();?>
          <?php else : ?>
            <div class="no-postfound"></div>
          <?php endif;
          endif;?>
        </div>
      </section>
      <?php } ?>

      <?php if(get_theme_mod('ecommerce_zone_home_product_setting',true) != ''){ ?>
      <section id="homepage-product">
        <h3><?php echo esc_html(get_theme_mod('ecommerce_zone_home_product_title','')); ?></h3>
        <div class="row product-home-box">
          <?php
          if ( class_exists( 'WooCommerce' ) ) {
            $ecommerce_zone_args = array(
            'post_type' => 'product',
            'posts_per_page' => get_theme_mod('ecommerce_zone_home_product_number'),
            'product_cat' => get_theme_mod('ecommerce_zone_home_product'),
            'order' => 'ASC'
            );
            $loop = new WP_Query( $ecommerce_zone_args );
            while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
              <div class="col-lg-3 col-md-4 col-sm-6">
                <div class="product-box">
                  <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>
                  <h4><a href="<?php echo esc_url(get_permalink( $loop->post->ID )); ?>">
                    <?php the_title(); ?><span class="screen-reader-text"><?php echo esc_html('best-seller-content', 'ecommerce-zone' ) ; ?></span>
                  </a></h4>
                  <p class="price"><?php echo $product->get_price_html(); ?></p>
                  <div class="pro-button">
                    <?php if( $product->is_type( 'simple' ) ){ woocommerce_template_loop_add_to_cart( $loop->post, $product ); } ?>
                  </div>
                </div>
              </div>
            <?php endwhile; wp_reset_query(); ?>
          <?php } ?>
        </div>
      </section>
      <?php } ?>
    </div>
  </div>
</div>

<?php get_footer(); ?>
