<?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 class="active"><a href="<?php echo $this->url('user/messages'); ?>" aria-current="page">💬 消息</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-messages">
    <h2>消息中心</h2>
    
    <!-- 标签页切换 -->
    <div class="tabs">
        <button class="tab-btn active" data-tab="notification">🔔 通知</button>
        <button class="tab-btn" data-tab="private">💬 私信</button>
    </div>
    
    <!-- 通知列表 -->
    <div class="tab-content active" id="notification-tab">
        <div class="message-list">
            <?php if (!empty($notifications)): ?>
                <?php foreach ($notifications as $notification): ?>
                <div class="message-item">
                    <img src="<?php echo $this->escape($notification['avatar'] ?? '/templates/defaultpro/images/avatar_default.png'); ?>" alt="头像" class="avatar">
                    <div class="message-content">
                        <div class="message-header">
                            <span class="sender-name"><?php echo $this->escape($notification['username']); ?></span>
                            <span class="message-time"><?php echo $this->escape($notification['time']); ?></span>
                        </div>
                        <p class="message-text"><?php echo $this->escape($notification['content']); ?></p>
                    </div>
                </div>
                <?php endforeach; ?>
            <?php else: ?>
                <div class="empty-state">
                    <div class="empty-icon">🔔</div>
                    <p>暂无通知</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
    
    <!-- 私信列表 -->
    <div class="tab-content" id="private-tab">
        <div class="message-list">
            <?php if (!empty($conversations)): ?>
                <?php foreach ($conversations as $conv): ?>
                <div class="message-item conversation-item">
                    <img src="<?php echo $this->escape($conv['avatar'] ?? '/templates/defaultpro/images/avatar_default.png'); ?>" alt="头像" class="avatar">
                    <div class="message-content">
                        <div class="message-header">
                            <span class="sender-name"><?php echo $this->escape($conv['username']); ?></span>
                            <span class="message-time"><?php echo $this->escape($conv['time']); ?></span>
                        </div>
                        <p class="message-text"><?php echo $this->escape($conv['last_message']); ?></p>
                        <?php if ($conv['unread'] > 0): ?>
                        <span class="unread-badge"><?php echo $conv['unread']; ?></span>
                        <?php endif; ?>
                    </div>
                </div>
                <?php endforeach; ?>
            <?php else: ?>
                <div class="empty-state">
                    <div class="empty-icon">💬</div>
                    <p>暂无私信</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
    </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 tabButtons = document.querySelectorAll('.tab-btn');
    var tabContents = document.querySelectorAll('.tab-content');
    
    tabButtons.forEach(function(btn) {
        btn.addEventListener('click', function() {
            var tabId = this.dataset.tab;
            
            // 切换按钮状态
            tabButtons.forEach(function(b) { b.classList.remove('active'); });
            this.classList.add('active');
            
            // 切换内容显示
            tabContents.forEach(function(content) {
                content.classList.remove('active');
            });
            document.getElementById(tabId + '-tab').classList.add('active');
        });
    });
})();
</script>