<?php
/**
 * Basic post list template
 *
 * @package Multi
 */

get_header();
get_template_part( 'template-parts/top-slider' );
get_template_part( 'template-parts/content-start' );

$multimagazine_load_posts = multimagazine_get_post_loading_classes();
$multimagazine_post_args  = multimagazine_postlist_meta_flags();
$multimagazine_layout     = absint( get_theme_mod( 'multi_post_list_layout', MULTIMAGAZINE_POST_LIST_LAYOUT ) );

?>
<main class="main">
	<?php
	if ( 1 === $multimagazine_layout ) : // Full width image.
		?>
		<div class="postlist postlist-full-width <?php echo esc_attr( $multimagazine_load_posts ); ?>">
		<?php
		if ( have_posts() ) {
			while ( have_posts() ) {
				the_post();
				get_template_part( 'post-list-parts/post-full-width-image', null, $multimagazine_post_args );
			}
		}
		?>
	</div>

<?php elseif ( 2 === $multimagazine_layout ) : // Post list with medium images. ?>
	<div class="postlist postlist-medium-image">
		<?php if ( ! empty( $multimagazine_load_posts ) ) : ?>
			<div class="<?php echo esc_attr( $multimagazine_load_posts ); ?>">
		<?php endif; ?>
		<?php
		if ( have_posts() ) {
			while ( have_posts() ) {
				the_post();
				get_template_part( 'post-list-parts/post-medium-image', null, $multimagazine_post_args );
			}
		}
		?>
		<?php if ( ! empty( $multimagazine_load_posts ) ) : ?>
			</div>
		<?php endif; ?>
		<?php get_template_part( 'template-parts/pager' ); ?>
	</div>

<?php elseif ( 3 === $multimagazine_layout ) : // Post list with exposed date. ?>
	<div class="postlist postlist-exposed-date">
		<?php if ( ! empty( $multimagazine_load_posts ) ) : ?>
			<div class="<?php echo esc_attr( $multimagazine_load_posts ); ?>">
		<?php endif; ?>
		<?php
		if ( have_posts() ) {
			while ( have_posts() ) {
				the_post();
				get_template_part( 'post-list-parts/post-exposed-date', null, $multimagazine_post_args );
			}
		}
		?>
		<?php if ( ! empty( $multimagazine_load_posts ) ) : ?>
			</div>
		<?php endif; ?>
		<?php get_template_part( 'template-parts/pager' ); ?>
	</div>

<?php elseif ( in_array( $multimagazine_layout, array( 4, 5 ), true ) ) : // 3 columns || 2 columns ?>
		<?php
		$layout_classes = 'postlist postlist-columns index ' . $multimagazine_load_posts;
		switch ( $multimagazine_layout ) {
			case 4:
				$layout_classes .= ' columns-3';
				break;
			case 5:
				$layout_classes .= ' columns-2';
				break;
		}
		?>
	<div class="<?php echo esc_attr( $layout_classes ); ?>">
	<?php
	if ( have_posts() ) {
		while ( have_posts() ) {
			the_post();
			get_template_part( 'post-list-parts/post-column', null, $multimagazine_post_args );
		}
	}
	?>
	</div>

<?php elseif ( 6 === $multimagazine_layout ) : // Full width masonry. ?>
	<?php get_template_part( 'post-list-parts/cat-filters' ); ?>
	<div class="postlist postlist-masonry masonry m4 <?php echo esc_attr( $multimagazine_load_posts ); ?>">
		<?php
		if ( have_posts() ) {
			while ( have_posts() ) {
				the_post();
				get_template_part( 'post-list-parts/post-masonry', null, $multimagazine_post_args );
			}
		}
		?>
	</div>

<?php elseif ( 7 === $multimagazine_layout ) : // Masonry with sidebar. ?>
	<div class="postlist postlist-masonry masonry m2 <?php echo esc_attr( $multimagazine_load_posts ); ?>">
	<?php
	if ( have_posts() ) {
		while ( have_posts() ) {
			the_post();
			get_template_part( 'post-list-parts/post-masonry', null, $multimagazine_post_args );
		}
	}
	?>
	</div>
	<?php get_template_part( 'template-parts/pager' ); ?>

<?php elseif ( 8 === $multimagazine_layout ) : // 3-column masonry. ?>
	<?php get_template_part( 'post-list-parts/cat-filters' ); ?>
	<div class="postlist postlist-masonry masonry m3 <?php echo esc_attr( $multimagazine_load_posts ); ?>">
		<?php
		if ( have_posts() ) {
			while ( have_posts() ) {
				the_post();
				get_template_part( 'post-list-parts/post-masonry', null, $multimagazine_post_args );
			}
		}
		?>
	</div>

<?php elseif ( in_array( $multimagazine_layout, array( 9, 10, 11 ), true ) ) : // Modern: 2 columns || 3 columns || 1 column. ?>
	<div class="postlist modern columns <?php echo esc_attr( $multimagazine_load_posts ); ?>">
		<?php
		if ( have_posts() ) {
			switch ( $multimagazine_layout ) {
				case 9:
					$multimagazine_post_args['classes']        = 'col2';
					$multimagazine_post_args['thumbnail-size'] = 'multimagazine-thumbnail-medium';
					break;
				case 10:
					$multimagazine_post_args['classes']        = 'col3';
					$multimagazine_post_args['thumbnail-size'] = 'multimagazine-thumbnail-medium';
					break;
				case 11:
					$multimagazine_post_args['classes']        = 'col1';
					$multimagazine_post_args['thumbnail-size'] = 'multimagazine-thumbnail-large';
					break;
			}

			while ( have_posts() ) {
				the_post();
				get_template_part( 'post-list-parts/post-modern', null, $multimagazine_post_args );
			}
		}
		?>
		</div>

