<?php
/**
 * The template for displaying archive pages.
 *
 * 
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

$globalFields = get_fields('option');
$headingBlog = $globalFields['heading_blog'];
?>

<div class="archive-wrapper">
	<div class="container">
        <div class="archive-head">
            <?php if ( is_home(  ) && !is_front_page() ) {
                ?>
                <?php if (!empty($headingBlog)) { ?>
                    <h2 class="heading-section"><?php echo $headingBlog; ?></h2>
                <?php } ?>
                <?php
            } else if ( is_archive(  ) ) {
                the_archive_title( '<h2 class="heading-section">', '</h2>' );
            } else if( is_search() ){
                ?>
                <h2 class="heading-section">
                    <?php echo esc_html__( 'Search results for: ', 'hello-thedavanigroup' ); ?>
                    <span><?php echo get_search_query(); ?></span>
                </h2>
                <?php
            } ?>
        </div>
        <div class="archive-main">
			<?php if (have_posts()) { ?>
            <div class="posts">
                <?php
                while ( have_posts() ) {
                    the_post();
                    $post_link = get_permalink();

                    $excerpt = get_the_excerpt();
                    $excerpt_length = (int) _x( '28', 'excerpt_length' );
                    $excerpt_length = (int) apply_filters( 'excerpt_length', $excerpt_length );
                    $excerpt_more = apply_filters( 'excerpt_more', '...' );
                    $post_excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
                    ?>
                    <article <?php post_class( 'post-item' ) ?>>
                        <?php if ( has_post_thumbnail() ): ?>
                        <div class="post-item-thumb">
                            <a class="post-item-link" href="<?php echo esc_url( $post_link ); ?>">
                                <?php echo get_the_post_thumbnail( $post, 'large' ); ?>
                            </a>
                        </div>
                        <?php endif; ?>
                        <div class="post-item-date"><?php echo get_the_date( 'j F Y' ); ?></div>
                        <h3 class="post-item-title">
                            <a class="post-item-link" href="<?php echo esc_url( $post_link ); ?>">
                                <?php echo wp_kses_post( get_the_title() ); ?>
                            </a>
                        </h3>
                        <div class="post-item-excerpt"><?php echo $post_excerpt; ?></div>
                    </article>
                <?php } ?>
            </div>
			<?php
            }else{
                ?>
                <p><?php echo esc_html_x( 'Sorry, but nothing was found. Please try a search with different keywords.', 'Message explaining that there are no results returned from a search.', 'hello-thedavanigroup' ); ?></p>
                <?php
            }
            ?>
            <?php
            global $wp_query;
            if ( $wp_query->max_num_pages > 1 ) :
                $prev_arrow = is_rtl() ? '&rarr;' : '&larr;';
                $next_arrow = is_rtl() ? '&larr;' : '&rarr;';
                echo get_the_posts_pagination( array(
                    'mid_size'  => 2,
                    'prev_text' => __( $prev_arrow, 'hello-thedavanigroup' ),
                    'next_text' => __( $next_arrow, 'hello-thedavanigroup' ),
                ) );
                ?>
            <?php endif; ?>
			<div class="blog-sidebar">
				<div class="recent-post">
					<div class="heading">
						<h3>Recent Posts</h3>
					</div>
				<?php
				$recent_posts = new WP_Query([
					'posts_per_page' => -1, 
					'post_status' => 'publish',
					'order'          => 'ASC',
				]);

				if ($recent_posts->have_posts()) : ?>
					<ul class="recent-posts">
						<?php while ($recent_posts->have_posts()) : $recent_posts->the_post(); ?>
							<li>
								<a href="<?php the_permalink(); ?>">
									<?php if (has_post_thumbnail()) : ?>
										<img src="<?php the_post_thumbnail_url('thumbnail'); ?>" alt="<?php the_title(); ?>">
									<?php endif; ?>
									<h6><?php the_title(); ?></h6>
								</a>
							</li>
						<?php endwhile; ?>
					</ul>
					<?php wp_reset_postdata(); ?>
				<?php endif; ?>
				</div>
			</div>
			
        </div>

	</div>
</div>
