<?php
/**
 * HuSNS - 一款免费开源的社交平台
 * 
 * @author  HYR
 * @QQ      281900864
 * @website https://huyourui.com
 * @license MIT
 * @声明    严禁用于违法违规用途
 */
$currentPage = 'home';
$pageType = 'home';
$bodyClass = 'home-page';
$showTabbar = true;
$tab = isset($tab) ? $tab : 'all';
$userId = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;

// 页面特定CSS和JS文件
$pageCssFiles = [$this->themeUrl() . '/app/css/index.css'];
$pageJsFiles = [$this->themeUrl() . '/app/js/index.js'];

$this->setLayout('app');
?>

<script>
window.HuSNS_Index = {
    apiUrls: {
        like: '<?php echo $this->url('mobileApi/like'); ?>',
        unlike: '<?php echo $this->url('mobileApi/unlike'); ?>',
        setTop: '<?php echo $this->url('mobileApi/setTop'); ?>',
        setEssence: '<?php echo $this->url('mobileApi/setEssence'); ?>',
        deletePost: '<?php echo $this->url('mobileApi/deletePost'); ?>'
    }
};
</script>

<div class="home-content">
    <!-- 标签栏 -->
    <div class="tab-container">
        <div class="tab-bar">
            <a href="<?php echo $this->url('mobile?tab=all'); ?>" class="tab-item <?php echo $tab === 'all' ? 'active' : ''; ?>" data-tab="all">全部</a>
            <a href="<?php echo $this->url('mobile?tab=following'); ?>" class="tab-item <?php echo $tab === 'following' ? 'active' : ''; ?>" data-tab="following">关注</a>
        </div>
    </div>
    
    <!-- 下拉刷新 -->
    <div class="pull-refresh" id="pullRefresh">
        <span id="refreshText">下拉刷新</span>
    </div>
    
    <!-- 帖子列表 -->
    <div class="post-list" id="postList">
        <?php if (empty($posts)): ?>
        <div class="empty-state">
            <div class="empty-icon"><span class="iconify" data-icon="mdi:email-open-outline"></span></div>
            <div class="empty-text">暂无内容</div>
            <div class="empty-subtext">关注更多用户，发现精彩内容</div>
        </div>
        <?php else: ?>
        <?php foreach ($posts as $post): ?>
        <div class="post-item" data-id="<?php echo $post['id']; ?>">
            <div class="post-header">
                <a href="<?php echo $this->url('mobile/user?id=' . $post['user_id']); ?>">
                    <?php echo $this->avatar($post['avatar'] ?? null, $post['username'] ?? '', 'small'); ?>
                </a>
                <div class="post-user-info">
                    <div class="post-username"><?php echo $this->escape($post['username']); ?></div>
                    <div class="post-time">
                        <?php echo $post['time_ago'] ?? Helper::formatTime($post['created_at']); ?>
                    </div>
                </div>
            </div>
            
            <a href="<?php echo $this->url('mobile/detail?id=' . $post['id']); ?>" style="text-decoration:none;color:inherit;">
                <div class="post-content">
                    <?php echo $post['content']; ?>
                </div>
                
                <?php if (!empty($post['images'])): ?>
                <?php
                $imageCount = count($post['images']);
                $gridClass = 'single';
                if ($imageCount === 2) $gridClass = 'double';
                elseif ($imageCount === 3) $gridClass = 'triple';
                elseif ($imageCount === 4) $gridClass = 'quad';
                elseif ($imageCount > 4) $gridClass = 'many';
                $imagesJson = htmlspecialchars(json_encode(array_map(function($img) { return $this->uploadUrl($img); }, $post['images'])), ENT_QUOTES, 'UTF-8');
                ?>
                <div class="post-images">
                    <div class="image-grid <?php echo $gridClass; ?>">
                        <?php foreach (array_slice($post['images'], 0, 9) as $index => $img): ?>
                        <img class="post-image" src="<?php echo $this->uploadUrl($img); ?>" alt="" loading="lazy" onclick="openImageViewer(<?php echo $imagesJson; ?>, <?php echo $index; ?>)">
                        <?php endforeach; ?>
                    </div>
                </div>
                <?php endif; ?>
                
                <?php if (!empty($post['videos']) && is_array($post['videos'])): ?>
                <div class="post-video-thumbnail" onclick="HuSNS.openVideoPlayer('<?php echo $this->uploadUrl($post['videos'][0]); ?>')" data-video="<?php echo $this->uploadUrl($post['videos'][0]); ?>">
                    <img class="post-video-poster" src="" alt="">
                    <div class="post-video-play-btn">
                        <span class="iconify" data-icon="mdi:play" style="color: white; font-size: 24px;"></span>
                    </div>
                    <div class="post-video-badge">视频</div>
                </div>
                <?php endif; ?>
                
                <?php
                $hasValidLat = isset($post['location_lat']) && $post['location_lat'] !== null && $post['location_lat'] !== '' && $post['location_lat'] !== 'null' && $post['location_lat'] !== '0';
                $hasValidLng = isset($post['location_lng']) && $post['location_lng'] !== null && $post['location_lng'] !== '' && $post['location_lng'] !== 'null' && $post['location_lng'] !== '0';
                $hasValidName = !empty($post['location_name']);

                if ($hasValidName):
                    if ($hasValidLat && $hasValidLng):
                ?>
                <div class="moment-location-card" onclick="HuSNS.showLocationMap('<?php echo $this->escape($post['location_name']); ?>', '<?php echo $post['location_lat']; ?>', '<?php echo $post['location_lng']; ?>')">
                    <span class="location-pin">
                        <svg viewBox="0 0 24 24">
                            <path d="M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5c-1.38 0-2.5-1.12-2.5-2.5s1.12-2.5 2.5-2.5 2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z"/>
                        </svg>
                    </span>
                    <span class="location-text"><?php echo $this->escape($post['location_name']); ?></span>
                </div>
                <?php else: ?>
                <a class="moment-location-link" href="<?php echo $this->url('mobile/hot?location=' . urlencode($post['location_name'])); ?>">
                    <span class="location-pin">
                        <svg viewBox="0 0 24 24">
                            <path d="M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5c-1.38 0-2.5-1.12-2.5-2.5s1.12-2.5 2.5-2.5 2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z"/>
                        </svg>
                    </span>
                    <span class="location-text"><?php echo $this->escape($post['location_name']); ?></span>
                </a>
                <?php
                    endif;
                endif;
                ?>
            </a>
            
            <?php if (!empty($post['repost_id'])): ?>
            <div class="repost-preview">
                <a href="<?php echo $this->url('mobile/detail?id=' . $post['repost_id']); ?>" class="repost-link">
                    @<?php echo $this->escape($post['repost_username'] ?? ''); ?>: <?php echo $this->escape(mb_substr($post['repost_content'] ?? '', 0, 50, 'UTF-8')); ?>...
                </a>
            </div>
            <?php endif; ?>
            
            <div class="post-actions" style="position:relative;">
                <button class="action-btn <?php echo !empty($post['is_liked']) ? 'liked' : ''; ?>" data-action="like" data-id="<?php echo $post['id']; ?>">
                    <span class="action-icon"><span class="iconify" data-icon="<?php echo !empty($post['is_liked']) ? 'mdi:heart' : 'mdi:heart-outline'; ?>"></span></span>
                    <span class="action-count"><?php echo $post['likes']; ?></span>
                </button>
                <button class="action-btn" onclick="openCommentModal(<?php echo $post['id']; ?>)">
                    <span class="action-icon"><span class="iconify" data-icon="mdi:comment-outline"></span></span>
                    <span class="action-count"><?php echo $post['comments']; ?></span>
                </button>
                <button class="action-btn" onclick="openShareModal('<?php echo $this->url('mobile/detail?id=' . $post['id']); ?>', '<?php echo $this->escape(mb_substr($post['content'], 0, 50, 'UTF-8')); ?>')">
                    <span class="action-icon"><span class="iconify" data-icon="mdi:share-outline"></span></span>
                </button>
                <button class="action-btn <?php echo !empty($post['is_favorited']) ? 'favorited' : ''; ?>" data-action="favorite" data-id="<?php echo $post['id']; ?>">
                    <span class="action-icon"><span class="iconify" data-icon="<?php echo !empty($post['is_favorited']) ? 'mdi:star' : 'mdi:star-outline'; ?>"></span></span>
                </button>
                <button class="more-menu-btn" onclick="toggleMoreMenu(this, <?php echo $post['id']; ?>)">
                    <span class="iconify" data-icon="mdi:dots-vertical"></span>
                </button>
                <div class="more-menu-dropdown">
                    <?php if (!empty($_SESSION['is_admin'])): ?>
                    <button class="more-menu-item topped" onclick="handlePostAction('top', <?php echo $post['id']; ?>, this)">
                        <span class="iconify" data-icon="mdi:pin"></span> <?php echo !empty($post['is_top']) ? '取消置顶' : '置顶'; ?>
                    </button>
                    <button class="more-menu-item essenced" onclick="handlePostAction('essence', <?php echo $post['id']; ?>, this)">
                        <span class="iconify" data-icon="mdi:diamond-stone"></span> <?php echo !empty($post['is_essence']) ? '取消加精' : '加精'; ?>
                    </button>
                    <button class="more-menu-item danger" onclick="handlePostAction('delete', <?php echo $post['id']; ?>, this)">
                        <span class="iconify" data-icon="mdi:delete"></span> 删除
                    </button>
                    <?php else: ?>
                    <button class="more-menu-item" onclick="handlePostAction('report', <?php echo $post['id']; ?>, this)">
                        <span class="iconify" data-icon="mdi:flag"></span> 举报
                    </button>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?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>