<?php
/**
 * 公告列表
 */
$me = current_user();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && input('act') === 'delete') {
    if (!$me || !is_admin($me)) {
        http_response_code(403);
        exit('只有管理员和超级管理员可以删除公告');
    }
    check_csrf();
    $id = (int) input('id');
    if ($id > 0 && announcement_by_id($id)) {
        DB::delete('announcements', 'id = ?', [$id]);
        flash('公告已删除');
    } else {
        flash('公告不存在或已被删除', 'err');
    }
    redirect(page_url('announcements'));
}
$kind = trim((string) input('kind', ''));
$where = 'a.status = 1';
$params = [];
if ($kind !== '' && array_key_exists($kind, announcement_kinds())) {
    $where .= ' AND a.kind = ?';
    $params[] = $kind;
}
$rows = DB::all('SELECT a.*, u.nickname, u.username, u.avatar, u.verified, u.verify_style, u.verify_title, u.role, u.is_official
                 FROM ' . DB::table('announcements') . ' a
                 LEFT JOIN ' . DB::table('users') . ' u ON u.id = a.author_id
                 WHERE ' . $where . ' ORDER BY a.id DESC LIMIT 200', $params);
$GLOBALS['PAGE'] = 'announcements';
render_header('公告列表');
?>
<section class="notice-page">
  <div class="notice-page-head">
    <span class="hero-kicker">NOTICE</span>
    <h1>公告列表</h1>
    <p>共 <?= count($rows) ?> 条公告 · 弹窗公告与滚动横幅公告都在这里</p>
  </div>
  <div class="notice-filter">
    <a class="chip <?= $kind === '' ? 'on' : '' ?>" href="<?= e(page_url('announcements')) ?>">全部</a>
    <?php foreach (announcement_kinds() as $key => $label): ?>
      <a class="chip <?= $kind === $key ? 'on' : '' ?>" href="<?= e(page_url('announcements', ['kind' => $key])) ?>"><?= e($label) ?></a>
    <?php endforeach; ?>
  </div>
</section>

<section class="notice-list">
  <?php if (!$rows): ?>
    <?php empty_state('还没有公告', '茶室暂时没有新的消息'); ?>
  <?php else: ?>
    <?php foreach ($rows as $r): ?>
      <?php $author = announcement_author($r); ?>
      <article class="notice-card">
        <div class="notice-card-head">
          <div class="notice-author">
            <?php if ($author): ?><?= verified_avatar_html($author, 'notice-avatar', 'notice-avatar-wrap') ?><?php endif; ?>
            <div>
              <strong><?= e(display_name($author) ?: '管理员') ?></strong>
              <?php if ($author): ?><?= verify_badge($author, true) ?><?php endif; ?>
              <span><?= $author && is_official_account($author) ? '官方固定账号 · ' : '' ?><?= e($author ? username_visible($author) : '') ?> · <?= e($r['created_at']) ?></span>
            </div>
          </div>
          <span class="announcement-kind-pill"><?= e(announcement_kind_label($r['kind'])) ?></span>
        </div>
        <h2><?= e($r['title']) ?></h2>
        <p class="notice-excerpt"><?= e(announcement_excerpt($r, 120)) ?></p>
        <div class="notice-card-actions">
          <a class="btn-mini" href="<?= e(page_url('announcement', ['id' => (int) $r['id']])) ?>">查看详情</a>
          <?php if ($me && is_admin($me)): ?>
            <a class="btn-mini ghost" href="<?= e(page_url('admin', ['s' => 'announcements', 'id' => (int) $r['id']])) ?>">修改</a>
            <form method="post" class="inline-form" data-confirm="确定删除这条公告？">
              <?= csrf_field() ?>
              <input type="hidden" name="act" value="delete">
              <input type="hidden" name="id" value="<?= (int) $r['id'] ?>">
              <button class="btn-mini danger" type="submit">删除</button>
            </form>
          <?php endif; ?>
        </div>
      </article>
    <?php endforeach; ?>
  <?php endif; ?>
</section>
<?php render_footer(); ?>
