<?php
/**
 * Template for displaying archive pages.
 *
 * Responsible for rendering posts grouped by categories, tags, authors, dates, or other taxonomies.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package Yaatra
 */

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

get_header();

/**
 * hook - yaatra_before_page_content
 * 
 * @since 1.1.0
 */
do_action( 'yaatra_before_page_content' );
?>
<div class="cv-content-wrapper">

	<?php get_sidebar( 'left' ); ?>

	<div id="primary" class="content-area">
		<main id="main" class="site-main">

			<?php
				$total_post_count = $wp_query->found_posts;
				$post_count = 1;
				$yaatra_archive_layout = yaatra_get_customizer_option_value( 'yaatra_archive_layout' );
				if ( have_posts() ) :
			?>

					<header class="page-header">
						<?php
							the_archive_title( '<h1 class="page-title">', '</h1>' );
							the_archive_description( '<div class="archive-description">', '</div>' );
						?>
					</header><!-- .page-header -->

			<?php
					/* Start the Loop */
					while ( have_posts() ) :
						the_post();
						if ( $yaatra_archive_layout == 'classic-grid' ) {
							if ( $post_count == 1 ) {
			                	echo '<div class="archive-grid-post-wrapper">';
			                } elseif ( $post_count == 5 ) {
			                	echo '<div class="archive-classic-post-wrapper">';
			                }
						}

						/*
						 * Include the Post-Type-specific template for the content.
						 * If you want to override this in a child theme, then include a file
						 * called content-___.php (where ___ is the Post Type name) and that will be used instead.
						 */
						get_template_part( 'template-parts/content', get_post_type() );

						if ( $yaatra_archive_layout == 'classic-grid' ) {
							if ( $post_count == 4 || $post_count == 5 || $post_count == $total_post_count ) {
								echo '</div>';
							}
							if ( $post_count == 5 ) { $post_count = 0; }
							$post_count++;
						}

					endwhile;

					the_posts_pagination();

				else :

					get_template_part( 'template-parts/content', 'none' );

				endif;
			?>

		</main><!-- #main -->
	</div><!-- #primary -->

    <?php get_sidebar(); ?>

</div><!-- .cv-content-wrapper -->

<?php
	/**
	 * hook - yaatra_after_page_content
	 *
	 * @since 1.1.0
	 */
	do_action( 'yaatra_after_page_content' );

	get_footer();
