/**
 * 详情页面 JavaScript
 * 包含评论功能
 */

// 评论功能模块
(function() {
    'use strict';

    // 配置参数，由页面传入
    window.HuSNS_Detail = window.HuSNS_Detail || {
        apiUrls: {
            like: '/?r=mobileApi/like',
            unlike: '/?r=mobileApi/unlike',
            favorite: '/?r=mobileApi/favorite',
            unfavorite: '/?r=mobileApi/unfavorite',
            comment: '/?r=mobileApi/comment',
            likeComment: '/?r=mobileApi/likeComment',
            unlikeComment: '/?r=mobileApi/unlikeComment'
        },
        postId: 0
    };

    document.addEventListener('DOMContentLoaded', function() {
        const commentInput = document.getElementById('commentInput');
        const commentSubmit = document.getElementById('commentSubmit');
        
        // 监听评论输入
        if (commentInput && commentSubmit) {
            commentInput.addEventListener('input', function() {
                commentSubmit.disabled = this.value.trim() === '';
            });
        }
        
        // 评论按钮点击聚焦输入框
        const commentButtons = document.querySelectorAll('.comment-btn');
        commentButtons.forEach(button => {
            button.addEventListener('click', function() {
                if (commentInput) {
                    commentInput.focus();
                }
            });
        });
    });
    
    // 提交评论
    window.submitComment = function() {
        const commentInput = document.getElementById('commentInput');
        const content = commentInput ? commentInput.value.trim() : '';
        
        if (!content) return;
        
        const commentSubmit = document.getElementById('commentSubmit');
        if (commentSubmit) {
            commentSubmit.textContent = '发送中...';
            commentSubmit.disabled = true;
        }
        
        fetch(window.HuSNS_Detail.apiUrls.comment, {
            method: 'POST',
            credentials: 'include',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({
                post_id: window.HuSNS_Detail.postId,
                content: content
            })
        }).then(r => r.json()).then(data => {
            if (data.code === 0) {
                if (window.HuSNS && HuSNS.showToast) {
                    HuSNS.showToast('评论成功');
                } else {
                    alert('评论成功');
                }
                commentInput.value = '';
                window.location.reload();
            } else {
                if (window.HuSNS && HuSNS.showToast) {
                    HuSNS.showToast(data.message || '评论失败', 'error');
                } else {
                    alert(data.message || '评论失败');
                }
            }
        }).catch(() => {
            if (window.HuSNS && HuSNS.showToast) {
                HuSNS.showToast('网络错误', 'error');
            } else {
                alert('网络错误');
            }
        }).finally(() => {
            if (commentSubmit) {
                commentSubmit.textContent = '发送';
                commentSubmit.disabled = true;
            }
        });
    };
    
    // 点赞/取消点赞评论
    window.toggleDetailCommentLike = function(element, commentId) {
        const isLiked = element.classList.contains('liked');
        const countElement = element.querySelector('.like-count');
        const svg = element.querySelector('svg');
        const currentCount = parseInt(countElement.textContent) || 0;
        
        // 先更新UI
        element.classList.toggle('liked');
        if (isLiked) {
            countElement.textContent = currentCount - 1;
            if (svg) svg.setAttribute('fill', 'currentColor');
        } else {
            countElement.textContent = currentCount + 1;
            if (svg) svg.setAttribute('fill', '#ff4757');
        }
        
        // 调用API
        const apiUrl = isLiked ? window.HuSNS_Detail.apiUrls.unlikeComment : window.HuSNS_Detail.apiUrls.likeComment;
        fetch(apiUrl, {
            method: 'POST',
            credentials: 'include',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({comment_id: commentId})
        }).then(r => r.json()).then(data => {
            if (data.code !== 0) {
                // 失败回滚
                element.classList.toggle('liked');
                countElement.textContent = currentCount;
                if (svg) svg.setAttribute('fill', isLiked ? '#ff4757' : 'currentColor');
                if (window.HuSNS && HuSNS.showToast) {
                    HuSNS.showToast(data.message || '操作失败', 'error');
                } else {
                    alert(data.message || '操作失败');
                }
            }
        }).catch(() => {
            // 网络错误回滚
            element.classList.toggle('liked');
            countElement.textContent = currentCount;
            if (svg) svg.setAttribute('fill', isLiked ? '#ff4757' : 'currentColor');
            if (window.HuSNS && HuSNS.showToast) {
                HuSNS.showToast('网络错误', 'error');
            } else {
                alert('网络错误');
            }
        });
    };
    
    // 回复评论
    window.replyComment = function(commentId, username) {
        const commentInput = document.getElementById('commentInput');
        if (commentInput) {
            commentInput.value = '@' + username + ' ';
            commentInput.focus();
        }
    };
    
    // 加载更多回复
    window.loadMoreReplies = function(commentId) {
        if (window.HuSNS && HuSNS.showToast) {
            HuSNS.showToast('功能开发中');
        } else {
            alert('功能开发中');
        }
    };
})();