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

$userId = isset($user['id']) ? $user['id'] : 0;
$username = isset($user['username']) ? $user['username'] : '';
$avatar = isset($user['avatar']) ? $user['avatar'] : null;
$bio = isset($user['bio']) ? $user['bio'] : '';
$points = isset($user['points']) ? (int)$user['points'] : 0;
?>

<!-- 主内容区 -->
<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">-</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><a href="<?php echo $this->url('user/profile'); ?>">👤 我的主页</a></li>
        <li class="active"><a href="<?php echo $this->url('user/favorites'); ?>" 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">
                <?php echo $this->avatar($avatar, $username, 'large'); ?>
            </div>
            <div class="profile-info">
                <h2><?php echo $this->escape($username); ?></h2>
                <p class="bio"><?php echo $this->escape($bio ?: '这个人很懒，什么都没写...'); ?></p>
                <div class="profile-stats">
                    <span>关注 <?php echo $followCount['following'] ?? 0; ?></span>
                    <span>粉丝 <?php echo $followCount['followers'] ?? 0; ?></span>
                    <span>动态 <?php echo $userId ? (new PostModel())->countUserPosts($userId) : 0; ?></span>
                    <span class="points-stat"><?php echo $this->escape(Setting::getPointName()); ?> <?php echo $points; ?></span>
                </div>
            </div>
        </div>

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

        <div class="profile-favorites">
            <?php if (empty($favorites)): ?>
            <div class="empty">暂无收藏</div>
            <?php else: ?>
            <div class="post-list">
                <?php foreach ($favorites as $post): ?>
                <?php
                $postId = isset($post['id']) ? $post['id'] : 0;
                $postUserId = isset($post['user_id']) ? $post['user_id'] : 0;
                $postUsername = isset($post['username']) ? $post['username'] : '';
                $postAvatar = isset($post['avatar']) ? $post['avatar'] : null;
                $postContent = isset($post['content']) ? $post['content'] : '';
                $postTimeAgo = isset($post['time_ago']) ? $post['time_ago'] : '';
                $postFavoritedTime = isset($post['favorited_time_ago']) ? $post['favorited_time_ago'] : '';
                $postReposts = isset($post['reposts']) ? $post['reposts'] : 0;
                $postComments = isset($post['comments']) ? $post['comments'] : 0;
                $postLikes = isset($post['likes']) ? $post['likes'] : 0;
                $postImages = isset($post['images']) ? $post['images'] : [];
                $postVideos = isset($post['videos']) ? $post['videos'] : [];
                ?>
                <div class="post-item" data-id="<?php echo $postId; ?>">
                    <div class="post-avatar">
                        <a href="<?php echo $this->url('user/profile?id=' . $postUserId); ?>">
                            <?php echo $this->avatar($postAvatar, $postUsername); ?>
                        </a>
                    </div>
                    <div class="post-content">
                        <div class="post-header">
                            <a href="<?php echo $this->url('user/profile?id=' . $postUserId); ?>" class="username">
                                <?php echo $this->escape($postUsername); ?>
                            </a>
                            <a href="<?php echo $this->url('post/detail?id=' . $postId); ?>" class="time"><?php echo $postTimeAgo; ?></a>
                            <span class="favorite-time">收藏于 <?php echo $postFavoritedTime; ?></span>
                        </div>

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

                        <?php if (!empty($postImages)): ?>
                        <?php echo Helper::renderImageGrid($postImages); ?>
                        <?php endif; ?>

                        <?php if (!empty($postVideos)): ?>
                        <div class="post-videos">
                            <?php foreach ($postVideos as $video): ?>
                            <?php
                            $videoPath = isset($video['path']) ? $video['path'] : '';
                            $videoExt = isset($video['ext']) ? $video['ext'] : '';
                            $videoName = isset($video['name']) ? $video['name'] : '';
                            ?>
                            <div class="post-video-item" onclick="playVideo(this)">
                                <video preload="metadata" playsinline>
                                    <source src="<?php echo $this->uploadUrl($videoPath); ?>#t=3" type="video/<?php echo $videoExt; ?>">
                                </video>
                                <div class="video-overlay">
                                    <div class="video-play-btn">▶</div>
                                </div>
                                <div class="video-name-tag"><?php echo $this->escape($videoName); ?></div>
                            </div>
                            <?php endforeach; ?>
                        </div>
                        <?php endif; ?>

                        <div class="post-actions">
                            <span class="action-btn repost-btn" data-id="<?php echo $postId; ?>" data-reposts="<?php echo $postReposts; ?>">转发(<?php echo $postReposts; ?>)</span>
                            <span class="action-btn comment-toggle" data-id="<?php echo $postId; ?>" data-comments="<?php echo $postComments; ?>">评论(<?php echo $postComments; ?>)</span>
                            <span class="action-btn like-btn" data-id="<?php echo $postId; ?>">点赞(<?php echo $postLikes; ?>)</span>
                            <span class="action-btn favorite-btn favorited" data-id="<?php echo $postId; ?>" data-favorited="1">已收藏</span>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>

            <?php if ($totalPages > 1): ?>
            <div class="pagination">
                <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                <a href="?r=user/favorites&page=<?php echo $i; ?>" class="<?php echo $i == $page ? 'active' : ''; ?>">
                    <?php echo $i; ?>
                </a>
                <?php endfor; ?>
            </div>
            <?php endif; ?>
            <?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>