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

<main id="content">
  <div class="container">
    <?php if( get_option('ovation_health_blog_post_slider_show_hide', false) !== 'off'){ ?>
      <section id="slider">
        <div id="carouselExampleAutoplaying" class="carousel slide" data-bs-ride="carousel">
          <?php $ovation_blog_slider_count = get_theme_mod('ovation_blog_slider_count');
              for ( $i = 1; $i <= $ovation_blog_slider_count; $i++ ) {
              $mod =  get_theme_mod( 'ovation_blog_post_setting' . $i );
              if ( 'page-none-selected' != $mod ) {
                $slide_pages[] = $mod;
              }
            }
             if( !empty($slide_pages) ) :
            $args = array(
              'post_type' =>array('post','page'),
              'post__in' => $slide_pages,
              'ignore_sticky_posts'  => true, // Exclude sticky posts by default
            );

            // Check if specific posts are selected
            if (empty($slide_pages) && is_sticky()) {
                $args['post__in'] = get_option('sticky_posts');
            }
              
            $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()){ ?>
                <img src="<?php the_post_thumbnail_url('full'); ?>"/>
              <?php }else{?>
              <img src="<?php echo esc_url(get_template_directory_uri()); ?>/assets/images/header-img-2.jpg" alt="" />
            <?php } ?>
              <div class="carousel-caption">
                <div class="inner_carousel">
                  <h2><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
                  <div class="date-box mt-4">
                    <?php if( get_option('ovation_health_blog_front_date',false) != 'off'){ ?>
                    <span class="me-3"><i class="far fa-calendar-alt me-2"></i><?php the_time( get_option( 'date_format' ) ); ?></span>
                    <?php }?>
                    <?php if( get_option('ovation_health_blog_front_comment',false) != 'off'){ ?>
                    <span class="entry-comments"><i class="fas fa-comments me-2"></i> <?php comments_number( __('0 Comments','ovation-health-blog'), __('0 Comments','ovation-health-blog'), __('% Comments','ovation-health-blog')); ?></span>
                    <?php }?>
                  </div>
                </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" type="button" data-bs-target="#carouselExampleAutoplaying" data-bs-slide="prev">
              <span class="carousel-control-prev-icon" aria-hidden="true"><i class="fas fa-arrow-left"></i></span>
            </a>
            <a class="carousel-control-next" type="button" data-bs-target="#carouselExampleAutoplaying" data-bs-slide="next">
              <span class="carousel-control-next-icon" aria-hidden="true"><i class="fas fa-arrow-right"></i></span>
            </a>
        </div>
        <div class="clearfix"></div>
      </section>
    <?php }?>

    <?php if( get_option('ovation_blog_top_category_show_hide', false) !== 'off'){ ?>
      <section id="featured_post" class="py-5">
        <div class="row">
          <?php
          $catData1=  get_theme_mod('ovation_blog_top_category_setting');
          $ovation_blog_top_category = get_theme_mod('ovation_blog_top_category_type','ascending');
          if($catData1){
            $ovation_blog_args = array( 

              'category_name' => esc_html($catData1 ,'ovation-health-blog'),

              'posts_per_page' => get_theme_mod('ovation_blog_top_post_count'),

              'order' => 'ASC',

            );
            // Adjust ordering based on user selection
            if ($ovation_blog_top_category == 'descending') {
              $ovation_blog_args['order'] = 'DESC';
            } else if ($ovation_blog_top_category == 'a-to-z') {
              $ovation_blog_args['orderby'] = 'title';
              $ovation_blog_args['order'] = 'ASC';
            } else if ($ovation_blog_top_category == 'z-to-a') {
              $ovation_blog_args['orderby'] = 'title';
              $ovation_blog_args['order'] = 'DESC';
            }
          $page_query = new WP_Query($ovation_blog_args);?>
            <?php while( $page_query->have_posts() ) : $page_query->the_post(); ?>
              <div class="col-lg-4 col-md-4 col-sm-6 wow zoomIn" data-wow-duration="2s">
                <div class="box mb-4">
                  <?php if(has_post_thumbnail()){ ?>
                    <?php the_post_thumbnail(); ?>
                  <?php }?>
                  <div class="box-content">
                    <h4><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h4>
                  </div>
                </div>
              </div>
            <?php endwhile;
            wp_reset_postdata();
          }?>
        </div>
      </section>
    <?php }?>
    <section id="custom-page-content" <?php if ( have_posts() && trim( get_the_content() ) !== '' ) echo 'class="pt-3"'; ?>>
      <?php while ( have_posts() ) : the_post(); ?>
        <?php the_content(); ?>
      <?php endwhile; ?>
  </section>
  </div>
</main>

<?php get_footer(); ?>
