<?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-points">
    <h2>积分中心</h2>
    
    <!-- 积分概览 -->
    <div class="points-summary">
        <div class="points-card">
            <div class="points-icon">⭐</div>
            <div class="points-info">
                <div class="points-value"><?php echo $user_points ?? 0; ?></div>
                <div class="points-label">当前积分</div>
            </div>
        </div>
        <div class="points-card">
            <div class="points-icon">🏆</div>
            <div class="points-info">
                <div class="points-value"><?php echo $level ?? 1; ?></div>
                <div class="points-label">当前等级</div>
            </div>
        </div>
        <div class="points-card">
            <div class="points-icon">📈</div>
            <div class="points-info">
                <div class="points-value"><?php echo $next_level_points ?? 0; ?></div>
                <div class="points-label">升级还需</div>
            </div>
        </div>
    </div>
    
    <!-- 获取积分方式 -->
    <div class="points-section">
        <h3>获取积分</h3>
        <div class="points-ways">
            <div class="way-item">
                <div class="way-icon">✍️</div>
                <div class="way-info">
                    <div class="way-title">发布动态</div>
                    <div class="way-desc">每天首次发布获得5积分</div>
                </div>
                <div class="way-points">+5</div>
            </div>
            <div class="way-item">
                <div class="way-icon">❤️</div>
                <div class="way-info">
                    <div class="way-title">点赞</div>
                    <div class="way-desc">每点赞一条动态获得1积分</div>
                </div>
                <div class="way-points">+1</div>
            </div>
            <div class="way-item">
                <div class="way-icon">💬</div>
                <div class="way-info">
                    <div class="way-title">评论</div>
                    <div class="way-desc">每条评论获得2积分</div>
                </div>
                <div class="way-points">+2</div>
            </div>
            <div class="way-item">
                <div class="way-icon">👥</div>
                <div class="way-info">
                    <div class="way-title">关注用户</div>
                    <div class="way-desc">每关注一位用户获得3积分</div>
                </div>
                <div class="way-points">+3</div>
            </div>
            <div class="way-item">
                <div class="way-icon">🎁</div>
                <div class="way-info">
                    <div class="way-title">每日签到</div>
                    <div class="way-desc">每日签到获得10积分</div>
                </div>
                <div class="way-points">+10</div>
            </div>
            <div class="way-item">
                <div class="way-icon">📸</div>
                <div class="way-info">
                    <div class="way-title">上传图片</div>
                    <div class="way-desc">发布带图片的动态获得额外3积分</div>
                </div>
                <div class="way-points">+3</div>
            </div>
        </div>
    </div>
    
    <!-- 积分记录 -->
    <div class="points-section">
        <h3>积分记录</h3>
        <div class="points-history">
            <?php if (!empty($history)): ?>
                <?php foreach ($history as $record): ?>
                <div class="history-item">
                    <div class="history-icon"><?php echo $record['type'] === 'earn' ? '+' : '-'; ?></div>
                    <div class="history-info">
                        <div class="history-title"><?php echo $this->escape($record['reason']); ?></div>
                        <div class="history-time"><?php echo $this->escape($record['time']); ?></div>
                    </div>
                    <div class="history-points <?php echo $record['type'] === 'earn' ? 'earn' : 'spend'; ?>">
                        <?php echo $record['type'] === 'earn' ? '+' : ''; ?><?php echo $record['points']; ?>
                    </div>
                </div>
                <?php endforeach; ?>
            <?php else: ?>
                <div class="empty-state">
                    <div class="empty-icon">📊</div>
                    <p>暂无积分记录</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
    
    <!-- 签到按钮 -->
    <?php if (isset($_SESSION['user_id'])): ?>
    <div class="sign-in-section">
        <button class="sign-in-btn" id="signInBtn">
            <?php echo isset($has_signed) && $has_signed ? '✅ 今日已签到' : '🎁 立即签到'; ?>
        </button>
    </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 signInBtn = document.getElementById('signInBtn');
    if (signInBtn && signInBtn.textContent.indexOf('今日已签到') === -1) {
        signInBtn.addEventListener('click', function() {
            fetch('<?php echo $this->url('user/signIn'); ?>', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: 'csrf_token=' + '<?php echo $_SESSION['csrf_token'] ?? ''; ?>'
            }).then(function(response) {
                return response.json();
            }).then(function(data) {
                if (data.code === 0) {
                    alert('签到成功！获得10积分');
                    signInBtn.textContent = '✅ 今日已签到';
                    signInBtn.disabled = true;
                } else {
                    alert(data.message || '签到失败');
                }
            }).catch(function() {
                alert('网络错误');
            });
        });
    }
})();
</script>