<?php
/**
 * نی نی بلاگ - صفحه آرشیو برچسب
 */
if (!defined('__TYPECHO_ROOT_DIR__')) exit;

$isSingleTag = $this->have() && $this->is('tag');
$this->need('header.php');
?>

<main class="archive-main full-width">
    <?php if ($isSingleTag): ?>
        <!-- سربرگ مدرن برای برچسب خاص -->
        <div class="archive-header-modern">
            <div class="container">
                <div class="archive-header-card">
                    <div class="archive-header-icon">
                        <i class="fa-solid fa-tag"></i>
                    </div>
                    <div class="archive-header-info">
                        <h1 class="archive-header-title"><?php $this->archiveTitle('', '', ''); ?></h1>
                        
                        <?php
                        $db = Typecho_Db::get();
                        $tagSlug = $this->getArchiveSlug();
                        $tag = $db->fetchRow($db->select()->from('table.metas')
                            ->where('slug = ?', $tagSlug)
                            ->where('type = ?', 'tag'));
                        
                        $tagPostsCount = 0;
                        if ($tag) {
                            $tagPostsCount = $db->fetchObject($db->select('COUNT(*) as count')
                                ->from('table.relationships')
                                ->where('mid = ?', $tag['mid']))->count;
                        }
                        ?>
                        
                        <div class="archive-header-meta">
                            <span><i class="fa-regular fa-file-lines"></i> <?php echo toPersianNumbers($tagPostsCount); ?> مقاله</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?php else: ?>
        <!-- اگر به دلایلی صفحه برچسب بدون برچسب خاص باز شد -->
        <div class="archive-header-modern">
            <div class="container">
                <div class="archive-header-card">
                    <div class="archive-header-icon">
                        <i class="fa-solid fa-tags"></i>
                    </div>
                    <div class="archive-header-info">
                        <h1 class="archive-header-title">برچسب‌ها</h1>
                    </div>
                </div>
            </div>
        </div>
    <?php endif; ?>

    <!-- لیست مقالات برچسب - دو ستونه -->
    <?php if ($this->have()): ?>
        <section class="archive-posts-section">
            <div class="container">
                <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>
                                <?php if ($this->categories): ?>
                                    <span class="blog-card-category"><?php $this->category(','); ?></span>
                                <?php endif; ?>
                            </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>
            </div>
        </section>
    <?php endif; ?>

    <!-- نمایش ابر برچسب‌ها (اختیاری) -->
    <section class="tags-cloud-section">
        <div class="container">
            <?php
            $allTags = $this->widget('Widget_Metas_Tag_Cloud', array('limit' => 30));
            if ($allTags->have()):
            ?>
                <div class="section-title" style="margin-top: 40px;">
                    <i class="fa-solid fa-cloud"></i>
                    <h2>برچسب‌های پرکاربرد</h2>
                </div>
                <div class="tags-cloud-wrapper">
                    <?php while($allTags->next()): ?>
                        <a href="<?php $allTags->permalink(); ?>" class="tag-cloud-item">
                            <?php $allTags->name(); ?>
                            <span class="tag-count">(<?php echo toPersianNumbers($allTags->count); ?>)</span>
                        </a>
                    <?php endwhile; ?>
                </div>
            <?php endif; ?>
        </div>
    </section>
</main>

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