<?php
/**
 * The template for displaying archive pages
 *
 * @package pix
 */

get_header();

$paged = max(1, (int) get_query_var('paged'));
$total_posts = isset($wp_query->found_posts) ? (int) $wp_query->found_posts : 0;
$max_pages = isset($wp_query->max_num_pages) ? (int) $wp_query->max_num_pages : 1;

$archive_title = get_the_archive_title();
$archive_desc = get_the_archive_description();
$archive_kicker = 'Archive';
$archive_icon = 'ri-archive-line';

if (is_category()) {
    $archive_title = single_cat_title('', false);
    $archive_kicker = 'Category';
    $archive_icon = 'ri-folder-3-line';
} elseif (is_tag()) {
    $archive_title = single_tag_title('', false);
    $archive_kicker = 'Tag';
    $archive_icon = 'ri-price-tag-3-line';
} elseif (is_date()) {
    $archive_kicker = 'Date Archive';
    $archive_icon = 'ri-calendar-line';
} elseif (is_post_type_archive()) {
    $archive_kicker = 'Post Type';
    $archive_icon = 'ri-stack-line';
} elseif (is_tax()) {
    $archive_title = single_term_title('', false);
    $archive_kicker = 'Taxonomy';
    $archive_icon = 'ri-hashtag';
}

$archive_title = $archive_title ? $archive_title : '内容归档';
?>

<div class="pix-content home-box home-classic pix-archive-wrap">
    <div class="uk-flex-center uk-grid-collapse" uk-grid>
        <div class="center-content full-width uk-width-expand">
            <main id="primary" class="site-main pix-archive-page cls-content">
                <section class="pix-archive-hero">
                    <div class="pix-archive-mark"><i class="<?php echo esc_attr($archive_icon); ?>"></i></div>
                    <div class="pix-search-kicker"><?php echo esc_html($archive_kicker); ?></div>
                    <h1><?php echo esc_html($archive_title); ?></h1>
                    <?php if ($archive_desc) : ?>
                        <div class="pix-archive-desc"><?php echo wp_kses_post($archive_desc); ?></div>
                    <?php else : ?>
                        <p>这里汇总了当前归档下的公开内容，你可以继续向下浏览全部结果。</p>
                    <?php endif; ?>
                    <div class="pix-archive-meta">
                        <span><i class="ri-file-list-3-line"></i><?php echo esc_html(number_format_i18n($total_posts)); ?> 条内容</span>
                        <?php if ($max_pages > 1) : ?>
                            <span><i class="ri-pages-line"></i>第 <?php echo esc_html(number_format_i18n($paged)); ?> / <?php echo esc_html(number_format_i18n($max_pages)); ?> 页</span>
                        <?php endif; ?>
                    </div>
                </section>

                <section class="pix-archive-body">
                    <?php if (have_posts()) : ?>
                        <div class="pix-search-section-head">
                            <div>
                                <span>Posts</span>
                                <h2>归档内容</h2>
                            </div>
                            <p>按发布时间排序</p>
                        </div>

                        <div id="blog-item" class="pix-search-results-grid pix-archive-results p-item box-p">
                            <?php
                            while (have_posts()) :
                                the_post();
                                if ('post' === get_post_type()) {
                                    get_template_part('tpl/content', 'grid');
                                } else {
                                    get_template_part('tpl/content', 'search');
                                }
                            endwhile;
                            ?>
                        </div>

                        <?php
                        $pagination = paginate_links(array(
                            'current' => $paged,
                            'total' => $max_pages,
                            'prev_text' => '<i class="ri-arrow-left-s-line"></i>',
                            'next_text' => '<i class="ri-arrow-right-s-line"></i>',
                            'type' => 'list',
                        ));

                        if ($pagination) :
                        ?>
                            <div class="pix-archive-pagination pagination-box">
                                <?php echo wp_kses_post($pagination); ?>
                            </div>
                        <?php endif; ?>
                    <?php else : ?>
                        <div class="pix-search-empty">
                            <div class="pix-search-empty-icon"><i class="ri-inbox-archive-line"></i></div>
                            <h2>暂无内容</h2>
                            <p>这个归档下暂时没有公开内容，可以先回到首页看看最新更新。</p>
                            <div class="pix-404-actions pix-archive-empty-actions">
                                <a class="pix-404-primary" href="<?php echo esc_url(home_url('/')); ?>"><i class="ri-home-4-line"></i> 回到首页</a>
                            </div>
                        </div>
                    <?php endif; ?>
                </section>
            </main>
        </div>
    </div>
</div>

<?php
get_footer();
