<?php
/**
 * HuSNS - 帖子卡片组件（统一模板）
 * 所有页面的帖子卡片都使用此模板，确保样式和结构一致
 */
$post = isset($post) ? $post : [];
$isOwner = isset($isOwner) ? $isOwner : false;
$isAdmin = isset($isAdmin) ? $isAdmin : (isset($_SESSION['is_admin']) && $_SESSION['is_admin'] ? true : false);
$currentUserId = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
?>

<div class="moment-card" data-id="<?php echo intval($post['id'] ?? 0); ?>">
    <div class="moment-header">
        <a href="<?php echo $this->url('mobile/user?id=' . intval($post['user_id'] ?? 0)); ?>">
            <?php echo $this->avatar($post['avatar'] ?? null, $post['username'] ?? '', 'small'); ?>
        </a>
        <div class="moment-user-info">
            <a href="<?php echo $this->url('mobile/user?id=' . intval($post['user_id'] ?? 0)); ?>" class="moment-username">
                <?php echo $this->escape($post['username']); ?>
                <?php if (!empty($post['is_online'])): ?>
                <span class="online-badge" title="在线"></span>
                <?php else: ?>
                <span class="online-badge offline" title="离线"></span>
                <?php endif; ?>
                <?php if (!empty($post['is_pinned'])): ?>
                <span class="moment-badge pinned">置顶</span>
                <?php endif; ?>
                <?php if (!empty($post['is_featured'])): ?>
                <span class="moment-badge featured">精华</span>
                <?php endif; ?>
            </a>
            <div class="moment-meta">
                <div class="moment-time"><?php echo $post['time_ago'] ?? ''; ?></div>
                <?php 
                $locationName = $post['location_name'] ?? '';
                if (!empty($post['ip']) && empty($locationName)): 
                ?>
                <div class="moment-ip"><?php echo $post['ip']; ?></div>
                <?php endif; ?>
            </div>
        </div>
        <div class="moment-more-btn" onclick="HuSNS.toggleDropdown(this, event)">
            <span class="moment-more">⋮</span>
            <div class="moment-dropdown">
                <?php if (!$isOwner && $currentUserId): ?>
                <?php if ((int)Setting::get('mobile_favorite_enabled', 1) === 1): ?>
                <button class="moment-dropdown-item" onclick="HuSNS.favoritePost(<?php echo intval($post['id'] ?? 0); ?>, <?php echo !empty($post['is_favorited']) ? 'true' : 'false'; ?>)">
                    <span class="item-icon"><span class="iconify" data-icon="<?php echo !empty($post['is_favorited']) ? 'mdi:star' : 'mdi:star-outline'; ?>"></span></span>
                    <?php echo !empty($post['is_favorited']) ? '取消收藏' : '收藏'; ?>
                </button>
                <?php endif; ?>
                <?php endif; ?>
                <button class="moment-dropdown-item" onclick="HuSNS.repostPost(<?php echo intval($post['id'] ?? 0); ?>)">
                    <span class="item-icon"><span class="iconify" data-icon="mdi:repeat-variant"></span></span>转发
                </button>
                <?php if ($isOwner): ?>
                <div class="dropdown-divider"></div>
                <button class="moment-dropdown-item" onclick="HuSNS.editPost(<?php echo intval($post['id'] ?? 0); ?>)">
                    <span class="item-icon"><span class="iconify" data-icon="mdi:pencil-outline"></span></span>编辑
                </button>
                <button class="moment-dropdown-item danger" onclick="HuSNS.deletePost(<?php echo intval($post['id'] ?? 0); ?>)">
                    <span class="item-icon"><span class="iconify" data-icon="mdi:delete-outline"></span></span>删除
                </button>
                <?php endif; ?>
                <?php if ($isAdmin): ?>
                <div class="dropdown-divider"></div>
                <button class="moment-dropdown-item" onclick="HuSNS.togglePinPost(<?php echo intval($post['id'] ?? 0); ?>, <?php echo !empty($post['is_pinned']) ? '1' : '0'; ?>)">
                    <span class="item-icon"><span class="iconify" data-icon="mdi:pin"></span></span>
                    <?php echo !empty($post['is_pinned']) ? '取消置顶' : '置顶'; ?>
                </button>
                <button class="moment-dropdown-item" onclick="HuSNS.toggleFeaturePost(<?php echo intval($post['id'] ?? 0); ?>, <?php echo !empty($post['is_featured']) ? '1' : '0'; ?>)">
                    <span class="item-icon"><span class="iconify" data-icon="mdi:trophy-outline"></span></span>
                    <?php echo !empty($post['is_featured']) ? '取消加精' : '加精'; ?>
                </button>
                <?php elseif ($currentUserId && !$isOwner): ?>
                <div class="dropdown-divider"></div>
                <button class="moment-dropdown-item" onclick="HuSNS.reportPost(<?php echo intval($post['id'] ?? 0); ?>)">
                    <span class="item-icon"><span class="iconify" data-icon="mdi:flag-outline"></span></span>举报
                </button>
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <div class="moment-content-link">
        <div class="moment-content">
            <?php if (!empty($post['circle_name'])): ?>
            <span class="moment-circle">
                <a href="<?php echo $this->url('mobile/circlePosts?circle_id=' . intval($post['circle_id'] ?? 0)); ?>" class="circle-name">
                    <?php echo $this->escape($post['circle_name']); ?>
                </a>
            </span>
            <?php endif; ?>
            <?php echo $post['content']; ?>
        </div>
        
        <?php if (!empty($post['images'])): ?>
        <?php
        $images = $post['images'];
        // 如果是JSON字符串，解码为数组
        if (!is_array($images)) {
            $images = json_decode($images, true);
        }
        $images = is_array($images) ? $images : [];
        $imageCount = count($images);
        $gridClass = $imageCount === 1 ? 'single-image' : '';
        ?>
        <div class="moment-images">
            <div class="moment-image-grid <?php echo $gridClass; ?>">
                <?php foreach (array_slice($images, 0, 9) as $img): ?>
                <img 
                    class="moment-img lazy-img" 
                    data-src="<?php echo $this->uploadUrl($img); ?>" 
                    src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Crect fill='%23f2f2f7' width='100' height='100'/%3E%3C/svg%3E" 
                    alt="" 
                />
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>
        
        <?php if (!empty($post['videos'])): ?>
        <?php
        $videos = $post['videos'];
        // 如果是JSON字符串，解码为数组
        if (!is_array($videos)) {
            $videos = json_decode($videos, true);
        }
        $videos = is_array($videos) ? $videos : [];
        ?>
        <?php foreach (array_slice($videos, 0, 1) as $video): ?>
        <div class="moment-video">
            <div class="moment-video-thumbnail" 
                 data-video="<?php echo $this->uploadUrl($video['url'] ?? $video); ?>" 
                 data-duration="<?php echo intval($video['duration'] ?? 0); ?>"
                 onclick="HuSNS.openVideoPlayer('<?php echo $this->uploadUrl($video['url'] ?? $video); ?>')">
                <img class="moment-video-poster" 
                     src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Crect fill='%231a1a1a' width='100' height='100'/%3E%3C/svg%3E" 
                     alt="" />
                <div class="moment-video-play-btn">
                    <svg viewBox="0 0 24 24" fill="white">
                        <path d="M8 5v14l11-7z"/>
                    </svg>
                </div>
                <div class="moment-video-badge">视频</div>
                <div class="moment-video-duration"><?php 
                    $duration = intval($video['duration'] ?? 0);
                    $mins = floor($duration / 60);
                    $secs = $duration % 60;
                    echo sprintf('%02d:%02d', $mins, $secs);
                ?></div>
            </div>
        </div>
        <?php endforeach; ?>
        <?php endif; ?>
        
        <?php
        // 位置卡片显示 - 只有当有精确坐标时才显示位置信息
        $locationName = $post['location_name'] ?? '';
        $locationLat = $post['location_lat'] ?? '';
        $locationLng = $post['location_lng'] ?? '';
        $hasCoords = !empty($locationLat) && !empty($locationLng) && $locationLat != '0' && $locationLng != '0';

        // 只有有精确坐标时才显示位置卡片
        if (!empty($locationName) && $hasCoords):
        ?>
        <div class="moment-location-card" onclick="HuSNS.showLocationMap('<?php echo $this->escape($locationName); ?>', '<?php echo $locationLat; ?>', '<?php echo $locationLng; ?>')">
            <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($locationName); ?></span>
        </div>
        <?php endif; ?>
    </div>
    
    <?php if (!empty($post['repost_id'])): ?>
    <div class="moment-repost">
        <a href="<?php echo $this->url('mobile/detail?id=' . intval($post['repost_id'] ?? 0)); ?>" class="moment-repost-link">
            @<?php echo $this->escape($post['repost_username'] ?? ''); ?>: 
            <?php echo $this->escape(mb_substr(strip_tags($post['repost_content'] ?? ''), 0, 80, 'UTF-8')); ?>...
        </a>
    </div>
    <?php endif; ?>
    
    <div class="moment-actions">
        <button class="moment-action like-btn <?php echo !empty($post['is_liked']) ? 'liked' : ''; ?>" data-post-id="<?php echo intval($post['id'] ?? 0); ?>">
            <span class="moment-action-icon"><span class="iconify" data-icon="<?php echo !empty($post['is_liked']) ? 'mdi:heart' : 'mdi:heart-outline'; ?>"></span></span>
            <span class="moment-action-count"><?php echo $post['likes'] ?: ''; ?></span>
            <span class="moment-action-text">赞</span>
        </button>
        <button class="moment-action comment-btn" data-post-id="<?php echo intval($post['id'] ?? 0); ?>">
            <span class="moment-action-icon"><span class="iconify" data-icon="mdi:comment-outline"></span></span>
            <span class="moment-action-count"><?php echo $post['comments'] ?: ''; ?></span>
            <span class="moment-action-text">评论</span>
        </button>
        <button class="moment-action" onclick="HuSNS.openShareModal('<?php echo $this->url('mobile/detail?id=' . intval($post['id'] ?? 0)); ?>', '<?php echo $this->escape(mb_substr(strip_tags($post['content']), 0, 50, 'UTF-8')); ?>')">
            <span class="moment-action-icon"><span class="iconify" data-icon="mdi:share-outline"></span></span>
            <span class="moment-action-text">分享</span>
        </button>
        <button class="moment-action" onclick="window.location.href='<?php echo $this->url('mobile/detail?id=' . intval($post['id'] ?? 0)); ?>'">
            <span class="moment-action-icon"><span class="iconify" data-icon="mdi:eye-outline"></span></span>
            <span class="moment-action-count"><?php echo $post['views'] ?: ''; ?></span>
            <span class="moment-action-text">阅读</span>
        </button>
    </div>
</div>