`;
logsDisplay.appendChild(logEl);
});
};
/**
* Handles the form submission to add a new communication log.
*/
const handleAddLog = (e) => {
e.preventDefault();
const memberInput = document.getElementById('team-member');
const channelInput = document.getElementById('channel');
const messageInput = document.getElementById('message');
if (!memberInput.value.trim() || !messageInput.value.trim()) {
alert('Please fill out all fields.');
return;
}
communicationLogs.push({
id: Date.now(),
member: memberInput.value.trim(),
channel: channelInput.value,
message: messageInput.value.trim()
});
renderLogs();
commLogForm.reset();
};
/**
* Handles deleting a log.
*/
const handleDeleteLog = (e) => {
if (e.target.classList.contains('delete-log-btn')) {
const logId = parseInt(e.target.dataset.id, 10);
communicationLogs = communicationLogs.filter(log => log.id !== logId);
renderLogs();
}
};
/**
* Handles PDF download functionality.
*/
const handlePdfDownload = () => {
const { jsPDF } = window.jspdf;
const content = document.getElementById('pdf-content');
if (!content) return;
html2canvas(content, { scale: 2 }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({
orientation: 'landscape',
unit: 'pt',
format: 'a4'
});
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const ratio = canvasWidth / canvasHeight;
let newWidth = pdfWidth - 40;
let newHeight = newWidth / ratio;
if (newHeight > pdfHeight - 60) {
newHeight = pdfHeight - 60;
newWidth = newHeight * ratio;
}
const x = (pdfWidth - newWidth) / 2;
pdf.setFont('Inter', 'normal');
pdf.text("Business Communication Analytics Dashboard", 20, 30);
pdf.addImage(imgData, 'PNG', x, 50, newWidth, newHeight);
pdf.save('communication-analytics.pdf');
});
};
// --- Event Listeners ---
tabs.dashboard.addEventListener('click', () => { currentTab = 'dashboard'; updateTabs(); });
tabs.config.addEventListener('click', () => { currentTab = 'config'; updateTabs(); });
navButtons.next.addEventListener('click', () => { currentTab = 'config'; updateTabs(); });
navButtons.prev.addEventListener('click', () => { currentTab = 'dashboard'; updateTabs(); });
commLogForm.addEventListener('submit', handleAddLog);
logsDisplay.addEventListener('click', handleDeleteLog);
downloadPdfBtn.addEventListener('click', handlePdfDownload);
// --- Initializations ---
updateTabs();
renderLogs();
});
