Add Your Reply
`;
};
// --- Tab Switching Logic ---
const switchToTab = (tabId) => {
tabButtons.forEach(btn => {
btn.classList.toggle('active', btn.dataset.tab === tabId);
});
if (tabId === 'all-discussions') {
renderAllDiscussions();
} else if (tabId === 'new-post') {
renderNewPostForm();
}
};
// --- Event Handlers ---
tabButtons.forEach(btn => {
btn.addEventListener('click', () => switchToTab(btn.dataset.tab));
});
mainContent.addEventListener('click', (e) => {
const target = e.target;
// View a thread
const threadCard = target.closest('[data-thread-id]');
if (threadCard && !target.closest('button')) {
const threadId = parseInt(threadCard.dataset.threadId);
renderThreadView(threadId);
}
// Back button
if (target.id === 'back-to-discussions') {
switchToTab('all-discussions');
}
// Submit new post
if (target.id === 'submit-post-btn') {
const title = document.getElementById('post-title').value;
const content = document.getElementById('post-content').value;
if (!title || !content) {
alert("Please provide a title and content for your post.");
return;
}
const newThread = {
id: discussionThreads.length + 1,
title, content, author: "Current User", // In a real app, this would be dynamic
timestamp: "Just now", replies: 0, replyList: []
};
discussionThreads.unshift(newThread); // Add to beginning
switchToTab('all-discussions');
}
// Submit reply
if (target.id === 'submit-reply-btn') {
const threadId = parseInt(target.dataset.threadId);
const replyContent = document.getElementById('reply-content').value;
if (!replyContent) {
alert("Please enter a reply.");
return;
}
const thread = discussionThreads.find(t => t.id === threadId);
thread.replyList.push({
author: "Current User", timestamp: "Just now", content: replyContent
});
thread.replies++;
renderThreadView(threadId); // Re-render the view
}
// Download PDF
if (target.id === 'download-thread-pdf') {
const threadId = parseInt(target.dataset.threadId);
generatePdf(threadId);
}
});
// --- PDF Generation ---
const generatePdf = (threadId) => {
const thread = discussionThreads.find(t => t.id === threadId);
if (!thread) return;
const buildPdfHtml = () => {
const date = new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
const repliesHtml = thread.replyList.map(reply => `
${reply.content}
— ${reply.author}, ${reply.timestamp}
`).join('');
return `
${thread.title}
Posted by ${thread.author} • Report generated on ${date}
Original Post
${thread.content}
Replies
${repliesHtml || '
No replies yet.
'}
`;
};
pdfRenderContainer.innerHTML = buildPdfHtml();
html2canvas(pdfRenderContainer, { scale: 2, useCORS: true })
.then(canvas => {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: 'a4' });
const imgData = canvas.toDataURL('image/png');
const pdfWidth = pdf.internal.pageSize.getWidth();
const ratio = canvas.width / canvas.height;
const pdfHeight = pdfWidth / ratio;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
pdf.save(`Discussion_${thread.title.replace(/\s+/g, '_')}.pdf`);
});
};
// --- Initial Load ---
switchToTab('all-discussions');
});