<?php
/**
 * HuSNS Mobile Theme - defaultpro
 * 首页 - 与原版保持一致
 */
?>
<div class="m-tabs">
    <a href="<?= $this->url('mobile?tab=all') ?>" class="m-tab <?= $tab === 'all' ? 'active' : '' ?>">全部</a>
    <a href="<?= $this->url('mobile?tab=following') ?>" class="m-tab <?= $tab === 'following' ? 'active' : '' ?>">关注</a>
    <?php if (!empty($circlesEnabled)): ?>
    <a href="<?= $this->url('mobile/circles') ?>" class="m-tab <?= $tab === 'circles' ? 'active' : '' ?>">圈子</a>
    <?php endif; ?>
</div>

<div class="m-post-list">
    <?php if (empty($posts)): ?>
    <div class="m-empty">
        <div class="m-empty-icon">📭</div>
        <div class="m-empty-text">暂无内容</div>
    </div>
    <?php else: ?>
    <?php foreach ($posts as $post): ?>
    <div class="m-post-item" data-id="<?= $post['id'] ?>">
        <div class="m-post-header">
            <a href="<?= $this->url('mobile/user?id=' . $post['user_id']) ?>">
                <?= $this->avatar($post['avatar'] ?? null, $post['username'] ?? '', 'small') ?>
            </a>
            <div class="m-post-user-info">
                <a href="<?= $this->url('mobile/user?id=' . $post['user_id']) ?>" class="m-post-username"><?= $this->escape($post['username']) ?></a>
                <div class="m-post-time"><?= $post['time_ago'] ?? Helper::formatTime($post['created_at']) ?></div>
            </div>
        </div>
        
        <a href="<?= $this->url('mobile/detail?id=' . $post['id']) ?>" style="text-decoration:none;color:inherit;">
            <div class="m-post-content">
                <?= $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';
            ?>
            <div class="m-post-images <?= $gridClass ?>">
                <?php foreach (array_slice($post['images'], 0, 9) as $img): ?>
                <img class="m-post-image" src="<?= $this->uploadUrl($img) ?>" alt="" loading="lazy">
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </a>
        
        <?php if (!empty($post['repost_id'])): ?>
        <div class="m-repost-preview">
            <a href="<?= $this->url('mobile/detail?id=' . $post['repost_id']) ?>" style="color:inherit;text-decoration:none;">
                @<?= $this->escape($post['repost_username'] ?? '') ?>: <?= $this->escape(mb_substr($post['repost_content'] ?? '', 0, 50, 'UTF-8')) ?>...
            </a>
        </div>
        <?php endif; ?>
        
        <div class="m-post-actions">
            <button class="m-action-btn <?= !empty($post['is_liked']) ? 'liked' : '' ?>" data-action="like" data-id="<?= $post['id'] ?>">
                <i class="m-action-icon <?= !empty($post['is_liked']) ? 'fa-solid fa-heart' : 'fa-regular fa-heart' ?>" <?= !empty($post['is_liked']) ? 'style="color:#ff4d4f;"' : '' ?>></i>
                <span class="m-action-count"><?= $post['likes'] ?></span>
            </button>
            <a href="<?= $this->url('mobile/detail?id=' . $post['id']) ?>" class="m-action-btn" data-action="comment" data-id="<?= $post['id'] ?>">
                <i class="m-action-icon fa-regular fa-comment"></i>
                <span class="m-action-count"><?= $post['comments'] ?></span>
            </a>
            <button class="m-action-btn <?= !empty($post['is_favorited']) ? 'favorited' : '' ?>" data-action="favorite" data-id="<?= $post['id'] ?>">
                <i class="m-action-icon <?= !empty($post['is_favorited']) ? 'fa-solid fa-star' : 'fa-regular fa-star' ?>" <?= !empty($post['is_favorited']) ? 'style="color:#ffad1f;"' : '' ?>></i>
            </button>
        </div>
    </div>
    <?php endforeach; ?>
    <?php endif; ?>
</div>

<?php if (count($posts) >= 15): ?>
<div class="m-load-more">
    <a href="<?= $this->url('mobile?tab=' . $tab . '&page=' . ($page + 1)) ?>" style="color:var(--primary-color);text-decoration:none;">加载更多</a>
</div>
<?php endif; ?>
