<?php
/**
 * HuSNS - 一款免费开源的社交平台
 * 
 * @author  HYR
 * @QQ      281900864
 * @website https://huyourui.com
 * @license MIT
 * @声明    禁止用于违法违纪用途
 */
$currentPage = 'home';
$pageType = 'home';
$bodyClass = 'home-page';
$tab = isset($tab) ? $tab : 'all';
$page = isset($page) ? $page : 1;
$posts = isset($posts) ? $posts : [];
$userId = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
$this->setLayout('app');
?>
<!-- 首页样式 -->
<link rel="stylesheet" href="<?php echo $this->themeUrl(); ?>/app/css/styles.css">

<div class="home-content">
    <!-- 标签栏 -->
    <div class="tab-bar" id="homeTabBar">
        <a class="tab-item <?php echo $tab === 'all' ? 'active' : ''; ?>" data-tab="all" onclick="switchTab('all')">全部</a>
        <a class="tab-item <?php echo $tab === 'following' ? 'active' : ''; ?>" data-tab="following" onclick="switchTab('following')">关注</a>
        <?php if (!empty($circlesEnabled)): ?>
        <a class="tab-item <?php echo $tab === 'circles' ? 'active' : ''; ?>" data-tab="circles" onclick="switchTab('circles')">圈子</a>
        <?php endif; ?>
    </div>
    <!-- 朋友圈卡片动态列表 -->
    <div class="post-list" id="postList">
        <?php if (empty($posts)): ?>
        <div class="loading-state">
            <div class="loading-spinner"></div>
            <div class="loading-text">加载中...</div>
        </div>
        <?php else: ?>
        <?php foreach ($posts as $post): ?>
        <?php 
            $isOwner = $userId && $userId == ($post['user_id'] ?? 0);
            include $this->themePath() . '/components/post-card.php';
        ?>
        <?php endforeach; ?>
        <?php endif; ?>
    </div>
    
    <!-- 加载更多 -->
    <?php if (count($posts) >= 15): ?>
    <div class="load-more">
        <button class="load-more-btn" id="loadMoreBtn" onclick="loadMore('<?php echo $this->url('mobile?tab=' . $tab . '&page=' . ($page + 1)); ?>')">加载更多</button>
    </div>
    <?php endif; ?>
</div>

<!-- JS模块 -->
<script src="<?php echo $this->themeUrl(); ?>/app/js/utils.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/app/js/post-card.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/app/js/post-actions.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/app/js/location-modal.js"></script>
<script src="<?php echo $this->themeUrl(); ?>/app/js/feed.js"></script>

<!-- 媒体查看器（整合图片和视频） -->
<script src="<?php echo $this->themeUrl(); ?>/app/js/media-viewer.js"></script>

<script>
    // 全局变量 - 供模块使用
    var IS_ADMIN = <?php echo !empty($_SESSION['is_admin']) ? 'true' : 'false'; ?>;
    var POSTS_COUNT = <?php echo count($posts); ?>;
    var API_BASE_URL = '<?php echo Helper::getBaseUrl(); ?>';
    var THEME_URL = '<?php echo $this->themeUrl(); ?>';
    
    // Feed状态 - PHP传入
    if (typeof HuSNS !== 'undefined') {
        HuSNS.feedState = {
            currentTab: '<?php echo $tab; ?>',
            isLoading: false,
            currentPage: <?php echo $page; ?>
        };
    }
    
    // 测试点赞函数
    function testLike(btn, e) {
        console.log('testLike 被调用');
        alert('点赞按钮被点击！');
        if (typeof HuSNS !== 'undefined' && typeof HuSNS.handleLike === 'function') {
            HuSNS.handleLike(btn, e);
        } else {
            console.error('HuSNS.handleLike 未定义');
        }
    }
</script>