<?php
/**
 * HuSNS - 一款免费开源的社交平台
 *
 * @author  HYR
 * @QQ      281900864
 * @website https://huyourui.com
 * @license MIT
 * @声明    严禁用于违法违规用途
 */
?>
<!-- 左侧栏 -->
<aside class="sidebar-left">
    <?php if (isset($_SESSION['user_id'])): ?>
    <div class="user-card">
            <img src="/templates/defaultpro/images/avatar_default.png" alt="" style="width:70px;height:70px;border-radius:50%;object-fit:cover;">
            <div class="user-card-name"><?php echo $this->escape($_SESSION['username'] ?? '用户'); ?></div>
            <div class="user-card-bio">这个人很懒，什么都没写</div>
            <div class="user-card-stats">
                <div class="stat-item"><div class="stat-value">-</div><div class="stat-label">动态</div></div>
                <div class="stat-item"><div class="stat-value">-</div><div class="stat-label">关注</div></div>
                <div class="stat-item"><div class="stat-value">-</div><div class="stat-label">粉丝</div></div>
            </div>
        </div>
        <?php else: ?>
        <div class="user-card">
            <img src="/templates/defaultpro/images/avatar_default.png" alt="" style="width:70px;height:70px;border-radius:50%;object-fit:cover;">
            <div class="user-card-name">游客</div>
            <div class="user-card-bio">登录后体验更多功能</div>
            <div class="user-card-stats">
                <div class="stat-item"><div class="stat-value">-</div><div class="stat-label">动态</div></div>
                <div class="stat-item"><div class="stat-value">-</div><div class="stat-label">关注</div></div>
                <div class="stat-item"><div class="stat-value">-</div><div class="stat-label">粉丝</div></div>
            </div>
        </div>
    <?php endif; ?>
    <?php if (!isset($_SESSION['user_id'])): ?>
    <div class="guest-tip">
        <p><a href="<?php echo $this->url('user/login'); ?>">登录</a> | <a href="<?php echo $this->url('user/register'); ?>">注册</a></p>
    </div>
    <?php endif; ?>

    <ul class="menu">
        <li><a href="<?php echo $this->url(); ?>">🏠 首页</a></li>
        <li><a href="<?php echo $this->url('?tab=following'); ?>">❤️ 关注</a></li>
        <li><a href="<?php echo $this->url('post/hot'); ?>">🔥 热门</a></li>
        <li><a href="<?php echo $this->url('post/featured'); ?>">✨ 推荐</a></li>
        <li><a href="<?php echo $this->url('notification'); ?>">💬 消息</a></li>
        <?php if (isset($_SESSION['user_id'])): ?>
        <li><a href="<?php echo $this->url('user/profile'); ?>">👤 我的主页</a></li>
        <li><a href="<?php echo $this->url('user/settings'); ?>">⚙️ 设置</a></li>
        <?php else: ?>
        <li><a href="<?php echo $this->url('user/login'); ?>">🔑 登录</a></li>
        <li><a href="<?php echo $this->url('user/register'); ?>">📝 注册</a></li>
        <?php endif; ?>
    </ul>

    <div class="side-card">
        <div class="side-title">快捷操作</div>
        <ul class="menu">
            <li><a href="#" onclick="showPublishPopup();return false;">✍️ 发布动态</a></li>
            <li><a href="<?php echo $this->url('user/search'); ?>">🔍 查找用户</a></li>
            <li><a href="<?php echo $this->url('post/topic'); ?>">🏷️ 话题广场</a></li>
        </ul>
    </div>
</aside>

