<?php
/**
 * Template Name: Custom home
 */
get_header(); ?>

<main role="main" role="main" id="maincontent">
  <?php do_action( 'perfect_blog_before_slider' ); ?>

  <?php /** slider section **/ ?>
  <?php if( get_theme_mod('perfect_blog_slider_hide_show', true) != '' || get_theme_mod('perfect_blog_responsive_slider', false) != ''){ ?>
    <section id="slider" class="mw-100 m-auto p-0">
      <div id="carouselExampleCaptions" class="carousel slide" data-bs-ride="carousel" data-interval="<?php echo esc_attr(get_theme_mod('perfect_blog_slider_speed_option', 3000)); ?>"> 
        <?php $perfect_blog_slider_pages = array();
          for ( $count = 1; $count <= 4; $count++ ) {
            $mod = intval( get_theme_mod( 'perfect_blog_slidersettings_page' . $count ));
            if ( 'page-none-selected' != $mod ) {
              $perfect_blog_slider_pages[] = $mod;
            }
          }
          if( !empty($perfect_blog_slider_pages) ) :
          $args = array(
              'post_type' => 'page',
              'post__in' => $perfect_blog_slider_pages,
              'orderby' => 'post__in'
          );
          $query = new WP_Query( $args );
          if ( $query->have_posts() ) :
            $i = 1;
        ?>     
        <div class="carousel-inner" role="listbox">
          <?php  while ( $query->have_posts() ) : $query->the_post(); ?>
          <div <?php if($i == 1){echo 'class="carousel-item active"';} else{ echo 'class="carousel-item"';}?>>
            <?php if(has_post_thumbnail()){
                  the_post_thumbnail();
              } else{?>
              <img src="<?php echo esc_url(get_template_directory_uri()); ?>/theme-block-pattern/images/banner.png" alt="" />
            <?php } ?>
            <div class="carousel-caption wow zoomIn">
              <div class="inner_carousel">
                <?php if( get_theme_mod('perfect_blog_slider_title_Show_hide',true) != ''){ ?>
                  <h1 class="text-uppercase"><a href="<?php echo esc_url( get_permalink() );?>"><?php the_title(); ?><span class="screen-reader-text"><?php the_title(); ?></span></a></h1> 
                <?php } ?>
                  <?php if( get_theme_mod('perfect_blog_slider_content_Show_hide',true) != ''){ ?>
                  <p><?php $perfect_blog_excerpt = get_the_excerpt(); echo esc_html( perfect_blog_string_limit_words( $perfect_blog_excerpt, esc_attr(get_theme_mod('perfect_blog_slider_excerpt_length','30')))); ?></p> 
                <?php } ?>
                <?php if( get_theme_mod('perfect_blog_slider_button_show_hide',true) != ''){ ?>
                <?php if( get_theme_mod('perfect_blog_slider_button','Read More') != '' || get_theme_mod('perfect_blog_slider_button_url') != ''){ ?>
                  <div class="more-btn mt-md-3">              
                    <a href="<?php echo esc_url(get_theme_mod('perfect_blog_slider_button_url')!= '') ? esc_url(get_theme_mod('perfect_blog_slider_button_url')) : esc_url(get_permalink()); ?>"><?php echo esc_html(get_theme_mod('perfect_blog_slider_button','Read More'));?><span class="screen-reader-text"><?php echo esc_html(get_theme_mod('perfect_blog_slider_button','Read More'));?></span></a>
                  </div>
                <?php } ?>   
                <?php }?>      
              </div>
            </div>
          </div>
          <?php $i++; endwhile; 
          wp_reset_postdata();?>
        </div>
        <?php else : ?>
        <div class="no-postfound"></div>
          <?php endif;
        endif;?>
        <a class="carousel-control-prev" data-bs-target="#carouselExampleCaptions" data-bs-slide="prev" role="button">
          <span class="carousel-control-prev-icon" aria-hidden="true"><i class="fas fa-chevron-left"></i></span>
          <span class="screen-reader-text"><?php esc_html_e( 'Previous','perfect-blog' );?></span>
        </a>
        <a class="carousel-control-next" data-bs-target="#carouselExampleCaptions" data-bs-slide="next" role="button">
          <span class="carousel-control-next-icon" aria-hidden="true"><i class="fas fa-chevron-right"></i></span>
          <span class="screen-reader-text"><?php esc_html_e( 'Next','perfect-blog' );?></span>
        </a>
      </div>  
      <div class="clearfix"></div>
    </section>
  <?php }?>

  <?php do_action( 'perfect_blog_after_slider' ); ?>

  <div class="container">
    <div class="row m-0 wow bounceInUp">
      <div class="col-lg-9 col-md-9 title">
        <?php if( get_theme_mod('perfect_blog_cat_title') != ''){ ?> <h2 class="text-uppercase mt-3 wow bounceInDown"><?php echo esc_html(get_theme_mod('perfect_blog_cat_title',__('THINGS YOU DONT WANNA MISS','perfect-blog'))); ?></h2>
        <hr class="titlehr m-0 wow bounceInDown">
        <?php }?>
        <div class="category-section row my-5">
          <?php 
            $perfect_blog_catData=  get_theme_mod('perfect_blog_category');
            if($perfect_blog_catData){
              $page_query = new WP_Query(array( 'category_name' => esc_html($perfect_blog_catData,'perfect-blog')));?>
              <?php while( $page_query->have_posts() ) : $page_query->the_post(); ?>
                <div class="col-lg-4 col-md-6 cat-content"> 
                  <div class="imagebox wow bounceInUp">
                    <?php if(has_post_thumbnail()) { ?><?php the_post_thumbnail(); ?><?php } ?>
                  </div>
                  <div class="contentbox w-100 my-0 mx-auto">
                    <h3><a href="<?php the_permalink(); ?>" class="text-uppercase p-3 mx-auto"><?php the_title(); ?><span class="screen-reader-text"><?php the_title(); ?></span></a></h3>
                  </div>
                </div>
              <?php endwhile;
              wp_reset_postdata();
            } 
          ?>
        </div>
        <?php while ( have_posts() ) : the_post(); ?>
          <?php the_content(); ?>
        <?php endwhile; // end of the loop. ?>
      </div>
      <div id="sidebar" class="col-lg-3 col-md-3 wow bounceInUp">
        <?php if (is_active_sidebar('home-page')) : ?>
          <?php dynamic_sidebar('home-page'); ?>
        <?php else : ?>
          <aside id="existing-posts" role="complementary" aria-label="<?php esc_attr_e( 'homepagesidebar', 'perfect-blog' ); ?>" class="widget">
            <h3 class="widget-title"><?php esc_html_e('Recent Posts', 'perfect-blog'); ?></h3>
            <ul>
              <?php
              $perfect_blog_recent_posts = new WP_Query(array(
                'post_type'      => 'post',
                'posts_per_page' => 1,
                'post_status'    => 'publish',
              ));

              if ($perfect_blog_recent_posts->have_posts()) :
              while ($perfect_blog_recent_posts->have_posts()) :
                $perfect_blog_recent_posts->the_post();
                ?>
                <li class="text-center">
                  <?php
                    if (has_post_thumbnail()) :
                  ?>
                  <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail('thumbnail'); ?>
                  </a>
                  <?php
                  endif;
                  ?>
                  <a href="<?php the_permalink(); ?>"><h4 class="post-title"><?php the_title(); ?></h4></a>
                  <p class="post-content"><?php echo wp_trim_words(get_the_content(), 20, '...'); ?></p>
                </li>
                <?php
              endwhile;
              else :
                ?>
                <li><?php esc_html_e('No posts found.', 'perfect-blog'); ?></li>
                <?php
              endif;

              wp_reset_postdata();
              ?>
            </ul>
          </aside>
        <?php endif; ?>
      </div>

    </div>
  </div>
  
  <?php do_action( 'perfect_blog_after_post_section' ); ?>

  <div class="container">
    <?php while ( have_posts() ) : the_post(); ?>
      <?php the_content(); ?>
    <?php endwhile; // end of the loop. ?>
  </div>
</main>

<?php get_footer(); ?>