// static/js/comments.js
document.addEventListener('DOMContentLoaded', function() {
    // 回复按钮点击事件
    document.querySelectorAll('.reply-btn').forEach(btn => {
        btn.addEventListener('click', function() {
            const parentId = this.dataset.commentId;
            const form = document.querySelector('.comment-form');
            form.querySelector('[name="parent"]').value = parentId;
            form.scrollIntoView({ behavior: 'smooth' });
        });
    });

    // AJAX表单提交
    document.querySelectorAll('.ajax-comment-form').forEach(form => {
        form.addEventListener('submit', async (e) => {
            e.preventDefault();
            const formData = new FormData(form);
            
            try {
                const response = await fetch(form.action, {
                    method: 'POST',
                    body: formData,
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest'
                    }
                });
                
                const data = await response.json();
                if (data.status === 'success') {
                    // 更新评论列表
                    location.reload(); 
                } else {
                    showErrors(data.errors);
                }
            } catch (error) {
                console.error('提交失败:', error);
            }
        });
    });
});
