<?php
/**
 * 全部评论：按最新评论展示，点击直接进入对应帖子
 */
$me = current_user();
$total = (int) DB::val('SELECT COUNT(*) FROM ' . DB::table('comments') . ' c
                         INNER JOIN ' . DB::table('posts') . ' p ON p.id = c.post_id
                         WHERE p.status = 1 AND c.status = 1 AND ' . post_author_only_filter('p', $me));
$pager = paginate($total, 20);
$rows = DB::all('SELECT c.*, p.title, p.password, p.id AS post_id,
                        u.nickname, u.username, u.avatar, u.gender,
                        u.verified, u.verify_style, u.verify_title, u.verify_type
                 FROM ' . DB::table('comments') . ' c
                 INNER JOIN ' . DB::table('posts') . ' p ON p.id = c.post_id
                 LEFT JOIN ' . DB::table('users') . ' u ON u.id = c.user_id
                 WHERE p.status = 1 AND c.status = 1 AND ' . post_author_only_filter('p', $me) . '
                 ORDER BY c.id DESC
                 LIMIT ' . $pager['limit'] . ' OFFSET ' . $pager['offset']);
$GLOBALS['PAGE'] = 'comments';
render_header('全部评论');
?>
<section class="panel comments-panel">
  <div class="panel-head row">
    <div>
      <h1>全部评论</h1>
      <p>共 <?= $total ?> 条评论 · 最新评论排在最前</p>
    </div>
    <a class="btn-mini ghost" href="<?= e(page_url('home')) ?>">返回首页</a>
  </div>
  <?php if (!$rows): ?>
    <?php empty_state('还没有评论', '等第一位茶客留下茶话'); ?>
  <?php else: ?>
    <div class="comment-feed-list">
      <?php foreach ($rows as $c): ?>
        <?php
          $quote = trim((string) $c['content']);
          $quoteShort = mb_strlen($quote) > 160 ? mb_substr($quote, 0, 160) . '…' : $quote;
        ?>
        <a class="comment-feed-item" href="<?= e(page_url('post', ['id' => $c['post_id']])) ?>">
          <div class="comment-feed-head">
            <?= avatar_html($c, 'comment-feed-avatar') ?>
            <span class="comment-feed-author"><?= e(display_name($c)) ?><?= verify_badge($c) ?></span>
            <span class="comment-feed-time"><?= e(time_ago($c['created_at'])) ?></span>
          </div>
          <p class="comment-feed-body">“<?= nl2br(e($quoteShort)) ?>”</p>
          <div class="comment-feed-post">
            <?php if ($c['password'] !== ''): ?><span class="tag lock" title="密码帖">🔒</span><?php endif; ?>
            <span>评论于</span>
            <strong>《<?= e($c['title']) ?>》</strong>
            <span class="comment-feed-enter">进入帖子 →</span>
          </div>
        </a>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>
</section>
<?= pager_html($pager, 'comments') ?>
<?php render_footer(); ?>
