<?php
/**
 * Search results template.
 *
 * @package Northfield_Theatre
 * @since 1.0.0
 */

get_header();
?>

<div class="search-results container">
	<header class="page-header">
		<h1 class="page-header__title">
			<?php
			printf(
				/* translators: %s: search query */
				esc_html__( 'Search results for: "%s"', 'northfield-theatre' ),
				get_search_query()
			);
			?>
		</h1>
		<p class="page-header__subtitle">
			<?php
			global $wp_query;
			printf(
				/* translators: %d: number of results */
				esc_html( _n( '%d result found', '%d results found', $wp_query->found_posts, 'northfield-theatre' ) ),
				$wp_query->found_posts
			);
			?>
		</p>
	</header>

	<?php if ( have_posts() ) : ?>
		<div class="search-results__list">
			<?php while ( have_posts() ) : the_post(); ?>
				<?php
				// If it's an event post, use event card.
				if ( 'nt_event' === get_post_type() ) :
					$json_id = get_post_meta( get_the_ID(), '_nt_event_json_id', true );
					$event = $json_id ? northfield_get_event( $json_id ) : null;
					if ( $event ) :
						get_template_part( 'template-parts/content', 'event-card', array( 'event' => $event, 'variant' => 'compact' ) );
						continue;
					endif;
				endif;
				?>
				<article class="search-result" id="post-<?php the_ID(); ?>">
					<h2 class="search-result__title">
						<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
					</h2>
					<p class="search-result__type">
						<?php
						$type_labels = array(
							'post'        => __( 'News', 'northfield-theatre' ),
							'page'        => __( 'Page', 'northfield-theatre' ),
							'nt_event'    => __( 'Event', 'northfield-theatre' ),
							'nt_instance' => __( 'Performance', 'northfield-theatre' ),
						);
						$type = get_post_type();
						echo esc_html( isset( $type_labels[ $type ] ) ? $type_labels[ $type ] : ucfirst( $type ) );
						?>
					</p>
					<?php if ( has_excerpt() ) : ?>
						<p class="search-result__excerpt"><?php echo esc_html( get_the_excerpt() ); ?></p>
					<?php endif; ?>
				</article>
			<?php endwhile; ?>
		</div>

		<?php the_posts_pagination( array(
			'prev_text' => esc_html__( 'Previous', 'northfield-theatre' ),
			'next_text' => esc_html__( 'Next', 'northfield-theatre' ),
		) ); ?>

	<?php else : ?>
		<div class="search-results__empty">
			<h2><?php esc_html_e( 'No results found', 'northfield-theatre' ); ?></h2>
			<p><?php esc_html_e( 'We couldn\'t find anything matching your search. Try different keywords or browse our programme.', 'northfield-theatre' ); ?></p>
			<a href="<?php echo esc_url( home_url( '/whats-on/' ) ); ?>" class="btn btn--primary">
				<?php esc_html_e( "See what's on", 'northfield-theatre' ); ?>
			</a>
		</div>
	<?php endif; ?>
</div>

<?php
get_footer();