<?php elseif ( 13 === $multimagazine_layout ) : // Modern: mix. ?>
	<div class="postlist columns modern <?php echo esc_attr( $multimagazine_load_posts ); ?>">
		<?php
		if ( have_posts() ) {
			$multimagazine_post_number = 0;
			while ( have_posts() ) {
				the_post();

				if ( 0 === $multimagazine_post_number && $paged < 2 ) {
					$multimagazine_post_args['classes']        = 'col1';
					$multimagazine_post_args['thumbnail-size'] = 'multimagazine-thumbnail-large';
				} elseif ( $multimagazine_post_number < 3 && $paged < 2 ) {
					$multimagazine_post_args['classes']        = 'col2';
					$multimagazine_post_args['thumbnail-size'] = 'multimagazine-thumbnail-medium';
				} else {
					$multimagazine_post_args['classes']        = 'col3';
					$multimagazine_post_args['thumbnail-size'] = 'multimagazine-thumbnail-medium';
				}
				$multimagazine_post_number ++;

				get_template_part( 'post-list-parts/post-modern', null, $multimagazine_post_args );
			}
		}
		?>
	</div>
<?php elseif ( 14 === $multimagazine_layout ) : // fresh mix. ?>
	<div class="postlist fresh columns <?php echo esc_attr( $multimagazine_load_posts ); ?>">
	<?php
	if ( have_posts() ) {
		$multimagazine_post_number = 0;

		while ( have_posts() ) {
			the_post();

			if ( 0 === $multimagazine_post_number ) {
				$multimagazine_post_args['classes']        = 'col1';
				$multimagazine_post_args['thumbnail-size'] = 'multimagazine-thumbnail-large';
			} elseif ( $multimagazine_post_number < 3 ) {
				$multimagazine_post_args['classes']        = 'col2';
				$multimagazine_post_args['thumbnail-size'] = 'multimagazine-thumbnail-medium';
			} elseif ( $multimagazine_post_number < 6 ) {
				$multimagazine_post_args['classes']        = 'col3';
				$multimagazine_post_args['thumbnail-size'] = 'multimagazine-thumbnail-medium';
			}

			$multimagazine_post_number ++;

			if ( $multimagazine_post_number <= 6 ) {
				get_template_part( 'post-list-parts/post-fresh', null, $multimagazine_post_args );
			} else {
				get_template_part( 'post-list-parts/post-medium-image', null, $multimagazine_post_args );
			}
		}
	}
	?>
	</div>

<?php elseif ( 16 === $multimagazine_layout ) : // trendy. ?>
	<div class="postlist columns modern trendy <?php echo esc_attr( $multimagazine_load_posts ); ?>">
		<?php
		if ( have_posts() ) {
			$multimagazine_post_number = 0;

			while ( have_posts() ) {
				the_post();

				if ( 0 === $multimagazine_post_number && $paged < 2 ) {
					$multimagazine_post_args['classes']        = 'col1';
					$multimagazine_post_args['thumbnail-size'] = 'multimagazine-thumbnail-large';
				} elseif ( $multimagazine_post_number < 3 && $paged < 2 ) {
					$multimagazine_post_args['classes']        = 'col2';
					$multimagazine_post_args['thumbnail-size'] = 'multimagazine-thumbnail-medium';
				} else {
					$multimagazine_post_args['classes']        = 'col3';
					$multimagazine_post_args['thumbnail-size'] = 'multimagazine-thumbnail-medium';
				}
				$multimagazine_post_number ++;

				get_template_part( 'post-list-parts/post-trendy', null, $multimagazine_post_args );
			}
		}
		?>
	</div>

<?php else : // Default: large image. ?>
	<div class="postlist postlist-large-image">
		<?php if ( ! empty( $multimagazine_load_posts ) ) : ?>
			<div class="<?php echo esc_attr( $multimagazine_load_posts ); ?>">
		<?php endif; ?>
		<?php
		if ( have_posts() ) {
			while ( have_posts() ) {
				the_post();
				get_template_part( 'post-list-parts/post-large-image', null, $multimagazine_post_args );
			}
		}
		?>
		<?php if ( ! empty( $multimagazine_load_posts ) ) : ?>
			</div>
		<?php endif; ?>
		<?php get_template_part( 'template-parts/pager' ); ?>
	</div>

<?php endif; ?>

<?php
$no_sidebar = in_array( $multimagazine_layout, array( 1, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16 ), true );

if ( $no_sidebar ) {
	get_template_part( 'template-parts/pager' );
	?>
	</main>
	<?php
} else {
	?>
	</main>
	<?php
	$multimagazine_sidebar_position = $multimagazine_sidebar_pos_global;
	if ( 'off' !== $multimagazine_sidebar_position ) {
		get_sidebar();
	}
}
?>

<?php get_footer(); ?>
