<?php
/**
 * HuSNS - 一款免费开源的社交平台
 * 
 * @author  HYR
 * @QQ      281900864
 * @website https://huyourui.com
 * @license MIT
 * @声明    严禁用于违法违规用途
 */
?>
<div class="page-detail" style="max-width:700px;margin:0 auto;">
    <div class="post" data-id="<?php echo $post['id']; ?>">
        <div class="post-header">
            <a href="<?php echo $this->url('user/profile?id=' . $post['user_id']); ?>">
                <?php echo $this->avatar($post['avatar'] ?? null, $post['username'], 50); ?>
            </a>
            <div>
                <a href="<?php echo $this->url('user/profile?id=' . $post['user_id']); ?>" class="post-user" style="font-size:16px;"><?php echo $this->escape($post['username']); ?></a>
                <?php if (!empty($post['repost_id'])): ?>
                <span style="color:#999;font-size:13px;margin-left:8px;">转发</span>
                <?php endif; ?>
            </div>
            <div class="post-time"><?php echo $post['time_ago']; ?></div>
        </div>
        
        <?php if (!empty($post['content'])): ?>
        <div class="post-content" style="margin:16px 0;font-size:15px;line-height:1.8;"><?php echo $post['formatted_content']; ?></div>
        <?php endif; ?>
        
        <?php if (!empty($post['original_post'])): ?>
        <div style="background:#f5f7fa;border-radius:12px;padding:16px;margin:12px 0;">
            <?php if (!empty($post['original_post']['deleted'])): ?>
            <div style="color:#999;">原文已删除</div>
            <?php else: ?>
            <div style="margin-bottom:8px;">
                <a href="<?php echo $this->url('user/profile?id=' . $post['original_post']['user_id']); ?>" style="color:#1677ff;font-weight:500;">@<?php echo $this->escape($post['original_post']['username']); ?></a>
            </div>
            <div><?php echo $post['original_post']['content']; ?></div>
            <?php if (!empty($post['original_post']['images'])): ?>
            <?php echo Helper::renderImageGrid($post['original_post']['images']); ?>
            <?php endif; ?>
            <?php if (!empty($post['original_post']['attachments'])): ?>
            <div style="margin-top:12px;">
                <div style="font-size:13px;color:#666;margin-bottom:6px;">📎 附件</div>
                <?php foreach ($post['original_post']['attachments'] as $index => $attachment): ?>
                <a href="<?php echo $this->url('download/attachment?id=' . $post['original_post']['id'] . '&index=' . $index); ?>" style="display:flex;align-items:center;gap:8px;padding:8px;background:#fff;border-radius:6px;margin-bottom:6px;">
                    <span style="font-size:20px;">📄</span>
                    <span style="flex:1;font-size:14px;"><?php echo $this->escape($attachment['name']); ?></span>
                    <span style="font-size:12px;color:#999;"><?php echo $this->formatFileSize($attachment['size']); ?></span>
                </a>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
            <?php endif; ?>
        </div>
        <?php elseif (empty($post['repost_id'])): ?>
        <?php if (!empty($post['images'])): ?>
        <?php echo Helper::renderImageGrid($post['images']); ?>
        <?php endif; ?>
        <?php if (!empty($post['videos'])): ?>
        <div class="post-videos">
            <?php foreach ($post['videos'] as $video): ?>
            <div class="post-video-item" onclick="playVideo(this)" style="position:relative;border-radius:12px;overflow:hidden;background:#000;max-width:500px;">
                <video preload="metadata" playsinline style="width:100%;">
                    <source src="<?php echo $this->uploadUrl($video['path']); ?>#t=3" type="video/<?php echo $video['ext']; ?>">
                </video>
                <div class="video-overlay" style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);width:60px;height:60px;background:rgba(255,255,255,0.9);border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:24px;">▶</div>
                <div class="video-name-tag" style="position:absolute;bottom:0;left:0;right:0;padding:8px 12px;background:linear-gradient(transparent,rgba(0,0,0,0.7));color:#fff;font-size:13px;"><?php echo $this->escape($video['name']); ?></div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
        <?php if (!empty($post['attachments'])): ?>
        <div style="margin-top:12px;">
            <div style="font-size:13px;color:#666;margin-bottom:6px;">📎 附件</div>
            <?php foreach ($post['attachments'] as $index => $attachment): ?>
            <a href="<?php echo $this->url('download/attachment?id=' . $post['id'] . '&index=' . $index); ?>" style="display:flex;align-items:center;gap:8px;padding:12px;background:#f5f7fa;border-radius:8px;margin-bottom:8px;">
                <span style="font-size:24px;">📄</span>
                <span style="flex:1;font-size:14px;"><?php echo $this->escape($attachment['name']); ?></span>
                <span style="font-size:12px;color:#999;"><?php echo $this->formatFileSize($attachment['size']); ?></span>
            </a>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
        <?php endif; ?>
        
        <?php echo $this->partial('partials/post-actions', ['post' => $post]); ?>
        
        <?php if (!empty($post['edit_count']) && $post['edit_count'] > 0): ?>
        <div style="font-size:12px;color:#999;margin-top:12px;padding-top:12px;border-top:1px solid #f0f0f0;">
            已编辑<?php echo $post['edit_count']; ?>次
        </div>
        <?php endif; ?>
    </div>

    <div style="margin-top:20px;">
        <?php echo CommentHelper::renderCommentSection($post['id'], $post['comments'], $comments); ?>
    </div>
