<?php
/**
 * Latest Posts Slider Template Part
 *
 * @package Peblog
 */

// Get latest posts for slider based on customizer setting.
$latest_category = get_theme_mod( 'peblog_latest_category', '' );
$latest_args     = array( 'numberposts' => 6 );
if ( $latest_category ) {
	$latest_args['category_name'] = $latest_category;
}
$latest_posts = get_posts( $latest_args );
?>

<!-- latest post design start -->
<section class="latest-post" id="latest-post">
	<div class="heading-container">
		<h2 class="heading"><?php echo esc_html( get_theme_mod( 'peblog_latest_title', __( 'Latest Posts', 'peblog' ) ) ); ?></h2>
		<span class="prev-slide"></span>
		<span class="next-slide"></span>
	</div>
	<div class="swiper latest-post-slider">
		<div class="swiper-wrapper">
			<?php if ( empty( $latest_posts ) ) : ?>
				<div class="swiper-slide">
					<div class="no-posts-message">
						<div class="no-posts-icon">
							<img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/icon/document.svg' ); ?>" alt="<?php esc_attr_e( 'No Posts', 'peblog' ); ?>" />
						</div>
						<h3><?php esc_html_e( 'No Latest Posts Found', 'peblog' ); ?></h3>
						<p><?php esc_html_e( 'There are no posts to display in the latest posts section. Please add some posts or check your category settings.', 'peblog' ); ?></p>
					</div>
				</div>
			<?php else : ?>
				<?php
				foreach ( $latest_posts as $index => $latest_post ) :
					// Setup post data for template tags.
					setup_postdata( $latest_post );
					$author     = get_the_author_meta( 'display_name', $latest_post->post_author );
					$post_date  = get_the_date( 'd.m.Y', $latest_post->ID );
					$categories = get_the_category( $latest_post->ID );
					?>
				<div class="swiper-slide">
					<article class="right-article">
						<div class="related-post-card">
							<div class="card-image">
								<a href="<?php echo esc_url( get_permalink( $latest_post->ID ) ); ?>">
									<?php if ( has_post_thumbnail( $latest_post->ID ) ) : ?>
										<?php echo get_the_post_thumbnail( $latest_post->ID, 'medium', array( 'class' => 'post-thumbnail' ) ); ?>
									<?php else : ?>
										<div class="post-placeholder">
											<div class="placeholder-icon">
												<img src="<?php echo esc_url( peblog_get_placeholder_icon( $latest_post->ID ) ); ?>" alt="<?php esc_attr_e( 'Post', 'peblog' ); ?>" />
											</div>
										</div>
									<?php endif; ?>
								</a>
							</div>
							<div class="card-content">
								<ul>
									<li class="post-author-image">
										<a href="<?php echo esc_url( get_author_posts_url( $latest_post->post_author ) ); ?>">
											<?php echo get_avatar( $latest_post->post_author, 32 ); ?>
										</a>
									</li>
									<li class="post-author"><a href="<?php echo esc_url( get_author_posts_url( $latest_post->post_author ) ); ?>"><?php echo esc_html( $author ); ?></a></li>
									<li class="post-date"><span><?php echo esc_html( $post_date ); ?></span></li>
								</ul>
								<h3 class="entry-title"><?php echo esc_html( $latest_post->post_title ); ?></h3>
								<div class="entry-expert">
									<p><?php echo esc_html( wp_trim_words( $latest_post->post_content, 15, '...' ) ); ?></p>
								</div>
								<div class="entry-meta">
									<div class="entry-tag">
										<?php if ( $categories ) : ?>
											<?php
											// Dynamic color classes.
											$color_classes = array( 'tag-main', 'tag-warning', 'tag-success', 'tag-danger', 'tag-info' );
											?>
											<?php foreach ( $categories as $category ) : ?>
												<?php
												$color_index = $category->term_id % count( $color_classes );
												$color_class = $color_classes[ $color_index ];
												?>
												<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>" class="<?php echo esc_attr( $color_class ); ?>"><?php echo esc_html( $category->name ); ?></a>
											<?php endforeach; ?>
										<?php endif; ?>
									</div>
									<?php if ( function_exists( 'pvc_get_post_views' ) ) : ?>
										<div class="entry-rate">
											<a href="<?php echo esc_url( get_permalink( $latest_post->ID ) ); ?>">
												<img src="<?php echo esc_url( get_template_directory_uri() ); ?>/assets/images/icon/eye.svg" alt="<?php esc_attr_e( 'Views', 'peblog' ); ?>" />
												<?php echo esc_html( pvc_get_post_views( $latest_post->ID ) ); ?>
											</a>
										</div>
									<?php endif; ?>
								</div>
							</div>
						</div>
					</article>
				</div>
				<?php endforeach; ?>
				<?php
				// Reset post data after the loop.
				wp_reset_postdata();
				?>
			<?php endif; ?>
		</div>
	</div>
</section>
<!-- latest post design end -->
