<?php
/**
 * نی نی بلاگ - صفحه آرشیو دسته‌بندی
 */
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
$this->need('header.php');
?>

<main class="archive-main full-width">
    <!-- سربرگ مدرن -->
    <div class="archive-header-modern">
        <div class="container">
            <div class="archive-header-card">
                <div class="archive-header-icon">
                    <i class="fa-solid fa-folder-open"></i>
                </div>
                <div class="archive-header-info">
                    <h1 class="archive-header-title"><?php $this->archiveTitle('', '', ''); ?></h1>
                    <?php if ($this->getDescription()): ?>
                        <p class="archive-header-desc"><?php echo $this->getDescription(); ?></p>
                    <?php endif; ?>
                    
                    <?php
                    // شمارش تعداد پست‌های این دسته
                    $db = Typecho_Db::get();
                    $categorySlug = $this->getArchiveSlug();
                    $category = $db->fetchRow($db->select()->from('table.metas')
                        ->where('slug = ?', $categorySlug)
                        ->where('type = ?', 'category'));
                    
                    $postsCount = 0;
                    if ($category) {
                        $postsCount = $db->fetchObject($db->select('COUNT(*) as count')
                            ->from('table.relationships')
                            ->where('mid = ?', $category['mid']))->count;
                    }
                    ?>
                    
                    <div class="archive-header-meta">
                        <span><i class="fa-regular fa-file-lines"></i> <?php echo toPersianNumbers($postsCount); ?> مقاله</span>
                        <?php if ($this->getTotal() > 0): ?>
                            <span><i class="fa-regular fa-eye"></i> صفحه <?php echo toPersianNumbers($this->_currentPage ?: 1); ?> از <?php echo toPersianNumbers(ceil($this->getTotal() / $this->parameter->pageSize)); ?></span>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- لیست مقالات - دو ستونه -->
    <section class="archive-posts-section">
        <div class="container">
            <?php if ($this->have()): ?>
                <div class="posts-grid grid-2-cols">
                    <?php while($this->next()): ?>
                        <article class="blog-card">
                            <div class="blog-card-img">
                                <a href="<?php $this->permalink(); ?>">
                                    <img src="<?php echo getPostImage($this); ?>" alt="<?php $this->title(); ?>" loading="lazy">
                                </a>
                                <span class="blog-card-category"><?php $this->category(','); ?></span>
                            </div>
                            
                            <div class="blog-card-body">
                                <div class="blog-card-meta">
                                    <span><i class="fa-regular fa-user"></i> <a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a></span>
                                    <span><i class="fa-regular fa-calendar"></i> <?php echo getLocalizedDate($this->created); ?></span>
                                    <span><i class="fa-regular fa-comment"></i> <?php $this->commentsNum('۰', '۱', '%d'); ?></span>
                                </div>
                                
                                <h2 class="blog-card-title">
                                    <a href="<?php $this->permalink(); ?>"><?php $this->title(); ?></a>
                                </h2>
                                
                                <p class="blog-card-excerpt"><?php $this->excerpt(120, '...'); ?></p>
                                
                                <a href="<?php $this->permalink(); ?>" class="read-more-btn">
                                    ادامه مطلب <i class="fa-solid fa-arrow-left"></i>
                                </a>
                            </div>
                        </article>
                    <?php endwhile; ?>
                </div>
                
                <!-- صفحه‌بندی -->
                <nav class="archive-pagination">
                    <?php $this->pageNav(
                        '<i class="fa-solid fa-chevron-right"></i>',
                        '<i class="fa-solid fa-chevron-left"></i>',
                        2,
                        '...',
                        array(
                            'wrapTag' => 'ul',
                            'wrapClass' => 'page-navigator',
                            'itemTag' => 'li',
                            'textTag' => 'span',
                            'currentClass' => 'current',
                            'prevClass' => 'prev',
                            'nextClass' => 'next'
                        )
                    ); ?>
                </nav>
                
            <?php else: ?>
                <div class="no-results-block">
                    <div class="no-results-icon"><i class="fa-regular fa-folder-open"></i></div>
                    <h3>هیچ مقاله‌ای در این دسته‌بندی یافت نشد</h3>
                    <a href="<?php $this->options->siteUrl(); ?>" class="btn-primary">بازگشت به خانه</a>
                </div>
            <?php endif; ?>
        </div>
    </section>
</main>

<?php $this->need('footer.php'); ?>