<?php
/**
 * Template part for displaying front page introduction.
 *
 * @package News Articles
 */

// Banner Section.
$banner_section = get_theme_mod( 'news_articles_banner_section_enable', false );

if ( false === $banner_section ) {
	return;
}

$slider_content_ids  = array();
$banner_content_type = get_theme_mod( 'news_articles_banner_slider_content_type', 'post' );

if ( $banner_content_type === 'post' ) {

	for ( $i = 1; $i <= 3; $i++ ) {
		$slider_content_ids[] = get_theme_mod( 'news_articles_banner_slider_post_' . $i );
	}

	$banner_args = array(
		'post_type'           => 'post',
		'posts_per_page'      => absint( 3 ),
		'ignore_sticky_posts' => true,
	);
	if ( ! empty( array_filter( $slider_content_ids ) ) ) {
		$banner_args['post__in'] = array_filter( $slider_content_ids );
		$banner_args['orderby']  = 'post__in';
	} else {
		$banner_args['orderby'] = 'date';
	}

} else {
	$cat_content_id = get_theme_mod( 'news_articles_banner_slider_category' );
	$banner_args    = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( 3 ),
	);
}

?>

<div id="news_articles_banner_section" class="main-banner-section style-2 adore-navigation">
	<div class="theme-wrapper">
		<div class="main-banner-section-wrapper">

			<!-- banner slider -->
			<div class="banner-slider-outer">
				<div class="banner-slider">
					<?php
					$banner_query = new WP_Query( $banner_args );
					if ( $banner_query->have_posts() ) {
						while ( $banner_query->have_posts() ) :
							$banner_query->the_post();
							?>
							<div class="post-item-outer">
								<div class="post-item overlay-post slider-item" style="background-image: url('<?php echo esc_url( get_the_post_thumbnail_url( get_the_ID(), 'full' ) ); ?>');">
									<div class="blog-banner">
										<div class="post-overlay">
											<div class="post-item-content">
												<div class="entry-cat">
													<?php the_category( '', '', get_the_ID() ); ?>
												</div>
												<h2 class="entry-title">
													<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
												</h2>
												<ul class="entry-meta">
													<li class="post-author"> <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>"><span class="far fa-user"></span><?php echo esc_html( get_the_author() ); ?></a></li>
													<li class="post-date"> <span class="far fa-calendar-alt"></span><?php echo esc_html( get_the_date() ); ?></li>
													<li class="post-comment"><span class="far fa-comment"></span><?php echo absint( get_comments_number( get_the_ID() ) ); ?></li>
												</ul>
											</div>   
										</div>
									</div>
								</div>
							</div>
							<?php
						endwhile;
						wp_reset_postdata();
					}
					?>
				</div>
			</div>
			<!-- end banner slider -->

			<?php if ( get_theme_mod( 'news_articles_random_news_tab_enable', true ) === true ) { ?>
				<div class="post-tabs-wrapper">
					<div class="post-tabs-head">
						<ul class="post-tabs">
								<li><i class="fas fa-random"></i><?php echo esc_html( 'Random News', 'news-articles' ); ?></li>
						</ul>
					</div>
					<div class="post-tab-content-wrapper">
							<div class="post-tab-container" id="tab-3">
								<?php
								$random_posts_args = array(
									'posts_per_page'      => 5,
									'post_type'           => 'post',
									'ignore_sticky_posts' => true,
									'orderby'             => 'rand',
								);
								$random_query = new WP_Query( $random_posts_args );
								if ( $random_query->have_posts() ) {
									while ( $random_query->have_posts() ) :
										$random_query->the_post();
										?>

										<div class="post-item post-list">
											<?php if ( has_post_thumbnail() ): ?>
												<div class="post-item-image">
													<?php the_post_thumbnail( 'thumbnail' ); ?>
												</div>
											<?php endif ?>
											<div class="post-item-content">
												<h3 class="entry-title">
													<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
												</h3>  
												<ul class="entry-meta">
													<li class="post-author"> <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>"><span class="far fa-user"></span><?php echo esc_html( get_the_author() ); ?></a></li>
													<li class="post-date"> <span class="far fa-calendar-alt"></span><?php echo esc_html( get_the_date() ); ?></li>
													<li class="post-comment"><span class="far fa-comment"></span><?php echo absint( get_comments_number( get_the_ID() ) ); ?></li>
												</ul>
											</div>
										</div>
										<?php
									endwhile;
									wp_reset_postdata();
								}
								?>
							</div>
					</div>
				</div>

			<?php } ?>

		</div>
	</div>
</div>