</div>

<script>
function openEditModal(postId) {
    var csrfInput = document.querySelector('#repostForm input[name="csrf_token"]');
    var csrfToken = csrfInput ? csrfInput.value : '';
    
    fetch('<?php echo $this->url("post/getEditData"); ?>&id=' + postId, {
        headers: { 'X-Requested-With': 'XMLHttpRequest' }
    })
    .then(function(r) { return r.json(); })
    .then(function(data) {
        if (data.code === 0) {
            document.getElementById('editPostId').value = data.data.id;
            document.getElementById('editContent').value = data.data.content;
            document.getElementById('editModal').style.display = 'flex';
        } else {
            alert(data.message || '获取数据失败');
        }
    })
    .catch(function() {
        alert('获取数据失败');
    });
}

document.querySelectorAll('.edit-btn').forEach(function(btn) {
    btn.addEventListener('click', function(e) {
        e.preventDefault();
        openEditModal(this.dataset.id);
    });
});

document.querySelectorAll('.delete-btn').forEach(function(btn) {
    btn.addEventListener('click', function(e) {
        e.preventDefault();
        if (!confirm('确定要删除这条动态吗？')) return;
        var postId = this.dataset.id;
        var csrfToken = document.querySelector('#repostForm input[name="csrf_token"]').value;
        
        fetch('<?php echo $this->url("post/delete"); ?>', {
            method: 'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body: 'id=' + postId + '&csrf_token=' + encodeURIComponent(csrfToken)
        })
        .then(function(r) { return r.json(); })
        .then(function(data) {
            if (data.code === 0) {
                alert('删除成功');
                window.location.href = '<?php echo $this->url(""); ?>';
            } else {
                alert(data.message || '删除失败');
            }
        })
        .catch(function() {
            alert('删除失败');
        });
    });
});

document.querySelectorAll('.pin-btn, .feature-btn').forEach(function(btn) {
    btn.addEventListener('click', function(e) {
        e.preventDefault();
        var postId = this.dataset.id;
        var btnEl = this;
        var csrfToken = document.querySelector('#repostForm input[name="csrf_token"]').value;
        var action = this.classList.contains('pin-btn') ? '<?php echo $this->url("admin/togglePin"); ?>' : '<?php echo $this->url("admin/toggleFeature"); ?>';
        
        var formData = new FormData();
        formData.append('id', postId);
        formData.append('csrf_token', csrfToken);
        
        fetch(action, { method: 'POST', body: formData })
        .then(function(r) { return r.json(); })
        .then(function(data) {
            if (data.code === 0) {
                if (btnEl.classList.contains('pin-btn')) {
                    var newStatus = data.data.is_pinned;
                    btnEl.textContent = newStatus ? '📌 取消置顶' : '📌 置顶';
                } else {
                    var newStatus = data.data.is_featured;
                    btnEl.textContent = newStatus ? '⭐ 取消加精' : '⭐ 加精';
                }
            } else {
                alert(data.message);
            }
        })
        .catch(function() {
            alert('操作失败');
        });
    });
});
</script>
