<?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 class="active"><a href="<?php echo $this->url('post/hot'); ?>" aria-current="page">🔥 热门</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-hot">
    <?php if (empty($posts)): ?>
    <div class="post" style="text-align:center;padding:60px 20px;color:#999;">
        <p style="font-size:48px;margin-bottom:16px;">🔥</p>
        <p style="font-size:16px;">暂无热门动态</p>
    </div>
    <?php else: ?>
    <?php foreach ($posts as $post): ?>
    <div class="post" data-id="<?php echo $post['id']; ?>">
        <div class="post-header">
            <a href="<?php echo $this->url('user/profile?id=' . $post['user_id']); ?>">
                <?php echo $this->avatar($post['avatar'] ?? null, $post['username'], 40); ?>
            </a>
            <div>
                <a href="<?php echo $this->url('user/profile?id=' . $post['user_id']); ?>" class="post-user"><?php echo $this->escape($post['username']); ?></a>
                <?php if (!empty($post['is_featured'])): ?>
                <span class="featured-tag">精华</span>
                <?php endif; ?>
            </div>
            <div style="margin-left:auto;display:flex;align-items:center;gap:10px;">
                <span style="color:#ff6b6b;font-size:13px;">🔥 <?php echo $post['hot_score']; ?></span>
                <span class="post-time"><?php echo $post['time_ago']; ?></span>
            </div>
        </div>

        <?php if (!empty($post['content'])): ?>
        <div class="post-content"><?php echo $post['content']; ?></div>
        <?php endif; ?>

        <?php if (!empty($post['images'])): ?>
        <?php echo Helper::renderImageGrid($post['images']); ?>
        <?php endif; ?>

        <?php if (!empty($post['videos'])): ?>
        <div class="post-videos">
            <?php foreach ($post['videos'] as $video): ?>
            <div class="post-video-item" onclick="playVideo(this)" style="position:relative;border-radius:12px;overflow:hidden;background:#000;max-width:400px;">
                <video preload="metadata" playsinline style="width:100%;">
                    <source src="<?php echo $this->uploadUrl($video['path']); ?>#t=3" type="video/<?php echo $video['ext']; ?>">
                </video>
                <div class="video-overlay" style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);width:50px;height:50px;background:rgba(255,255,255,0.9);border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:20px;">▶</div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>

        <?php if (!empty($post['attachments'])): ?>
        <div style="margin-top:12px;">
            <?php foreach ($post['attachments'] as $attachment): ?>
            <a href="<?php echo $this->url('download/file?id=' . $post['id'] . '&file=' . urlencode($attachment['path'])); ?>" style="display:flex;align-items:center;gap:10px;padding:12px;background:#f5f7fa;border-radius:8px;margin-bottom:8px;">
                <span style="font-size:24px;">📎</span>
                <span style="flex:1;font-size:14px;"><?php echo $this->escape($attachment['name']); ?></span>
                <span style="font-size:12px;color:#999;"><?php echo $this->formatFileSize($attachment['size']); ?></span>
            </a>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>

        <?php echo $this->partial('partials/post-actions', ['post' => $post]); ?>
        <?php echo CommentHelper::renderCommentBox($post['id']); ?>
    </div>
    <?php endforeach; ?>

    <?php if ($totalPages > 1): ?>
    <div style="display:flex;justify-content:center;gap:10px;padding:20px 0;">
        <?php if ($page > 1): ?>
        <a href="<?php echo $this->url('post/hot?page=' . ($page - 1)); ?>" class="btn" style="padding:8px 16px;background:#fff;color:#1677ff;border:1px solid #1677ff;border-radius:6px;">上一页</a>
        <?php endif; ?>

        <span style="padding:8px 16px;color:#999;"><?php echo $page; ?> / <?php echo $totalPages; ?></span>

        <?php if ($page < $totalPages): ?>
        <a href="<?php echo $this->url('post/hot?page=' . ($page + 1)); ?>" class="btn" style="padding:8px 16px;background:#1677ff;color:#fff;border-radius:6px;">下一页</a>
        <?php endif; ?>
    </div>
    <?php endif; ?>
    <?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>