<?php
/**
 * Blog posts list
 *
 * @package CWC
 * @since 1.0.0
 */

namespace CWC;

// Determine classes based on sidebar presence
if ( hasSidebar() ) {
	$classes  = 'container grid grid-cols-1 lg:grid-cols-4 gap-8 xl:gap-16 my-section mx-auto lg:px-0!';
	$clsEntry = 'lg:col-span-3';
} else {
	$classes = 'container my-section lg:my-0 mx-auto';
}

ob_start();
get_template_part( 'views/icons/prev' );
$pagination_prev_icon = ob_get_clean();

ob_start();
get_template_part( 'views/icons/next' );
$pagination_next_icon = ob_get_clean();

get_header();
?>

<section class="<?php echo esc_attr( $classes ); ?>">
	<?php if ( have_posts() ) : ?>
		<div class="blog-posts <?php echo esc_attr( $clsEntry ); ?>">
			<div class="post-list">
				<div class="post-list__posts grid grid-cols-[repeat(auto-fit,minmax(20rem,1fr))] gap-12">
					<?php
					while ( have_posts() ) :
						the_post();
						?>
						<div class="post-list__post flex flex-col rounded-md shadow-lg hover:prose-img:scale-110 hover:prose-img:origin-center hover:prose-img:duration-500">
							<figure class="post-list__img aspect-video rounded-t-md block h-auto w-full overflow-hidden m-0! p-0!">
								<?php if ( has_post_thumbnail() ) : ?>
									<?php
										$featImg = get_the_post_thumbnail_url();
										$postImg = $featImg ? $featImg : 'https://picsum.photos/600/400?random=' . get_the_ID();
										$imgAlt  = get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ) ?? get_the_title();
									?>

									<img class="block h-full object-cover transition-transform duration-300 ease-linear w-full will-change-transform" src="<?php echo esc_url( $postImg ); ?>" alt="<?php echo esc_attr( $imgAlt ); ?>">
								<?php endif; ?>
							</figure>

							<div class="post-list__details p-8 pb-10 flex flex-col justify-between grow">
                                <h2 class="post-list__title font-normal text-32px mt-0 mx-0">
                                    <a href="<?php the_permalink(); ?>" class=""><?php the_title(); ?></a>
                                </h2>

                                <div class="post-list__excerpt line-clamp-5 mb-4">
                                    <?php the_excerpt(); ?>
                                </div>

                                <?php /* translators: %s: Post title. */ ?>
                                <a class="post-list__read-more border-b-2 border-b-secondary w-fit" href="<?php echo esc_url( $post_permalink ); ?>" aria-label="<?php echo esc_attr( sprintf( __( 'Continue reading %s', 'cwc' ), $post_title ) ); ?>">
                                    <?php echo esc_html__( 'Read More', 'cwc' ); ?>
                                </a>
							</div>
						</div>
					<?php endwhile; ?>
				</div>

				<div class="post-list__pagination">
					<?php
					the_posts_pagination(
						array(
							'mid_size'  => 2,
							'prev_text' => $pagination_prev_icon . '<span class="sr-only">' . esc_html__( 'Previous', 'cwc' ) . '</span>',
							'next_text' => $pagination_next_icon . '<span class="sr-only">' . esc_html__( 'Next', 'cwc' ) . '</span>',
						)
					);
					?>
				</div>
			</div>
		</div>
	<?php else : ?>
		<div class="blog-posts <?php echo esc_attr( $clsEntry ); ?>">
			<div class="no-posts">
				<h2 class="no-posts__title">Nothing here yet&hellip;</h2>
				<p class="no-posts__desc">No published posts found.</p>
			</div>
		</div>
	<?php endif; ?>

	<?php if ( hasSidebar() ) : ?>
		<?php get_sidebar(); ?>
	<?php endif; ?>
</section>

<?php get_footer(); ?>
