<?php
get_header();

$is_tax       = is_tax();
$current_term = $is_tax ? get_queried_object() : null;

global $wp_query;
$total = (int) $wp_query->found_posts;
?>

<main id="primary" class="site-main" role="main">
	<section class="blog-archive-section">
		<div class="container">
			<div class="blog-archive-header archive-header-v2">
				<div class="archive-header-main">
					<div class="archive-header-icon" aria-hidden="true">
						<i class="fa-regular fa-folder-open"></i>
					</div>
					<div class="archive-header-info">
						<h1 class="section-title" style="margin-bottom:4px;">
							<?php
							if ( $is_tax && $current_term ) {
								echo esc_html( $current_term->name );
							} else {
								esc_html_e( 'پروژه‌ها', 'vista' );
							}
							?>
						</h1>
						<p class="archive-header-desc">
							<?php
							if ( $is_tax && $current_term && ! empty( $current_term->description ) ) {
								echo esc_html( $current_term->description );
							} elseif ( $is_tax ) {
								esc_html_e( 'پروژه‌های این دسته‌بندی', 'vista' );
							} 
							?>
						</p>
					</div>
				</div>

				<div class="archive-header-end">
					<nav class="breadcrumbs archive-breadcrumbs" aria-label="breadcrumb">
						<?php if ( ! function_exists( 'vista_show' ) || vista_show( 'show_breadcrumb', true ) ) { vista_breadcrumbs(); } ?>
					</nav>
					<?php if ( $total > 0 ) : ?>
						<div class="archive-post-count">
							<span><?php echo number_format_i18n( $total ); ?></span>
							<?php esc_html_e( 'پروژه', 'vista' ); ?>
						</div>
					<?php endif; ?>
				</div>
			</div>
			<?php if ( have_posts() ) : ?>

				<div class="pf-archive-grid" id="portfolioGrid">
					<?php
					while ( have_posts() ) :
						the_post();
						$tax_terms = get_the_terms( get_the_ID(), 'portfolio_category' );
						$cat_name  = '';
						if ( $tax_terms && ! is_wp_error( $tax_terms ) ) {
							$cat_name = $tax_terms[0]->name;
						}
					?>
						<div class="pf-card">
							<div class="pf-card-image">

								<?php if ( has_post_thumbnail() ) : ?>
									<a href="<?php the_permalink(); ?>">
										<?php the_post_thumbnail( 'vista-portfolio-thumb', array(
											'alt'     => the_title_attribute( array( 'echo' => false ) ),
											'loading' => 'lazy',
										) ); ?>
									</a>
								<?php else : ?>
									<a href="<?php the_permalink(); ?>" class="pf-no-thumb">
										<i class="fa-solid fa-folder-open" aria-hidden="true"></i>
									</a>
								<?php endif; ?>

								<?php if ( $cat_name ) : ?>
									<span class="pf-badge"><?php echo esc_html( $cat_name ); ?></span>
								<?php endif; ?>

							</div>

							<div class="pf-card-footer">
								<div class="pf-card-text">
									<h3>
										<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
									</h3>
									<?php if ( has_excerpt() ) : ?>
										<p><?php echo esc_html( wp_trim_words( get_the_excerpt(), 10 ) ); ?></p>
									<?php endif; ?>
								</div>
								<a href="<?php the_permalink(); ?>" class="pf-action-btn" aria-label="<?php the_title_attribute(); ?>">
									<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" aria-hidden="true">
										<path d="M7 17L17 7M17 7H7M17 7v10"/>
									</svg>
								</a>
							</div>
						</div>

					<?php endwhile; ?>
				</div>
				<div class="pagination-wrap">
					<?php
					$paginate_args = array(
						'type'      => 'list',
						'prev_text' => is_rtl()
							? '<i class="fa-solid fa-chevron-right"></i>'
							: '<i class="fa-solid fa-chevron-left"></i>',
						'next_text' => is_rtl()
							? '<i class="fa-solid fa-chevron-left"></i>'
							: '<i class="fa-solid fa-chevron-right"></i>',
					);
					$pagination = paginate_links( $paginate_args );
					if ( $pagination ) {
						echo wp_kses_post( $pagination );
					}
					?>
				</div>

			<?php else : ?>

				<div class="no-posts-found" style="text-align:center;padding:80px 20px;">
					<i class="fa-solid fa-folder-open" style="font-size:48px;color:var(--t3);margin-bottom:20px;display:block;" aria-hidden="true"></i>
					<h2 style="font-size:22px;font-weight:800;color:var(--t1);margin-bottom:10px;">
						<?php esc_html_e( 'پروژه‌ای یافت نشد', 'vista' ); ?>
					</h2>
					<p style="font-size:14px;color:var(--t2);">
						<?php esc_html_e( 'هنوز پروژه‌ای در این بخش ثبت نشده است.', 'vista' ); ?>
					</p>
				</div>

			<?php endif; ?>

		</div>
	</section>
</main>

<?php get_footer(); ?>
