<?php
/**
 * Template part for displaying front page introduction.
 *
 * @package Translucent Blog
 */

// List Posts Section.
$list_posts_section = get_theme_mod( 'translucent_blog_list_posts_section_enable', false );

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

$content_ids = array();

$list_posts_content_type = get_theme_mod( 'translucent_blog_list_posts_content_type', 'post' );

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

	for ( $i = 1; $i <= 4; $i++ ) {
		$content_ids[] = get_theme_mod( 'translucent_blog_list_posts_post_' . $i );
	}

	$args = array(
		'post_type'           => 'post',
		'post__in'            => array_filter( $content_ids ),
		'orderby'             => 'post__in',
		'posts_per_page'      => absint( 4 ),
		'ignore_sticky_posts' => true,
	);

} else {
	$cat_content_id = get_theme_mod( 'translucent_blog_list_posts_category' );
	$args           = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( 4 ),
	);
}

$query = new WP_Query( $args );
if ( $query->have_posts() ) {

	$section_title    = get_theme_mod( 'translucent_blog_list_posts_title', __( 'List Posts', 'translucent-blog' ) );
	$section_subtitle = get_theme_mod( 'translucent_blog_list_posts_subtitle', '' );
	$viewall_btn      = get_theme_mod( 'translucent_blog_list_posts_view_all_button_label', __( 'View All', 'translucent-blog' ) );
	$viewall_btn_link = get_theme_mod( 'translucent_blog_list_posts_view_all_button_url', '#' );
	?>

	<div id="translucent_blog_list_posts_section" class="frontpage list-post">
		<div class="theme-wrapper">
			<?php if ( ! empty( $section_title || $section_subtitle || $viewall_btn ) ) : ?>
				<div class="section-head">
					<div class="section-header">
						<h3 class="section-title"><?php echo esc_html( $section_title ); ?></h3>
						<p class="section-subtitle"><?php echo esc_html( $section_subtitle ); ?></p>
					</div>
					<?php if ( ! empty( $viewall_btn ) ) { ?>
						<a href="<?php echo esc_url( $viewall_btn_link ); ?>" class="adore-view-all"><?php echo esc_html( $viewall_btn ); ?></a>
					<?php } ?>
				</div>
			<?php endif; ?>
			<div class="list-post-wrapper">
				<?php
				while ( $query->have_posts() ) :
					$query->the_post();
					?>
					<div class="post-item post-list">
						<?php if ( has_post_thumbnail() ) : ?>
							<div class="post-item-image">
								<a href="<?php the_permalink(); ?>">
									<?php the_post_thumbnail( 'post-thumbnail' ); ?>
								</a>
							</div>
						<?php endif; ?>
						<div class="post-item-content">
							<div class="entry-cat">
								<?php the_category( '', '', get_the_ID() ); ?>
							</div>
							<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' ) ) ); ?>"><i class="far fa-user"></i><?php echo esc_html( get_the_author() ); ?></a></li>
								<li class="post-date"><i class="far fa-calendar-alt"></i><?php echo esc_html( get_the_date() ); ?></li>
								<li class="reading-time"><i class="far fa-clock"></i>
								<?php
								echo translucent_blog_time_interval( get_the_content() );
								echo esc_html__( ' min read', 'translucent-blog' );
								?>
								</li>
								<li class="comment"><i class="far fa-comment"></i><?php echo absint( get_comments_number( get_the_ID() ) ); ?></li>
							</ul>
						</div>
					</div>
					<?php
				endwhile;
				wp_reset_postdata();
				?>
			</div>
		</div>
	</div>

	<?php
}
