<?php
/**
 * HuSNS - 一款免费开源的社交平台
 * 
 * @author  HYR
 * @QQ      281900864
 * @website https://huyourui.com
 * @license MIT
 * @声明    严禁用于违法违规用途
 */
// 检查登录状态
$userId = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
if (!$userId) {
    // 使用弹窗登录，不跳转页面
    $this->assign('showLoginModal', true);
}

$currentPage = 'detail';
$pageType = 'detail';
$bodyClass = 'detail-page';
$hideTabBar = false; // 显示页脚导航
$showBack = true;
$headerTitle = '帖子详情';

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

// 将页面特定资源添加到视图数据中
$this->assign('pageCssFiles', $pageCssFiles);
$this->assign('pageJsFiles', $pageJsFiles);

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

<!-- 配置参数 -->
<script>
window.HuSNS_Detail = {
    apiUrls: {
        like: '<?php echo $this->url('mobileApi/like'); ?>',
        unlike: '<?php echo $this->url('mobileApi/unlike'); ?>',
        favorite: '<?php echo $this->url('mobileApi/favorite'); ?>',
        unfavorite: '<?php echo $this->url('mobileApi/unfavorite'); ?>',
        comment: '<?php echo $this->url('mobileApi/comment'); ?>',
        likeComment: '<?php echo $this->url('mobileApi/likeComment'); ?>',
        unlikeComment: '<?php echo $this->url('mobileApi/unlikeComment'); ?>'
    },
    postId: <?php echo !empty($post['id']) ? $post['id'] : '0'; ?>
};
</script>

