<?php
/**
 * Shared archive, posts-page, category and search layout.
 *
 * @package HaPantheon
 */

if ( is_search() ) {
	$eyebrow = __( 'חיפוש', 'hapantheon' );
	$title    = sprintf(
		/* translators: %s: search query. */
		__( 'תוצאות חיפוש עבור „%s”', 'hapantheon' ),
		get_search_query()
	);
	$intro = sprintf(
		/* translators: %d: result count. */
		__( 'נמצאו %d תוצאות בארכיון.', 'hapantheon' ),
		(int) $GLOBALS['wp_query']->found_posts
	);
} elseif ( is_category() ) {
	$eyebrow = __( 'אולם', 'hapantheon' );
	$title    = single_cat_title( '', false );
	$intro    = category_description() ? wp_strip_all_tags( category_description() ) : __( 'מגילות מן האולם הזה בארכיון.', 'hapantheon' );
} elseif ( is_home() ) {
	$posts_page_id = (int) get_option( 'page_for_posts' );
	$eyebrow       = 'TABULARIUM';
	$title          = $posts_page_id ? get_the_title( $posts_page_id ) : __( 'הארכיון', 'hapantheon' );
	$intro          = $posts_page_id && has_excerpt( $posts_page_id ) ? get_the_excerpt( $posts_page_id ) : __( 'כל המגילות, המחקרים והסיפורים במקום אחד.', 'hapantheon' );
} else {
	$eyebrow = __( 'ארכיון', 'hapantheon' );
	$title    = get_the_archive_title();
	$intro    = get_the_archive_description() ? wp_strip_all_tags( get_the_archive_description() ) : __( 'עיינו בארכיון החי.', 'hapantheon' );
}

$current_category = is_category() ? get_queried_object_id() : 0;
$hall_slugs       = array( 'recipes', 'stories', 'travel', 'diy', 'oracle' );
?>
<section class="page-hero">
	<div class="wrap">
		<span class="eyebrow"><?php echo esc_html( $eyebrow ); ?></span>
		<h1><?php echo esc_html( $title ); ?></h1>
		<?php if ( $intro ) : ?><p><?php echo esc_html( $intro ); ?></p><?php endif; ?>
		<div class="meander" aria-hidden="true"></div>
	</div>
</section>

<section class="wrap archive-shell">
	<nav class="tabs archive-tabs" aria-label="<?php esc_attr_e( 'סינון לפי אולם', 'hapantheon' ); ?>">
		<a class="tab" href="<?php echo esc_url( hapantheon_get_archive_url() ); ?>" aria-selected="<?php echo $current_category ? 'false' : 'true'; ?>"><?php esc_html_e( 'הכול', 'hapantheon' ); ?></a>
		<?php
		foreach ( $hall_slugs as $hall_slug ) :
			$term = get_term_by( 'slug', $hall_slug, 'category' );
			if ( ! $term || is_wp_error( $term ) ) {
				continue;
			}
			?>
			<a class="tab" href="<?php echo esc_url( get_category_link( $term->term_id ) ); ?>" aria-selected="<?php echo (int) $current_category === (int) $term->term_id ? 'true' : 'false'; ?>">
				<?php echo esc_html( $term->name ); ?>
			</a>
		<?php endforeach; ?>
	</nav>

	<div class="view-switch" role="group" aria-label="<?php esc_attr_e( 'בחירת תצוגת ארכיון', 'hapantheon' ); ?>">
		<button class="vbtn" type="button" data-view="cards" aria-pressed="true" aria-label="<?php esc_attr_e( 'תצוגת כרטיסים', 'hapantheon' ); ?>">
			<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" aria-hidden="true"><rect x="3" y="3" width="8" height="8" rx="1"/><rect x="13" y="3" width="8" height="8" rx="1"/><rect x="3" y="13" width="8" height="8" rx="1"/><rect x="13" y="13" width="8" height="8" rx="1"/></svg>
		</button>
		<button class="vbtn" type="button" data-view="list" aria-pressed="false" aria-label="<?php esc_attr_e( 'תצוגת רשימה', 'hapantheon' ); ?>">
			<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" aria-hidden="true"><path d="M9 6h12M9 12h12M9 18h12"/><circle cx="4.5" cy="6" r="1.3" fill="currentColor"/><circle cx="4.5" cy="12" r="1.3" fill="currentColor"/><circle cx="4.5" cy="18" r="1.3" fill="currentColor"/></svg>
		</button>
		<button class="vbtn" type="button" data-view="masonry" aria-pressed="false" aria-label="<?php esc_attr_e( 'תצוגת בנייה', 'hapantheon' ); ?>">
			<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" aria-hidden="true"><rect x="3" y="3" width="8" height="11" rx="1"/><rect x="13" y="3" width="8" height="6" rx="1"/><rect x="13" y="11" width="8" height="10" rx="1"/><rect x="3" y="16" width="8" height="5" rx="1"/></svg>
		</button>
	</div>

	<?php if ( have_posts() ) : ?>
		<div class="entries">
			<?php
			while ( have_posts() ) :
				the_post();
				hapantheon_post_card();
			endwhile;
			?>
		</div>
		<nav class="archive-pagination" aria-label="<?php esc_attr_e( 'עמודי הארכיון', 'hapantheon' ); ?>">
			<?php
			the_posts_pagination(
				array(
					'mid_size'  => 2,
					'prev_text' => __( 'הקודם', 'hapantheon' ),
					'next_text' => __( 'הבא', 'hapantheon' ),
				)
			);
			?>
		</nav>
	<?php else : ?>
		<?php get_template_part( 'template-parts/content', 'none' ); ?>
	<?php endif; ?>
</section>