<!-- 中间内容 -->
<section class="home-main">
    <div class="page-recommend">
    <h2>推荐用户</h2>
    
    <!-- 筛选标签 -->
    <div class="filter-tags">
        <span class="filter-tag active" data-filter="all">全部</span>
        <span class="filter-tag" data-filter="hot">🔥 热门用户</span>
        <span class="filter-tag" data-filter="new">✨ 新用户</span>
        <span class="filter-tag" data-filter="expert">🏆 优质博主</span>
    </div>
    
    <!-- 用户列表 -->
    <div class="user-grid">
        <?php if (!empty($users)): ?>
            <?php foreach ($users as $user): ?>
            <div class="user-card-item">
                <a href="<?php echo $this->url('user/profile') . '?id=' . $user['id']; ?>">
                    <img src="<?php echo $this->escape($user['avatar'] ?? '/templates/defaultpro/images/avatar_default.png'); ?>" alt="头像" class="user-avatar">
                    <div class="user-name"><?php echo $this->escape($user['username']); ?></div>
                    <div class="user-bio"><?php echo $this->escape($user['bio'] ?? '暂无简介'); ?></div>
                    <div class="user-stats">
                        <span><?php echo $user['posts_count'] ?? 0; ?> 动态</span>
                        <span><?php echo $user['followers_count'] ?? 0; ?> 粉丝</span>
                    </div>
                </a>
                <?php if (isset($_SESSION['user_id']) && $_SESSION['user_id'] != $user['id']): ?>
                <button class="follow-btn" data-user-id="<?php echo $user['id']; ?>">
                    <?php echo isset($user['is_following']) && $user['is_following'] ? '已关注' : '关注'; ?>
                </button>
                <?php endif; ?>
            </div>
            <?php endforeach; ?>
        <?php else: ?>
            <div class="empty-state">
                <div class="empty-icon">👥</div>
                <p>暂无推荐用户</p>
            </div>
        <?php endif; ?>
    </div>
    
    <!-- 分页 -->
    <?php if ($total_pages > 1): ?>
    <div class="pagination">
        <a href="?page=1" class="page-link">首页</a>
        <?php if ($current_page > 1): ?>
        <a href="?page=<?php echo $current_page - 1; ?>" class="page-link">上一页</a>
        <?php endif; ?>
        <span class="page-current"><?php echo $current_page; ?>/<?php echo $total_pages; ?></span>
        <?php if ($current_page < $total_pages): ?>
        <a href="?page=<?php echo $current_page + 1; ?>" class="page-link">下一页</a>
        <?php endif; ?>
        <a href="?page=<?php echo $total_pages; ?>" class="page-link">末页</a>
    </div>
    <?php endif; ?>
    </div>
</section>

<!-- 右侧栏 -->
<aside class="sidebar-right">
    <div class="side-card">
        <div class="side-title">推荐关注</div>
        <div id="recommend-users">
            <div class="empty-tip">登录后查看推荐</div>
        </div>
    </div>

    <div class="side-card">
        <div class="side-title">热门话题</div>
        <div id="hot-topics">
            <div class="empty-tip">登录后查看热门话题</div>
        </div>
    </div>
</aside>

<script>
(function() {
    var filterTags = document.querySelectorAll('.filter-tag');
    
    filterTags.forEach(function(tag) {
        tag.addEventListener('click', function() {
            filterTags.forEach(function(t) { t.classList.remove('active'); });
            this.classList.add('active');
            
            var filter = this.dataset.filter;
            window.location.href = '?filter=' + filter;
        });
    });
    
    // 关注用户
    var followBtns = document.querySelectorAll('.follow-btn');
    followBtns.forEach(function(btn) {
        btn.addEventListener('click', function() {
            var userId = this.dataset.userId;
            var btn = this;
            
            fetch('<?php echo $this->url('user/follow'); ?>', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: 'user_id=' + userId + '&csrf_token=' + '<?php echo $_SESSION['csrf_token'] ?? ''; ?>'
            }).then(function(response) {
                return response.json();
            }).then(function(data) {
                if (data.code === 0) {
                    if (btn.textContent === '关注') {
                        btn.textContent = '已关注';
                        btn.classList.add('followed');
                    } else {
                        btn.textContent = '关注';
                        btn.classList.remove('followed');
                    }
                } else {
                    alert(data.message || '操作失败');
                }
            }).catch(function() {
                alert('网络错误');
            });
        });
    });
})();
</script>