<div class="detail-content">
    <?php if (!empty($post)): ?>
    <!-- 帖子详情 -->
    <div class="detail-post">
        <div class="detail-header">
            <?php if (!empty($post['is_anonymous'])): ?>
            <!-- 匿名发布：隐藏头像链接 -->
            <div class="anonymous-avatar">
                <svg viewBox="0 0 24 24">
                    <path d="M12 1L3 5v6c0 5.55 3.84 10.74 9 12 5.16-1.26 9-6.45 9-12V5l-9-4zm0 10.99h7c-.53 4.12-3.28 7.79-7 8.94V12H5V6.3l7-3.11v8.8z"/>
                </svg>
            </div>
            <div class="detail-user-info">
                <div class="detail-username" style="color: var(--text-secondary-color);"><?php echo $this->escape($post['username']); ?></div>
                <div class="detail-time"><?php echo $post['time_ago'] ?? Helper::formatTime($post['created_at']); ?></div>
            </div>
            <?php else: ?>
            <a href="<?php echo $this->url('mobile/user?id=' . intval($post['user_id'] ?? 0)); ?>">
                <?php 
                if (!empty($post['avatar'])) {
                    echo '<img class="detail-avatar" src="' . $this->uploadUrl($post['avatar']) . '" alt="' . $this->escape($post['username']) . '">';
                } else {
                    echo '<img class="detail-avatar" src="' . $this->themeUrl() . '/images/avatar_default.png" alt="' . $this->escape($post['username']) . '">';
                }
                ?>
            </a>
            <div class="detail-user-info">
                <div class="detail-username"><?php echo $this->escape($post['username']); ?></div>
                <div class="detail-time"><?php echo $post['time_ago'] ?? Helper::formatTime($post['created_at']); ?></div>
            </div>
            <?php endif; ?>
        </div>
        
        <div class="detail-content-text"><?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';
        ?>
        <div class="detail-images">
            <div class="detail-image-grid <?php echo $gridClass; ?>">
                <?php foreach ($post['images'] as $index => $img): ?>
                <img class="detail-image" 
                     src="<?php echo $this->uploadUrl($img); ?>" 
                     alt="" 
                     loading="lazy"
                     data-images='<?php echo htmlspecialchars(json_encode(array_map(function($i) { return $this->uploadUrl($i); }, $post['images'])), ENT_QUOTES); ?>'
                     data-index="<?php echo $index; ?>"
                     onclick="HuSNS.openMediaViewerFromElement(this)">
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>
        
        <?php if (!empty($post['videos']) && is_array($post['videos'])): ?>
        <?php 
            $videoUrls = [];
            foreach ($post['videos'] as $video) {
                if (is_string($video)) {
                    $videoUrls[] = $this->uploadUrl($video);
                } elseif (is_array($video) && !empty($video['file'])) {
                    $videoUrls[] = $this->uploadUrl($video['file']);
                } elseif (is_array($video) && !empty($video['path'])) {
                    $videoUrls[] = $this->uploadUrl($video['path']);
                }
            }
        ?>
        <div class="detail-videos" id="detail-videos-<?php echo $post['id']; ?>"></div>
        <script>
            document.getElementById('detail-videos-<?php echo $post['id']; ?>').innerHTML = HuSNS.createVideoHTML(<?php echo json_encode($videoUrls); ?>);
        </script>
        <?php endif; ?>
        
        <?php if (!empty($post['repost_id'])): ?>
        <div class="detail-repost">
            <a href="<?php echo $this->url('mobile/detail?id=' . intval($post['repost_id'] ?? 0)); ?>" class="detail-repost-link">
                @<?php echo $this->escape($post['repost_username'] ?? ''); ?>: <?php echo $this->escape($post['repost_content'] ?? ''); ?>
            </a>
        </div>
        <?php endif; ?>
        
        <div class="detail-actions">
            <button class="detail-action-btn <?php echo !empty($post['is_liked']) ? 'liked' : ''; ?>" data-action="like" data-id="<?php echo intval($post['id'] ?? 0); ?>">
                <svg class="detail-action-icon" width="20" height="20" viewBox="0 0 24 24" fill="<?php echo !empty($post['is_liked']) ? '#ff4757' : 'currentColor'; ?>">
                    <path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>
                </svg>
                <span class="detail-action-count"><?php echo $post['likes']; ?></span>
            </button>
            <button class="detail-action-btn comment-btn" data-post-id="<?php echo intval($post['id'] ?? 0); ?>">
                <svg class="detail-action-icon" width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
                    <path d="M21 6h-2v9H6v2c0 .55.45 1 1 1h11l4 4V7c0-.55-.45-1-1-1zm-4 6V3c0-.55-.45-1-1-1H3c-.55 0-1 .45-1 1v14l4-4h10c.55 0 1-.45 1-1z"/>
                </svg>
                <span class="detail-action-count"><?php echo $post['comments']; ?></span>
            </button>
            <button class="detail-action-btn" onclick="HuSNS.repostPost(<?php echo intval($post['id'] ?? 0); ?>)">
                <svg class="detail-action-icon" width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
                    <path d="M12 4V1L8 5l4 4V6c3.31 0 6 2.69 6 6 0 1.01-.25 1.97-.7 2.8l1.46 1.46C19.54 15.03 20 13.57 20 12c0-4.42-3.58-8-8-8zm0 14c-3.31 0-6-2.69-6-6 0-1.01.25-1.97.7-2.8L5.24 7.74C4.46 8.97 4 10.43 4 12c0 4.42 3.58 8 8 8v3l4-4-4-4v3z"/>
                </svg>
            </button>
            <button class="detail-action-btn" 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')); ?>')">
                <svg class="detail-action-icon" width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
                    <path d="M18 16.08c-.76 0-1.44.3-1.96.77L8.91 12.7c.05-.23.09-.46.09-.7s-.04-.47-.09-.7l7.05-4.11c.54.5 1.25.81 2.04.81 1.66 0 3-1.34 3-3s-1.34-3-3-3-3 1.34-3 3c0 .24.04.47.09.7L8.04 9.81C7.5 9.31 6.79 9 6 9c-1.66 0-3 1.34-3 3s1.34 3 3 3c.79 0 1.5-.31 2.04-.81l7.12 4.16c-.05.21-.08.43-.08.65 0 1.61 1.31 2.92 2.92 2.92s2.92-1.31 2.92-2.92-1.31-2.92-2.92-2.92z"/>
                </svg>
            </button>
            <button class="detail-action-btn <?php echo !empty($post['is_favorited']) ? 'favorited' : ''; ?>" data-action="favorite" data-id="<?php echo intval($post['id'] ?? 0); ?>">
                <svg class="detail-action-icon" width="20" height="20" viewBox="0 0 24 24" fill="<?php echo !empty($post['is_favorited']) ? '#ffa502' : 'currentColor'; ?>">
                    <path d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z"/>
                </svg>
            </button>
        </div>
    </div>
    
    <!-- 评论区 -->
    <div class="comments-section">
        <div class="comments-header">评论 (<?php echo $post['comments']; ?>)</div>
        
        <?php if (!empty($comments)): ?>
        <?php foreach ($comments as $comment): ?>
        <div class="comment-item">
            <a href="<?php echo $this->url('mobile/user?id=' . intval($comment['user_id'] ?? 0)); ?>">
                <?php 
                if (!empty($comment['avatar'])) {
                    echo '<img class="comment-avatar" src="' . $this->uploadUrl($comment['avatar']) . '" alt="' . $this->escape($comment['username']) . '">';
                } else {
                    echo '<img class="comment-avatar" src="' . $this->themeUrl() . '/images/avatar_default.png" alt="' . $this->escape($comment['username']) . '">';
                }
                ?>
            </a>
            <div class="comment-content">
                <div class="comment-user"><?php echo $this->escape($comment['username']); ?></div>
                <div class="comment-text"><?php echo $this->escape($comment['content']); ?></div>
                <div class="comment-time"><?php echo $comment['time_ago'] ?? Helper::formatTime($comment['created_at']); ?></div>
                <div class="comment-actions">
                    <span class="comment-action like-action <?php echo !empty($comment['is_liked']) ? 'liked' : ''; ?>" onclick="toggleDetailCommentLike(this, <?php echo intval($comment['id'] ?? 0); ?>)">
                    <svg width="14" height="14" viewBox="0 0 24 24" fill="<?php echo !empty($comment['is_liked']) ? '#ff4757' : 'currentColor'; ?>">
                        <path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>
                    </svg>
                    <span class="like-count"><?php echo $comment['likes']; ?></span>
                </span>
                <span class="comment-action" onclick="replyComment(<?php echo intval($comment['id'] ?? 0); ?>, '<?php echo $this->escape($comment['username']); ?>')">回复</span>
                </div>
                
                <!-- 子评论（楼中楼） -->
                <?php if (!empty($comment['replies'])): ?>
                <div style="background-color: var(--background-color); border-radius: 8px; padding: 12px; margin-top: 12px;">
                    <?php foreach ($comment['replies'] as $reply): ?>
                    <div style="display: flex; margin-bottom: 12px;">
                        <a href="<?php echo $this->url('mobile/user?id=' . intval($reply['user_id'] ?? 0)); ?>" style="flex-shrink: 0; margin-right: 8px;">
                            <?php 
                            if (!empty($reply['avatar'])) {
                                echo '<img class="comment-avatar" style="width: 28px; height: 28px;" src="' . $this->uploadUrl($reply['avatar']) . '" alt="' . $this->escape($reply['username']) . '">';
                            } else {
                                echo '<img class="comment-avatar" style="width: 28px; height: 28px;" src="' . $this->themeUrl() . '/images/avatar_default.png" alt="' . $this->escape($reply['username']) . '">';
                            }
                            ?>
                        </a>
                        <div style="flex: 1;">
                            <div style="font-size: 13px; color: var(--text-color);">
                                <span style="font-weight: 600;"><?php echo $this->escape($reply['username']); ?></span>
                                <span style="color: var(--text-secondary-color);"> 回复 </span>
                                <span style="color: var(--primary-color);">@<?php echo $this->escape($reply['reply_to_username'] ?? $comment['username']); ?></span>
                                <span style="margin-left: 8px;"><?php echo $this->escape($reply['content']); ?></span>
                            </div>
                            <div style="display: flex; align-items: center; gap: 12px; margin-top: 4px;">
                                <span style="font-size: 11px; color: var(--text-secondary-color);"><?php echo $reply['time_ago'] ?? Helper::formatTime($reply['created_at']); ?></span>
                                <span style="font-size: 11px; color: var(--text-secondary-color); cursor: pointer;" class="like-action <?php echo !empty($reply['is_liked']) ? 'liked' : ''; ?>" onclick="toggleDetailCommentLike(this, <?php echo intval($reply['id'] ?? 0); ?>)">
                                    <svg width="12" height="12" viewBox="0 0 24 24" fill="<?php echo !empty($reply['is_liked']) ? '#ff4757' : 'currentColor'; ?>">
                                        <path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>
                                    </svg>
                                    <span class="like-count"><?php echo $reply['likes']; ?></span>
                                </span>
                                <span style="font-size: 11px; color: var(--text-secondary-color); cursor: pointer;" onclick="replyComment(<?php echo intval($reply['id'] ?? 0); ?>, '<?php echo $this->escape($reply['username']); ?>')">回复</span>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                    <?php if (count($comment['replies']) >= 3): ?>
                    <div style="text-align: center; padding-top: 8px; border-top: 1px solid var(--border-color);">
                        <span style="font-size: 13px; color: var(--primary-color); cursor: pointer;" onclick="loadMoreReplies(<?php echo intval($comment['id'] ?? 0); ?>)">展开更多回复</span>
                    </div>
                    <?php endif; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <?php endforeach; ?>
        <?php else: ?>
        <div class="empty-comments">
            暂无评论，快来抢沙发吧！
        </div>
        <?php endif; ?>
    </div>
    <?php else: ?>
    <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 endif; ?>
    
    <!-- 评论输入 -->
    <?php if (!empty($post)): ?>
    <div class="comment-input-section">
        <input type="text" class="comment-input" id="commentInput" placeholder="写下你的评论...">
        <button class="comment-submit" id="commentSubmit" onclick="submitComment()">发送</button>
    </div>
    <?php endif; ?>
</div>