<?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 class="active"><a href="<?php echo $this->url('post/topics'); ?>">🏷️ 话题广场</a></li>
        </ul>
    </div>
</aside>

<!-- 中间内容 -->
<section class="home-main">
    <div class="page-topics">
    <h2>话题广场</h2>
    
    <!-- 搜索框 -->
    <div class="search-bar">
        <input type="text" id="topicSearch" placeholder="搜索话题..." onkeyup="searchTopics()">
        <button class="search-btn" onclick="searchTopics()">🔍</button>
    </div>
    
    <!-- 热门话题标签 -->
    <div class="topic-tags">
        <span class="tag active" data-tag="">全部</span>
        <span class="tag" data-tag="hot">🔥 热门</span>
        <span class="tag" data-tag="new">✨ 最新</span>
        <span class="tag" data-tag="follow">❤️ 已关注</span>
    </div>
    
    <!-- 话题列表 -->
    <div class="topic-list">
        <?php if (!empty($topics)): ?>
            <?php foreach ($topics as $topic): ?>
            <div class="topic-card">
                <div class="topic-header">
                    <img src="<?php echo $this->escape($topic['cover'] ?? '/templates/defaultpro/images/default_topic.png'); ?>" alt="话题封面" class="topic-cover">
                    <div class="topic-info">
                        <h3 class="topic-name">#<?php echo $this->escape($topic['name']); ?></h3>
                        <p class="topic-description"><?php echo $this->escape($topic['description'] ?? '暂无描述'); ?></p>
                        <div class="topic-stats">
                            <span class="stat"><?php echo $topic['posts_count'] ?? 0; ?> 帖子</span>
                            <span class="stat"><?php echo $topic['followers_count'] ?? 0; ?> 关注</span>
                        </div>
                    </div>
                </div>
                <div class="topic-actions">
                    <a href="<?php echo $this->url('post/topic') . '?id=' . $topic['id']; ?>" class="btn btn-outline">查看话题</a>
                    <?php if (isset($_SESSION['user_id'])): ?>
                    <button class="btn btn-primary follow-btn" data-topic-id="<?php echo $topic['id']; ?>">关注</button>
                    <?php endif; ?>
                </div>
            </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 searchTopics() {
    var keyword = document.getElementById('topicSearch').value.trim();
    var tags = document.querySelectorAll('.tag');
    var activeTag = null;
    
    tags.forEach(function(tag) {
        if (tag.classList.contains('active')) {
            activeTag = tag.dataset.tag;
        }
    });
    
    var url = '?';
    if (keyword) url += 'keyword=' + encodeURIComponent(keyword) + '&';
    if (activeTag) url += 'tag=' + activeTag + '&';
    url = url.replace(/&$/, '');
    
    window.location.href = url;
}

(function() {
    var tags = document.querySelectorAll('.tag');
    
    tags.forEach(function(tag) {
        tag.addEventListener('click', function() {
            tags.forEach(function(t) { t.classList.remove('active'); });
            this.classList.add('active');
            searchTopics();
        });
    });
    
    // 关注话题
    var followBtns = document.querySelectorAll('.follow-btn');
    followBtns.forEach(function(btn) {
        btn.addEventListener('click', function() {
            var topicId = this.dataset.topicId;
            var btn = this;
            
            fetch('<?php echo $this->url('post/followTopic'); ?>', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: 'topic_id=' + topicId + '&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.remove('btn-primary');
                        btn.classList.add('btn-outline');
                    } else {
                        btn.textContent = '关注';
                        btn.classList.remove('btn-outline');
                        btn.classList.add('btn-primary');
                    }
                } else {
                    alert(data.message || '操作失败');
                }
            }).catch(function() {
                alert('网络错误');
            });
        });
    });
})();
</script>