<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php date_default_timezone_set('Asia/Tehran'); ?>
<div class="col-mb-12 col-offset-1 col-3 kit-hidden-tb" id="secondary" role="complementary">
    <?php if (!empty($this->options->sidebarBlock) && in_array('ShowRecentPosts', $this->options->sidebarBlock)): ?>
        <section class="widget">
            <h3 class="widget-title"><?php _e('最新文章'); ?></h3>
            <ul class="widget-list">
                <?php \Widget\Contents\Post\Recent::alloc()
                    ->parse('<li><a href="{permalink}">{title}</a></li>'); ?>
            </ul>
        </section>
    <?php endif; ?>

    <?php if (!empty($this->options->sidebarBlock) && in_array('ShowRecentComments', $this->options->sidebarBlock)): ?>
        <section class="widget">
            <h3 class="widget-title"><?php _e('最近回复'); ?></h3>
            <ul class="widget-list">
                <?php \Widget\Comments\Recent::alloc()->to($comments); ?>
                <?php while ($comments->next()): ?>
                    <li>
                        <a href="<?php $comments->permalink(); ?>"><?php $comments->author(false); ?></a>: <?php $comments->excerpt(35, '...'); ?>
                    </li>
                <?php endwhile; ?>
            </ul>
        </section>
    <?php endif; ?>

    <?php if (!empty($this->options->sidebarBlock) && in_array('ShowCategory', $this->options->sidebarBlock)): ?>
        <section class="widget">
            <h3 class="widget-title"><?php _e('分类'); ?></h3>
            <?php \Widget\Metas\Category\Rows::alloc()->listCategories('wrapClass=widget-list'); ?>
        </section>
    <?php endif; ?>

    <?php if (!empty($this->options->sidebarBlock) && in_array('ShowArchive', $this->options->sidebarBlock)): ?>
       <section class="widget">
            <h3 class="widget-title"><?php _e('归档'); ?></h3>
            <ul class="widget-list">
                <?php
                $archives = array();
                // داده‌های آرشیو پست‌ها را بگیر
                $db = Typecho_Db::get();
                $select = $db->select('created')->from('table.contents')
                    ->where('type = ?', 'post')
                    ->where('status = ?', 'publish')
                    ->order('created', Typecho_Db::SORT_DESC);

                $result = $db->fetchAll($select);

                foreach ($result as $row) {
                    $timestamp = $row['created'];
                    // کلید آرشیو (شمسی یا میلادی) را بساز
                    if (function_exists('jalali_date')) {
                        $key = jalali_date('Y/m', $timestamp);
                        $label = jalali_date('F Y', $timestamp);
                    } else {
                        $key = date('Y/m', $timestamp);
                        $label = date('F Y', $timestamp);
                    }
                    $archives[$key] = array('timestamp' => $timestamp, 'label' => $label);
                }

                $archives = array_unique($archives, SORT_REGULAR);
                foreach ($archives as $key => $data) {
                    // ساخت آدرس آرشیو (میلادی یا شمسی را اینجا لحاظ کن - ولی لینک تایپچو معمولا فقط میلادی است!)
                    $url = Helper::options()->index . '/archive/' . $key . '/';
                    ?>
                    <li>
                        <a href="<?php echo $url; ?>">
                            <?php echo $data['label']; ?>
                        </a>
                    </li>
                    <?php
                }
                ?>
            </ul>
        </section>

    <?php endif; ?>

    <?php if (!empty($this->options->sidebarBlock) && in_array('ShowOther', $this->options->sidebarBlock)): ?>
        <section class="widget">
            <h3 class="widget-title"><?php _e('其它'); ?></h3>
            <ul class="widget-list">
                <?php if ($this->user->hasLogin()): ?>
                    <li class="last"><a href="<?php $this->options->adminUrl(); ?>"><?php _e('进入后台'); ?>
                            (<?php $this->user->screenName(); ?>)</a></li>
                    <li><a href="<?php $this->options->logoutUrl(); ?>"><?php _e('退出'); ?></a></li>
                <?php else: ?>
                    <li class="last"><a href="<?php $this->options->adminUrl('login.php'); ?>"><?php _e('登录'); ?></a>
                    </li>
                <?php endif; ?>
                <li><a href="<?php $this->options->feedUrl(); ?>"><?php _e('文章 RSS'); ?></a></li>
                <li><a href="<?php $this->options->commentsFeedUrl(); ?>"><?php _e('评论 RSS'); ?></a></li>
                <li><a href="https://typecho.org">Typecho</a></li>
            </ul>
        </section>
    <?php endif; ?>

</div><!-- end #sidebar -->
