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

// دریافت اطلاعات نویسنده
$authorId = $this->author->uid;
$authorName = $this->author->screenName;
$authorMail = $this->author->mail;
$authorUrl = $this->author->url;
$authorGroup = $this->author->group;
$authorRegistered = $this->author->created;

$db = Typecho_Db::get();

// تعداد کل پست‌های نویسنده
$totalPosts = $db->fetchObject($db->select('COUNT(*) as count')
    ->from('table.contents')
    ->where('authorId = ?', $authorId)
    ->where('type = ?', 'post')
    ->where('status = ?', 'publish'))->count;

// تعداد کل نظرات دریافت شده توسط نویسنده (نظراتی که روی پست‌های نویسنده ثبت شده)
$totalComments = $db->fetchObject($db->select('COUNT(*) as count')
    ->from('table.comments')
    ->where('ownerId = ?', $authorId)  // ownerId = نویسنده پست
    ->where('status = ?', 'approved'))->count;

// آخرین فعالیت (تاریخ آخرین پست)
$lastPost = $db->fetchRow($db->select('created')
    ->from('table.contents')
    ->where('authorId = ?', $authorId)
    ->where('type = ?', 'post')
    ->where('status = ?', 'publish')
    ->order('created', Typecho_Db::SORT_DESC)
    ->limit(1));

$lastActivity = $lastPost ? getLocalizedDate($lastPost['created'], 'Y/m/d') : 'هنوز فعالیتی نداشته';

// میانگین بازدید هر پست (در صورت وجود افزونه)
$avgViews = '---';
if (class_exists('ViewsCounter_Plugin')) {
    $totalViews = 0;
    $posts = $db->fetchAll($db->select('cid')
        ->from('table.contents')
        ->where('authorId = ?', $authorId)
        ->where('type = ?', 'post')
        ->where('status = ?', 'publish'));
    
    if (!empty($posts)) {
        foreach ($posts as $post) {
            $totalViews += ViewsCounter_Plugin::getViewsById($post['cid']);
        }
        $avgViews = round($totalViews / count($posts));
        $avgViews = toPersianNumbers($avgViews);
    }
}

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

<main class="archive-main full-width">
    <!-- سربرگ مدرن نویسنده -->
    <div class="archive-header-modern">
        <div class="container">
            <div class="archive-header-card author-header-card">
                <div class="author-header-avatar">
                    <img src="https://www.gravatar.com/avatar/<?php echo md5(strtolower($authorMail)); ?>?s=120&d=mp&r=g" alt="<?php echo htmlspecialchars($authorName); ?>">
                </div>
                <div class="archive-header-info">
                    <h1 class="archive-header-title"><?php echo htmlspecialchars($authorName); ?></h1>
                    
                    <?php
                    $roleClass = '';
                    $roleText = '';
                    if ($authorGroup == 'administrator') {
                        $roleClass = 'admin';
                        $roleText = 'مدیر سایت';
                    } elseif ($authorGroup == 'editor') {
                        $roleClass = 'editor';
                        $roleText = 'ویراستار';
                    } else {
                        $roleClass = 'author';
                        $roleText = 'نویسنده';
                    }
                    ?>
                    <span class="author-role-badge <?php echo $roleClass; ?>">
                        <i class="fa-solid fa-crown"></i> <?php echo $roleText; ?>
                    </span>
                    
                    <?php if ($authorUrl): ?>
                        <div class="author-website">
                            <i class="fa-solid fa-globe"></i>
                            <a href="<?php echo $authorUrl; ?>" target="_blank" rel="nofollow noopener">
                                <?php echo parse_url($authorUrl, PHP_URL_HOST) ?: $authorUrl; ?>
                            </a>
                        </div>
                    <?php endif; ?>
                    
                    <!-- کارت‌های آمار -->
                    <div class="author-stats-grid">
                        <div class="author-stat-item">
                            <span class="stat-value"><?php echo toPersianNumbers($totalPosts); ?></span>
                            <span class="stat-label">مقاله</span>
                        </div>
                        <div class="author-stat-item">
                            <span class="stat-value"><?php echo toPersianNumbers($totalComments); ?></span>
                            <span class="stat-label">نظر دریافتی</span>
                        </div>
                        <div class="author-stat-item">
                            <span class="stat-value"><?php echo getLocalizedDate($authorRegistered, 'Y/m/d'); ?></span>
                            <span class="stat-label">عضویت</span>
                        </div>
                        <div class="author-stat-item">
                            <span class="stat-value"><?php echo $lastActivity; ?></span>
                            <span class="stat-label">آخرین فعالیت</span>
                        </div>
                        <?php if ($avgViews !== '---'): ?>
                        <div class="author-stat-item">
                            <span class="stat-value"><?php echo $avgViews; ?></span>
                            <span class="stat-label">میانگین بازدید</span>
                        </div>
                        <?php endif; ?>
                    </div>
                    
                    <!-- شبکه‌های اجتماعی (در صورت تنظیم) -->
                    <div class="author-social-mini">
                        <?php if ($this->options->telegram): ?>
                            <a href="<?php $this->options->telegram(); ?>" class="social-link telegram" target="_blank"><i class="fa-brands fa-telegram"></i></a>
                        <?php endif; ?>
                        <?php if ($this->options->instagram): ?>
                            <a href="<?php $this->options->instagram(); ?>" class="social-link instagram" target="_blank"><i class="fa-brands fa-instagram"></i></a>
                        <?php endif; ?>
                        <?php if ($this->options->twitter): ?>
                            <a href="<?php $this->options->twitter(); ?>" class="social-link twitter" target="_blank"><i class="fa-brands fa-twitter"></i></a>
                        <?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>
                                <?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-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-pen-to-square"></i></div>
                    <h3>این نویسنده هنوز مقاله‌ای منتشر نکرده است</h3>
                </div>
            <?php endif; ?>
        </div>
    </section>
</main>

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