<?php
/**
 * 关注 / 粉丝列表
 */
require_login();
$me = current_user();
$uid = (int) input('id', (int) $me['id']);
$user = user_by_id($uid);
if (!$user) { render_header('用户不存在'); empty_state('没有找到这位茶客'); render_footer(); return; }
$tab = input('tab') === 'following' ? 'following' : 'followers';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && input('act') === 'follow') {
    check_csrf();
    $target = (int) input('target');
    if ($target > 0 && $target !== (int) $me['id']) {
        $on = follow_toggle((int) $me['id'], 'user', $target);
        if ($on) { notification_insert($target, (int) $me['id'], 'follow', (int) $me['id'], display_name($me) . ' 关注了你', page_url('user', ['id' => (int) $me['id']])); }
        flash($on ? '已关注' : '已取消关注');
    }
    redirect(page_url('followers', ['id' => $uid, 'tab' => $tab]));
}

$rows = $tab === 'followers'
    ? DB::all('SELECT u.*, f.created_at AS follow_at FROM ' . DB::table('follows') . ' f INNER JOIN ' . DB::table('users') . ' u ON u.id = f.follower_id WHERE f.target_type = ? AND f.target_id = ? AND u.status = 1 ORDER BY f.id DESC LIMIT 100', ['user', $uid])
    : DB::all('SELECT u.*, f.created_at AS follow_at FROM ' . DB::table('follows') . ' f INNER JOIN ' . DB::table('users') . ' u ON u.id = f.target_id WHERE f.follower_id = ? AND f.target_type = ? AND u.status = 1 ORDER BY f.id DESC LIMIT 100', [$uid, 'user']);

$GLOBALS['PAGE'] = 'followers';
render_header(($tab === 'followers' ? '关注 ' . display_name($user) . ' 的人' : display_name($user) . ' 关注的人'));
?>
<section class="panel">
  <div class="panel-head row">
    <div><h2 class="section-title"><?= e(display_name($user)) ?></h2><p class="muted">关注关系会同步显示在用户主页。</p></div>
    <a class="btn-mini" href="<?= e(page_url('user', ['id' => $uid])) ?>">返回主页</a>
  </div>
  <div class="tabs-row">
    <a class="chip <?= $tab === 'followers' ? 'on' : '' ?>" href="<?= e(page_url('followers', ['id' => $uid, 'tab' => 'followers'])) ?>">关注 TA 的人</a>
    <a class="chip <?= $tab === 'following' ? 'on' : '' ?>" href="<?= e(page_url('followers', ['id' => $uid, 'tab' => 'following'])) ?>">TA 关注的人</a>
  </div>
  <?php if (!$rows): ?>
    <?php empty_state('这里还是空的', '关注喜欢的茶客，动态就会出现在首页「关注」'); ?>
  <?php else: ?>
    <div class="user-grid">
      <?php foreach ($rows as $u): $following = follow_is((int) $me['id'], 'user', (int) $u['id']); ?>
        <div class="user-cell">
          <a class="uc-avatar" href="<?= e(page_url('user', ['id' => $u['id']])) ?>"><?= avatar_img($u, 'uc-img') ?></a>
          <a class="uc-name" href="<?= e(page_url('user', ['id' => $u['id']])) ?>"><?= e(display_name($u)) ?></a>
          <?php if ((int) $u['id'] !== (int) $me['id']): ?>
          <form method="post" class="inline-form">
            <?= csrf_field() ?><input type="hidden" name="act" value="follow"><input type="hidden" name="target" value="<?= (int) $u['id'] ?>">
            <button class="btn-mini <?= $following ? 'ghost' : '' ?>" type="submit"><?= $following ? '已关注' : '关注' ?></button>
          </form>
          <?php endif; ?>
        </div>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>
</section>
<?php render_footer(); ?>
