<?php
/**
 * Case archive template with case_type filter.
 *
 * @package Fragrance_Trade
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

get_header();
?>
<main id="primary" class="site-main">
	<div class="page-content container">
		<?php
		get_template_part(
			'template-parts/breadcrumb',
			null,
			array(
				'items' => array(
					array( 'label' => __( '首页', 'fragrance-trade' ), 'url' => home_url( '/' ) ),
					array( 'label' => __( '案例作品', 'fragrance-trade' ), 'url' => null ),
				),
			)
		);
		?>
	<header class="page-header pb-5">
		<h1><?php esc_html_e( '案例作品', 'fragrance-trade' ); ?></h1>
		<p><?php esc_html_e( '浏览不同车型与项目的施工记录。实际效果会受膜料、车漆、光线和拍摄条件影响。', 'fragrance-trade' ); ?></p>
	</header>
	<div class="pb-5">
		<?php
		$case_terms = get_terms(
			array(
				'taxonomy'   => 'case_type',
				'hide_empty' => false,
			)
		);
		if ( ! empty( $case_terms ) && ! is_wp_error( $case_terms ) ) :
			$current_type = isset( $_GET['case_type'] ) ? sanitize_text_field( wp_unslash( $_GET['case_type'] ) ) : '';
			?>
			<div class="case-filter mb-4 d-flex flex-wrap gap-2">
				<a href="<?php echo esc_url( home_url( '/cases/' ) ); ?>" class="btn btn-sm <?php echo $current_type ? 'btn-outline-secondary' : 'btn-b2b-primary'; ?>"><?php esc_html_e( '全部', 'fragrance-trade' ); ?></a>
				<?php foreach ( $case_terms as $term ) : ?>
					<a href="<?php echo esc_url( add_query_arg( 'case_type', $term->slug, home_url( '/cases/' ) ) ); ?>" class="btn btn-sm <?php echo $current_type === $term->slug ? 'btn-b2b-primary' : 'btn-outline-secondary'; ?>"><?php echo esc_html( $term->name ); ?></a>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>

		<?php if ( have_posts() ) : ?>
			<div class="case-grid case-grid-archive">
				<?php while ( have_posts() ) : the_post(); ?>
					<a class="case-card" href="<?php the_permalink(); ?>">
						<?php if ( has_post_thumbnail() ) : ?>
							<?php the_post_thumbnail( 'medium', array( 'class' => 'img-fluid' ) ); ?>
						<?php else : ?>
							<img src="<?php echo esc_url( fragrance_trade_asset_uri( 'images/products/placeholder-01.svg' ) ); ?>" alt="<?php the_title_attribute(); ?>" class="img-fluid">
						<?php endif; ?>
						<span class="case-card-title"><?php the_title(); ?></span>
					</a>
				<?php endwhile; ?>
			</div>
			<?php the_posts_pagination(); ?>
		<?php else : ?>
			<p><?php esc_html_e( '案例内容正在整理中。', 'fragrance-trade' ); ?></p>
		<?php endif; ?>
	</div>
	</div>
</main>
<?php get_footer(); ?>
