<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>

<aside class="nini-sidebar">
    <?php $widgets = $this->options->sidebarWidgets; ?>

    <!-- ========== جستجو ========== -->
    <?php if (empty($widgets) || in_array('ShowSearch', (array)$widgets)): ?>
    <div class="sidebar-widget">
        <h3 class="widget-title">
            <i class="fa-solid fa-magnifying-glass"></i> جستجو
        </h3>
        <form action="<?php $this->options->siteUrl(); ?>" method="get" class="search-form">
            <input type="text" name="s" placeholder="چه می‌خواهید پیدا کنید؟">
            <button type="submit"><i class="fa-solid fa-magnifying-glass"></i></button>
        </form>
    </div>
    <?php endif; ?>

    <!-- ========== دسته‌بندی‌ها ========== -->
    <?php if (empty($widgets) || in_array('ShowCategories', (array)$widgets)): ?>
    <div class="sidebar-widget">
        <h3 class="widget-title">
            <i class="fa-solid fa-folder"></i> دسته‌بندی‌ها
        </h3>
        <ul class="category-list">
            <?php
            $this->widget('Widget_Metas_Category_List')->to($categories);
            while($categories->next()):
            ?>
                <li>
                    <a href="<?php $categories->permalink(); ?>">
                        <span><?php $categories->name(); ?></span>
                        <span class="count"><?php echo toPersianNumbers($categories->count); ?></span>
                    </a>
                </li>
            <?php endwhile; ?>
        </ul>
    </div>
    <?php endif; ?>

    <!-- ========== محبوب‌ترین‌ها (پربازدیدترین‌ها) ========== -->
<?php if (empty($widgets) || in_array('ShowPopular', (array)$widgets)): ?>
<div class="sidebar-widget">
    <h3 class="widget-title">
        <i class="fa-solid fa-fire"></i> محبوب‌ترین‌ها
    </h3>

<?php
$popularPosts = [];

if (class_exists('ViewsCounter_Plugin')) {
    $popularPosts = ViewsCounter_Plugin::getMostViewed();
}

if (!empty($popularPosts)):
    foreach ($popularPosts as $post):
?>

    <div class="popular-post">
        <div class="popular-post-img">
            <a href="<?php $post->permalink(); ?>">
                <img src="<?php echo getPostImage($post, 'small'); ?>"
                     alt="<?php $post->title(); ?>">
            </a>
        </div>

        <div class="popular-post-content">
            <h4>
                <a href="<?php $post->permalink(); ?>">
                    <?php $post->title(); ?>
                </a>
            </h4>
            <span>
                <i class="fa-regular fa-calendar"></i>
                <?php echo getLocalizedDate($post->created); ?>
            </span>
        </div>
    </div>

<?php
    endforeach;
else:

    $this->widget('Widget_Contents_Post_Recent', 'pageSize=5')->to($recentPosts);
    while ($recentPosts->next()):
?>

    <div class="popular-post">
        <div class="popular-post-img">
            <a href="<?php $recentPosts->permalink(); ?>">
                <img src="<?php echo getPostImage($recentPosts, 'small'); ?>"
                     alt="<?php $recentPosts->title(); ?>">
            </a>
        </div>

        <div class="popular-post-content">
            <h4>
                <a href="<?php $recentPosts->permalink(); ?>">
                    <?php $recentPosts->title(); ?>
                </a>
            </h4>
            <span>
                <i class="fa-regular fa-calendar"></i>
                <?php echo getLocalizedDate($recentPosts->created); ?>
            </span>
        </div>
    </div>

<?php
    endwhile;
endif;
?>

</div>
<?php endif; ?>


    <!-- ========== آخرین نظرات ========== -->
    <?php if (in_array('ShowRecentComments', (array)$widgets)): ?>
    <div class="sidebar-widget">
        <h3 class="widget-title">
            <i class="fa-solid fa-comments"></i> آخرین نظرات
        </h3>
        <?php
        $recentComments = $this->widget('Widget_Comments_Recent', array('pageSize' => 5));
        if ($recentComments->have()):
            while ($recentComments->next()):

                $avatarUrl = 'https://www.gravatar.com/avatar/' .
                    md5(strtolower(trim($recentComments->mail))) . '?s=50&d=mp&r=g';

                $commentText = strip_tags($recentComments->text);
                $excerpt = mb_strlen($commentText) > 60 ? mb_substr($commentText, 0, 60) . '...' : $commentText;
        ?>
            <div class="recent-comment">
                <div class="recent-comment-avatar">
                    <img src="<?php echo $avatarUrl; ?>" alt="<?php echo htmlspecialchars($recentComments->author); ?>">
                </div>
                <div class="recent-comment-content">
                    <span class="comment-author-name"><?php echo htmlspecialchars($recentComments->author); ?></span>
                    <p class="comment-excerpt"><?php echo $excerpt; ?></p>
                    <a href="<?php $recentComments->permalink(); ?>" class="comment-post-link">
                        <?php $recentComments->title(); ?>
                    </a>
                </div>
            </div>
        <?php
            endwhile;
        else:
        ?>
            <p class="no-comments-msg">هنوز نظری ثبت نشده است.</p>
        <?php endif; ?>
    </div>
    <?php endif; ?>

    <!-- ========== برچسب‌ها ========== -->
    <?php if (empty($widgets) || in_array('ShowTags', (array)$widgets)): ?>
    <div class="sidebar-widget">
        <h3 class="widget-title">
            <i class="fa-solid fa-tags"></i> برچسب‌ها
        </h3>
        <div class="tags-cloud">
            <?php
            $this->widget('Widget_Metas_Tag_Cloud', 'limit=20')->to($tags);
            while($tags->next()):
            ?>
                <a href="<?php $tags->permalink(); ?>"><?php $tags->name(); ?></a>
            <?php endwhile; ?>
        </div>
    </div>
    <?php endif; ?>

</aside>
