<?php
get_header();
?>

<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_home() && ! is_front_page() ) {
                    single_post_title();
                } elseif ( is_category() ) {
                    single_cat_title();
                } elseif ( is_tag() ) {
                    single_tag_title();
                } elseif ( is_author() ) {
                    echo get_avatar( get_the_author_meta( 'ID' ), 32 );
                    the_author();
                } elseif ( is_year() ) {
                    the_time( 'Y' );
                } elseif ( is_month() ) {
                    the_time( 'F Y' );
                } elseif ( is_day() ) {
                    the_time( 'j F Y' );
                } elseif ( is_search() ) {
                    printf(
                        esc_html__( 'نتایج: %s', 'vista' ),
                        '<em>' . get_search_query() . '</em>'
                    );
                } else {
                    esc_html_e( 'آخرین نوشته‌ها', 'vista' );
                }
                ?>
            </h1>
            <?php if ( is_category() ) : ?>
                <p class="archive-header-desc">
                    <?php esc_html_e( 'مطالب دسته‌بندی نشده', 'vista' ); ?>
                </p>
            <?php endif; ?>
        </div>
    </div>

    <div class="archive-header-end">
        <nav class="breadcrumbs archive-breadcrumbs" aria-label="breadcrumb">
            <?php vista_breadcrumbs(); ?>
        </nav>
        <?php
        global $wp_query;
        $total = (int) $wp_query->found_posts;
        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="blog-grid">
					<?php
					$post_count = 0;
					while ( have_posts() ) :
						the_post();
						$post_count++;
						
						$delay_class = 'd' . min( $post_count, 4 );
						?>
						<article id="post-<?php the_ID(); ?>" <?php post_class( 'blog-card rv ' . $delay_class ); ?> aria-label="<?php the_title_attribute(); ?>">

							<div class="blog-card-thumb">
								<?php if ( has_post_thumbnail() ) : ?>
									<a href="<?php the_permalink(); ?>" tabindex="-1" aria-hidden="true">
										<?php
										the_post_thumbnail(
											'vista-blog-card',
											array(
												'loading' => 'lazy',
												'alt'     => the_title_attribute( array( 'echo' => false ) ),
											)
										);
										?>
									</a>
								<?php else : ?>
									<a href="<?php the_permalink(); ?>" tabindex="-1" aria-hidden="true">
										<i class="fa-solid fa-newspaper" style="font-size:36px;color:var(--t3);" aria-hidden="true"></i>
									</a>
								<?php endif; ?>

								<?php
								$categories = get_the_category();
								if ( $categories ) :
									?>
									<a href="<?php echo esc_url( get_category_link( $categories[0]->term_id ) ); ?>"
										class="blog-card-cat">
										<?php echo esc_html( $categories[0]->name ); ?>
									</a>
								<?php endif; ?>
							</div>

							<div class="blog-card-body">

								<div class="blog-card-meta">
									<time datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>">
										<?php echo esc_html( get_the_date() ); ?>
									</time>
									<span class="blog-card-meta-sep" aria-hidden="true"></span>
									<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>">
										<?php the_author(); ?>
									</a>
								</div>

								<h2 class="blog-card-title">
									<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
								</h2>

								<div class="blog-card-excerpt">
									<?php the_excerpt(); ?>
								</div>

<div class="blog-card-footer">
    <a href="<?php the_permalink(); ?>" class="blog-card-more">
<?php
if ( function_exists('vista_opt') ) {
    $lang      = function_exists('pll_current_language') ? pll_current_language() : 'fa';
    $key       = $lang === 'fa' ? 'read_more_text' : 'read_more_text_' . $lang;
    $read_more = vista_opt( $key, __('ادامه', 'vista') );
} else {
    $read_more = __('ادامه', 'vista');
}
echo esc_html($read_more);
?>
        <i class="fa-solid fa-arrow-<?php echo is_rtl() ? esc_attr('left') : esc_attr('right'); ?>" aria-hidden="true"></i>
    </a>
    <?php if ( ! function_exists( 'vista_show' ) || vista_show( 'show_reading_time', true ) ) : ?>
    <div class="blog-card-read-time">
        <?php vista_reading_time( get_the_ID(), true ); ?>
    </div>
    <?php endif; ?>
</div>
							</div>
						</article>
					<?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-magnifying-glass" 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>
					<?php if ( is_search() ) : ?>
						<p style="margin-top:20px;">
							<?php get_search_form(); ?>
						</p>
					<?php endif; ?>
				</div>

			<?php endif; ?>
		</div>
	</section>

</main>
<?php
get_footer();
