<?php
/**
 * Single news/blog post template.
 *
 * @package Northfield_Theatre
 * @since 1.0.0
 */

get_header();

if ( have_posts() ) :
	while ( have_posts() ) :
		the_post();
		$cats = get_the_category();
		$cat  = ! empty( $cats ) ? $cats[0] : null;
?>

<article class="news-post">

	<?php $post_image = northfield_get_post_illustration_url( get_the_ID() ); ?>
	<header class="news-post__hero section--dark<?php echo $post_image ? ' news-post__hero--has-image' : ''; ?>"
	        <?php if ( $post_image ) : ?>style="background-image: url('<?php echo esc_url( $post_image ); ?>')"<?php endif; ?>>
		<div class="container news-post__hero-inner">
			<nav class="news-post__breadcrumb" aria-label="<?php esc_attr_e( 'Breadcrumb', 'northfield-theatre' ); ?>">
				<a href="<?php echo esc_url( home_url( '/news/' ) ); ?>">
					← <?php esc_html_e( 'All news', 'northfield-theatre' ); ?>
				</a>
			</nav>
			<?php if ( $cat ) : ?>
				<p class="news-post__category"><?php echo esc_html( $cat->name ); ?></p>
			<?php endif; ?>
			<h1 class="news-post__title"><?php the_title(); ?></h1>
			<div class="news-post__meta">
				<time datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>">
					<?php echo esc_html( get_the_date( 'j F Y' ) ); ?>
				</time>
				<?php
				$reading_time = max( 1, (int) ceil( str_word_count( strip_tags( get_the_content() ) ) / 220 ) );
				?>
				<span class="news-post__dot" aria-hidden="true">·</span>
				<span><?php printf( esc_html__( '%d min read', 'northfield-theatre' ), $reading_time ); ?></span>
			</div>
		</div>
	</header>

	<div class="news-post__body section">
		<div class="container container--narrow">
			<div class="entry-content news-post__content">
				<?php the_content(); ?>
			</div>

			<footer class="news-post__footer">
				<?php
				// Tags.
				$tags = get_the_tags();
				if ( $tags ) :
				?>
					<ul class="news-post__tags" role="list">
						<?php foreach ( $tags as $tag ) : ?>
							<li><a href="<?php echo esc_url( get_tag_link( $tag ) ); ?>"><?php echo esc_html( $tag->name ); ?></a></li>
						<?php endforeach; ?>
					</ul>
				<?php endif; ?>

				<a href="<?php echo esc_url( home_url( '/news/' ) ); ?>" class="btn btn--outline btn--sm">
					← <?php esc_html_e( 'Back to all news', 'northfield-theatre' ); ?>
				</a>
			</footer>
		</div>
	</div>

	<?php
	// Related posts (same category).
	if ( $cat ) :
		$related = new WP_Query( array(
			'post_type'      => 'post',
			'posts_per_page' => 3,
			'post__not_in'   => array( get_the_ID() ),
			'cat'            => $cat->term_id,
			'orderby'        => 'date',
			'order'          => 'DESC',
		) );

		if ( $related->have_posts() ) :
	?>
	<section class="news-related section section--cream">
		<div class="container">
			<h2 class="news-related__heading"><?php esc_html_e( 'More from the theatre', 'northfield-theatre' ); ?></h2>
			<div class="news-related__grid">
				<?php while ( $related->have_posts() ) : $related->the_post();
					$r_cat = get_the_category();
					$r_cat_name = ! empty( $r_cat ) ? $r_cat[0]->name : '';
				?>
					<article class="news-card">
						<a href="<?php the_permalink(); ?>" class="news-card__link">
							<?php if ( $r_cat_name ) : ?>
								<span class="news-card__category"><?php echo esc_html( $r_cat_name ); ?></span>
							<?php endif; ?>
							<h3 class="news-card__title"><?php the_title(); ?></h3>
							<p class="news-card__excerpt"><?php echo esc_html( get_the_excerpt() ); ?></p>
							<div class="news-card__meta">
								<time datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>">
									<?php echo esc_html( get_the_date( 'j F Y' ) ); ?>
								</time>
								<span class="news-card__arrow" aria-hidden="true">→</span>
							</div>
						</a>
					</article>
				<?php endwhile; wp_reset_postdata(); ?>
			</div>
		</div>
	</section>
	<?php
		endif;
	endif;
	?>

</article>

<?php
	endwhile;
endif;

get_footer();
