<?php
/**
 * HuSNS - 一款免费开源的社交平台
 *
 * @author  HYR
 * @QQ      281900864
 * @website https://huyourui.com
 * @license MIT
 * @声明    严禁用于违法违规用途
 */
?>

<!-- 主内容区 -->
<div class="main-wrapper">
    <!-- 左侧栏 -->
    <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"><?php echo $posts['pagination']['total'] ?? 0; ?></div><div class="stat-label">动态</div></div>
                <div class="stat-item"><div class="stat-value"><?php echo $followCount['following'] ?? 0; ?></div><div class="stat-label">关注</div></div>
                <div class="stat-item"><div class="stat-value"><?php echo $followCount['followers'] ?? 0; ?></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 class="active"><a href="<?php echo $this->url('user/profile'); ?>" aria-current="page">👤 我的主页</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-profile">
    <div class="profile-header">
        <div class="profile-avatar">
            <img src="/templates/defaultpro/images/avatar_default.png" alt="" style="width:100px;height:100px;border-radius:50%;object-fit:cover;">
        </div>
        <div class="profile-info">
            <h2><?php echo $this->escape($user['username']); ?></h2>
            <p class="bio"><?php echo $this->escape($user['bio'] ?: '这个人很懒，什么都没写...'); ?></p>
            <div class="profile-stats">
                <span>关注 <?php echo $followCount['following']; ?></span>
                <span>粉丝 <?php echo $followCount['followers']; ?></span>
                <span>动态 <?php echo $posts['pagination']['total'] ?? 0; ?></span>
                <span class="points-stat">积分 <?php echo isset($user['points']) ? (int)$user['points'] : 0; ?></span>
            </div>
        </div>
        <?php if (!$isSelf && isset($_SESSION['user_id'])): ?>
        <div class="profile-action">
            <form method="post" action="<?php echo $this->url('user/' . ($isFollowing ? 'unfollow' : 'follow')); ?>">
                <?php echo $this->csrf(); ?>
                <input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
                <button type="submit" class="btn <?php echo $isFollowing ? 'btn-default' : 'btn-primary'; ?>">
                    <?php echo $isFollowing ? '取消关注' : '关注'; ?>
                </button>
            </form>
        </div>
        <?php endif; ?>
    </div>

    <div class="profile-tabs">
        <a href="<?php echo $this->url('user/profile?id=' . $user['id']); ?>" class="tab-item active">动态</a>
        <?php if ($isSelf): ?>
        <a href="<?php echo $this->url('user/favorites'); ?>" class="tab-item">我的收藏</a>
        <?php endif; ?>
    </div>

    <div class="profile-posts">
        <div class="post-list">
            <?php if (empty($posts['items'])): ?>
            <div class="empty">暂无动态</div>
            <?php else: ?>
            <?php foreach ($posts['items'] as $post): ?>
            <div class="post-item" data-id="<?php echo $post['id']; ?>">
                <div class="post-avatar">
                    <a href="<?php echo $this->url('user/profile?id=' . $post['user_id']); ?>">
                        <img src="/templates/defaultpro/images/avatar_default.png" alt="" style="width:40px;height:40px;border-radius:50%;object-fit:cover;">
                    </a>
                </div>
                <div class="post-content">
                    <div class="post-header">
                        <a href="<?php echo $this->url('user/profile?id=' . $post['user_id']); ?>" class="username">
                            <?php echo $this->escape($post['username']); ?>
                        </a>
                        <?php if (!empty($post['is_featured'])): ?>
                        <span class="featured-tag">精华</span>
                        <?php endif; ?>
                        <?php if (!empty($post['repost_id'])): ?>
                        <span class="repost-label">转发</span>
                        <?php endif; ?>
                        <a href="<?php echo $this->url('post/detail?id=' . $post['id']); ?>" class="time"><?php echo $post['time_ago']; ?></a>
                    </div>

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

                    <?php if (!empty($post['original_post'])): ?>
                    <div class="repost-box">
                        <?php if (!empty($post['original_post']['deleted'])): ?>
                        <div class="repost-deleted">原文已删除</div>
                        <?php else: ?>
                        <div class="repost-header">
                            <a href="<?php echo $this->url('user/profile?id=' . $post['original_post']['user_id']); ?>" class="username">@<?php echo $this->escape($post['original_post']['username']); ?></a>
                        </div>
                        <div class="repost-content"><?php echo $post['original_post']['content']; ?></div>
                        <?php if (!empty($post['original_post']['images'])): ?>
                        <?php echo Helper::renderImageGrid($post['original_post']['images']); ?>
                        <?php endif; ?>
                        <?php if (!empty($post['original_post']['videos'])): ?>
                        <div class="post-videos">
                            <?php foreach ($post['original_post']['videos'] as $video): ?>
                            <div class="post-video-item" onclick="playVideo(this)">
                                <video preload="metadata" playsinline>
                                    <source src="<?php echo $this->uploadUrl($video['path']); ?>#t=3" type="video/<?php echo $video['ext']; ?>">
                                </video>
                                <div class="video-overlay">
                                    <div class="video-play-btn">▶</div>
                                </div>
                                <div class="video-name-tag"><?php echo $this->escape($video['name']); ?></div>
                            </div>
                            <?php endforeach; ?>
                        </div>
                        <?php endif; ?>
                        <?php if (!empty($post['original_post']['attachments'])): ?>
                        <div class="post-attachments">
                            <div class="post-attachments-title">📎 附件</div>
                            <?php foreach ($post['original_post']['attachments'] as $index => $attachment): ?>
                            <a href="<?php echo $this->url('download/attachment?id=' . $post['original_post']['id'] . '&index=' . $index); ?>" class="post-attachment-item">
                                <span class="post-attachment-icon">📄</span>
                                <span class="post-attachment-name"><?php echo $this->escape($attachment['name']); ?></span>
                                <span class="post-attachment-size"><?php echo $this->formatFileSize($attachment['size']); ?></span>
                            </a>
                            <?php endforeach; ?>
                        </div>
                        <?php endif; ?>
                        <?php endif; ?>
                    </div>
                    <?php else: ?>
                    <?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)">
                            <video preload="metadata" playsinline>
                                <source src="<?php echo $this->uploadUrl($video['path']); ?>#t=3" type="video/<?php echo $video['ext']; ?>">
                            </video>
                            <div class="video-overlay">
                                <div class="video-play-btn">▶</div>
                            </div>
                            <div class="video-name-tag"><?php echo $this->escape($video['name']); ?></div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    <?php endif; ?>
                    <?php if (!empty($post['attachments'])): ?>
                    <div class="post-attachments">
                        <div class="post-attachments-title">📎 附件</div>
                        <?php foreach ($post['attachments'] as $index => $attachment): ?>
                        <a href="<?php echo $this->url('download/attachment?id=' . $post['id'] . '&index=' . $index); ?>" class="post-attachment-item">
                            <span class="post-attachment-icon">📄</span>
                            <span class="post-attachment-name"><?php echo $this->escape($attachment['name']); ?></span>
                            <span class="post-attachment-size"><?php echo $this->formatFileSize($attachment['size']); ?></span>
                        </a>
                        <?php endforeach; ?>
                    </div>
                    <?php endif; ?>
                    <?php endif; ?>

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

        <?php if (!empty($posts['pagination']) && $posts['pagination']['totalPages'] > 1): ?>
        <div class="pagination">
            <?php if ($posts['pagination']['page'] > 1): ?>
            <a href="<?php echo $this->url('user/profile?id=' . $user['id'] . '&page=' . ($posts['pagination']['page'] - 1)); ?>" class="btn">上一页</a>
            <?php endif; ?>

            <span class="page-info">第 <?php echo $posts['pagination']['page']; ?> / <?php echo $posts['pagination']['totalPages']; ?> 页</span>

            <?php if ($posts['pagination']['page'] < $posts['pagination']['totalPages']): ?>
            <a href="<?php echo $this->url('user/profile?id=' . $user['id'] . '&page=' . ($posts['pagination']['page'] + 1)); ?>" class="btn">下一页</a>
            <?php endif; ?>
        </div>
        <?php endif; ?>
    </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>
</div>

<script>
window.addEventListener('load', function() {
    if (typeof window.initAllPostEvents === 'function') {
        window.initAllPostEvents();
    }
    if (typeof window.initRepostModal === 'function') {
        window.initRepostModal();
    }
});
</script>