<?php
get_header();

if ( function_exists( 'wp_body_open' ) ) {
    wp_body_open();
}
?>

<section class="archive-page" aria-labelledby="archive-page-title">
    <header class="archive-page__header">
        <div class="archive-page__intro">
            <h1 id="archive-page-title" class="archive-page__title"><?php esc_html_e( 'آخرین مقالات', 'caffito' ); ?></h1>
            <?php
            $description = get_bloginfo( 'description' );
            if ( $description ) :
                ?>
                <p class="archive-page__description"><?php echo esc_html( $description ); ?></p>
            <?php endif; ?>
        </div>
        <div class="archive-page__tools">
            <?php get_search_form(); ?>
        </div>
    </header>

    <?php if ( have_posts() ) : ?>
        <div class="archive-page__layout">
            <div class="archive-page__posts">
                <div class="archive-grid">
                    <?php
                    while ( have_posts() ) :
                        the_post();
                        ?>
                        <article <?php post_class( 'archive-card' ); ?> aria-labelledby="post-<?php the_ID(); ?>-title">
                            <?php if ( has_post_thumbnail() ) : ?>
                                <a class="archive-card__media" href="<?php the_permalink(); ?>">
                                    <?php the_post_thumbnail( 'medium_large', [ 'class' => 'archive-card__image', 'loading' => 'lazy' ] ); ?>
                                </a>
                            <?php endif; ?>

                            <div class="archive-card__body">
                                <header class="archive-card__header">
                                    <div class="archive-card__meta">
                                        <time class="archive-card__date" datetime="<?php echo esc_attr( get_the_date( DATE_W3C ) ); ?>">
                                            <?php echo esc_html( get_the_date() ); ?>
                                        </time>
                                        <?php
                                        $categories = get_the_category();
                                        if ( ! empty( $categories ) ) :
                                            ?>
                                            <ul class="archive-card__categories">
                                                <?php foreach ( $categories as $category ) : ?>
                                                    <li class="archive-card__category">
                                                        <a href="<?php echo esc_url( get_category_link( $category ) ); ?>"><?php echo esc_html( $category->name ); ?></a>
                                                    </li>
                                                <?php endforeach; ?>
                                            </ul>
                                        <?php endif; ?>
                                    </div>
                                    <h2 id="post-<?php the_ID(); ?>-title" class="archive-card__title">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </h2>
                                </header>

                                <div class="archive-card__excerpt">
                                    <?php the_excerpt(); ?>
                                </div>

                                <footer class="archive-card__footer">
                                    <a class="archive-card__link" href="<?php the_permalink(); ?>">
                                        <?php esc_html_e( 'ادامه مطلب', 'caffito' ); ?>
                                    </a>
                                </footer>
                            </div>
                        </article>
                    <?php endwhile; ?>
                </div>

                <nav class="archive-pagination" aria-label="<?php esc_attr_e( 'صفحات مقالات', 'caffito' ); ?>">
                    <?php
                    the_posts_pagination( [
                        'mid_size'           => 2,
                        'prev_text'          => __( 'قبلی', 'caffito' ),
                        'next_text'          => __( 'بعدی', 'caffito' ),
                        'class'              => 'archive-pagination__links',
                        'screen_reader_text' => __( 'صفحه‌بندی', 'caffito' ),
                    ] );
                    ?>
                </nav>
            </div>

            <aside class="archive-page__sidebar">
                <section class="archive-widget archive-widget--categories">
                    <h2 class="archive-widget__title"><?php esc_html_e( 'دسته‌بندی‌ها', 'caffito' ); ?></h2>
                    <ul class="archive-widget__list">
                        <?php wp_list_categories( [ 'title_li' => '', 'show_count' => true ] ); ?>
                    </ul>
                </section>

                <section class="archive-widget archive-widget--recent">
                    <h2 class="archive-widget__title"><?php esc_html_e( 'آخرین نوشته‌ها', 'caffito' ); ?></h2>
                    <ul class="archive-widget__list">
                        <?php
                        $recent_posts = new WP_Query( [
                            'posts_per_page'      => 5,
                            'ignore_sticky_posts' => true,
                        ] );

                        if ( $recent_posts->have_posts() ) :
                            while ( $recent_posts->have_posts() ) :
                                $recent_posts->the_post();
                                ?>
                                <li class="archive-widget__item">
                                    <a class="archive-widget__link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                </li>
                            <?php
                            endwhile;
                            wp_reset_postdata();
                        else :
                            ?>
                            <li class="archive-widget__item archive-widget__item--empty">
                                <?php esc_html_e( 'نوشته‌ای یافت نشد.', 'caffito' ); ?>
                            </li>
                        <?php endif; ?>
                    </ul>
                </section>
            </aside>
        </div>
    <?php else : ?>
        <div class="archive-empty">
            <p class="archive-empty__message"><?php esc_html_e( 'هنوز هیچ مقاله‌ای منتشر نشده است.', 'caffito' ); ?></p>
            <a class="archive-empty__link" href="<?php echo esc_url( home_url( '/' ) ); ?>">
                <?php esc_html_e( 'بازگشت به صفحه اصلی', 'caffito' ); ?>
            </a>
        </div>
    <?php endif; ?>
</section>




<?php get_footer(); ?>
