<?php
if ( ! get_theme_mod( 'neo_magazine_enable_banner_section', false ) ) {
	return;
}

$main_news_content_ids = array();
$main_content_type     = get_theme_mod( 'neo_magazine_main_news_content_type', 'post' );

if ( $main_content_type === 'post' ) {
	for ( $i = 1; $i <= 5; $i++ ) {
		$main_news_content_ids[] = get_theme_mod( 'neo_magazine_main_news_content_post_' . $i );
	}
	$main_news_args = array(
		'post_type'           => 'post',
		'post__in'            => array_filter( $main_news_content_ids ),
		'orderby'             => 'post__in',
		'posts_per_page'      => absint( 5 ),
		'ignore_sticky_posts' => true,
	);
	if ( ! empty( array_filter( $main_news_content_ids ) ) ) {
		$main_news_args['post__in'] = array_filter( $main_news_content_ids );
		$main_news_args['orderby']  = 'post__in';
	} else {
		$main_news_args['orderby'] = 'date';
	}
} else {
	$cat_content_id = get_theme_mod( 'neo_magazine_main_news_content_category' );
	$main_news_args = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( 5 ),
	);
}
$main_news_args = apply_filters( 'neo_magazine_banner_section_args', $main_news_args );

neo_magazine_render_banner_section( $main_news_args );

/**
 * Render Banner Section.
 */
function neo_magazine_render_banner_section( $main_news_args ) {
	$main_news_query = new WP_Query( $main_news_args );
	if ( $main_news_query->have_posts() ) {
		$banner_style = get_theme_mod( 'neo_magazine_banner_section_style', 'banner-style-1' );
		?>

		<div id="neo_magazine_banner_section"  class="magazine-banner section-splitter banner-style-1">
			<?php
			if ( is_customize_preview() ) :
				neo_magazine_section_link( 'neo_magazine_banner_section' );
			endif;
			?>
			<div class="section-wrapper">
				<div class="banner-container-wrapper">
					<div class="banner-main-part">
						<div class="banner-main-wrap">
							<?php
							while ( $main_news_query->have_posts() ) :
								$main_news_query->the_post();
								?>
								<div class="blog-post-container tile-layout">
									<div class="blog-post-inner">
										<?php if ( has_post_thumbnail() ) { ?>
											<div class="blog-post-image">
												<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'full' ); ?></a>
											</div>
										<?php } ?>
										<div class="blog-post-detail">
											<?php neo_magazine_categories_list(); ?>
											<h2 class="entry-title">
												<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
											</h2>
											<div class="post-meta">
												<?php
												neo_magazine_posted_by();
												neo_magazine_posted_on();
												?>
											</div>
										</div>
									</div>
								</div>
								<?php
							endwhile;
							wp_reset_postdata();
							?>
						</div>
					</div>
				</div>
			</div>
		</div>

		<?php
	}

}
