<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit;
$db = \Typecho\Db::get();
$userBaseUrl = \Typecho\Common::url('/user', $this->options->index);
?>
<div class="uc-page">
    <div class="uc-page-header">
        <h2 class="uc-page-title">我的文章</h2>
        <a href="<?php echo $userBaseUrl . '/write'; ?>" class="uc-btn uc-btn-primary"><i class="ri-add-line"></i> 发布</a>
    </div>

    <div class="uc-list">
        <?php
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $pageSize = 10;

        $posts = $db->fetchAll($db->select()->from('table.contents')
            ->where('authorId = ?', $this->user->uid)
            ->where('type = ?', 'post')
            ->order('created', \Typecho\Db::SORT_DESC)
            ->page($page, $pageSize));

        $total = $db->fetchObject($db->select('COUNT(cid) AS num')->from('table.contents')
            ->where('authorId = ?', $this->user->uid)
            ->where('type = ?', 'post'))->num;
        ?>

        <?php if (empty($posts)): ?>
            <div class="uc-empty"><img src="<?php $this->options->themeUrl('assets/images/no.svg'); ?>" alt="你还没有发布过文章">你还没有发布过文章</div>
        <?php else: ?>
            <?php foreach ($posts as $post): ?>
                <div class="uc-list-item">
                    <div class="uc-item-body">
                        <div class="uc-item-title">
                            <?php if ($post['status'] == 'publish'): ?>
                                <a href="<?php echo \Typecho\Router::url('post', $post, $this->options->index); ?>" target="_blank"><?php echo $post['title']; ?></a>
                            <?php else: ?>
                                <?php echo $post['title']; ?>
                            <?php endif; ?>
                        </div>
                        <div class="uc-item-meta">
                            <span><i class="ri-time-line"></i> <?php echo (new \Typecho\Date($post['created']))->format('Y-m-d'); ?></span>
                            <span class="uc-tag uc-tag-<?php echo $post['status']; ?>">
                                <?php
                                $statusMap = ['publish' => '已发布', 'private' => '私密', 'waiting' => '待审核', 'hidden' => '草稿'];
                                echo isset($statusMap[$post['status']]) ? $statusMap[$post['status']] : $post['status'];
                                ?>
                            </span>
                            <span><i class="ri-message-3-line"></i> <?php echo $post['commentsNum']; ?></span>
                        </div>
                    </div>
                    <div class="uc-item-actions">
                        <?php if ($post['status'] == 'publish'): ?>
                            <a href="<?php echo \Typecho\Router::url('post', $post, $this->options->index); ?>" class="uc-icon-btn" title="查看" target="_blank"><i class="ri-eye-line"></i></a>
                        <?php endif; ?>
                        <a href="<?php echo $userBaseUrl . '/write?cid=' . $post['cid']; ?>" class="uc-icon-btn" title="编辑"><i class="ri-edit-line"></i></a>
                    </div>
                </div>
            <?php endforeach; ?>

            <?php if ($total > $pageSize): ?>
                <?php
                $totalPages = ceil($total / $pageSize);
                echo Mirai_customPagination($page, $totalPages, function($p) use ($userBaseUrl) {
                    return $userBaseUrl . '/posts?page=' . $p;
                });
                ?>
            <?php endif; ?>
        <?php endif; ?>
    </div>
</div>
