`;
tipsContainer.innerHTML += getTipsForCategory(categoryId, scorePercentage);
}
}
function getTipsForCategory(categoryId, scorePercentage) {
let tips = [];
if (scorePercentage < 50) { // Low score tips
switch(categoryId) {
case 'workload': tips = ['Prioritize tasks using a system like the Eisenhower Matrix.', 'Discuss your workload with your manager to explore solutions.', 'Delegate tasks if possible.']; break;
case 'boundaries': tips = ['Set clear start and end times for your workday.', 'Turn off work notifications on your phone after hours.', 'Create a dedicated workspace to physically separate work from personal life.']; break;
case 'flexibility': tips = ['Explore flexible scheduling options with your employer.', 'Block out time in your calendar for personal appointments.', 'Communicate your needs for flexibility clearly and proactively.']; break;
case 'wellbeing': tips = ['Schedule short breaks throughout the day to stretch or walk.', 'Ensure you are getting adequate sleep.', 'Make time for a hobby or activity you enjoy completely unrelated to work.']; break;
}
} else if (scorePercentage < 80) { // Medium score tips
switch(categoryId) {
case 'workload': tips = ['Review your weekly schedule to identify time-wasting activities.', 'Try time-blocking to focus on one task at a time.']; break;
case 'boundaries': tips = ['Plan an after-work activity to help you transition out of work mode.', 'Communicate your availability clearly to your team.']; break;
case 'flexibility': tips = ['Take advantage of existing flexible work policies.', 'Plan personal appointments in advance where possible.']; break;
case 'wellbeing': tips = ['Incorporate mindfulness or meditation into your daily routine.', 'Ensure you take your full lunch break away from your desk.']; break;
}
} else { // High score tips
tips = ["You're doing great in this area! Continue to be mindful of these healthy habits."];
}
return `
`;
}
// --- EVENT HANDLERS & LOGIC ---
window.changeTab = (tabName) => {
currentTab = tabName;
Object.values(tabElements).forEach(el => el.classList.add('hidden'));
Object.values(tabButtons).forEach(btn => btn.classList.remove('active'));
tabElements[tabName].classList.remove('hidden');
tabButtons[tabName].classList.add('active');
updateNavButtons();
};
window.navigateTabs = (direction) => {
const currentIndex = tabs.indexOf(currentTab);
const nextIndex = direction === 'next' ? (currentIndex + 1) : (currentIndex - 1);
if (nextIndex >= 0 && nextIndex < tabs.length) changeTab(tabs[nextIndex]);
};
const updateNavButtons = () => {
const currentIndex = tabs.indexOf(currentTab);
prevButton.classList.toggle('invisible', currentIndex === 0);
nextButton.classList.toggle('invisible', currentIndex === tabs.length - 1);
};
if (assessmentForm) {
assessmentForm.addEventListener('submit', (e) => {
e.preventDefault();
const formData = new FormData(assessmentForm);
const scores = {};
for (const q of assessmentQuestions) {
const answer = parseInt(formData.get(`question_${q.id}`));
if (!scores[q.category]) {
scores[q.category] = { score: 0, count: 0, maxScore: 0 };
}
scores[q.category].score += answer;
scores[q.category].count++;
scores[q.category].maxScore += 5; // Max score per question is 5
}
userResults = { scores };
renderResults();
changeTab('results');
});
}
if (addQuestionForm) {
addQuestionForm.addEventListener('submit', (e) => {
e.preventDefault();
const text = document.getElementById('newQuestionText').value.trim();
const category = document.getElementById('newQuestionCategory').value;
if (text && category) {
const newId = assessmentQuestions.length > 0 ? Math.max(...assessmentQuestions.map(q => q.id)) + 1 : 1;
assessmentQuestions.push({ id: newId, category, text });
renderQuestions();
renderQuestionsList();
addQuestionForm.reset();
alert('Question added successfully!');
}
});
}
window.deleteQuestion = (id) => {
if (confirm('Are you sure you want to delete this question?')) {
assessmentQuestions = assessmentQuestions.filter(q => q.id !== id);
renderQuestions();
renderQuestionsList();
}
};
// --- PDF DOWNLOAD ---
window.downloadPDF = async function() {
if (!userResults) {
alert('Please complete the assessment first.');
return;
}
const { jsPDF } = window.jspdf;
const doc = new jsPDF('p', 'pt', 'a4');
const categoryStats = Object.entries(userResults.scores).map(([id, data]) => ({
name: assessmentCategories[id],
score: (data.score / data.maxScore) * 100
}));
const canvas = document.createElement('canvas');
canvas.width = 450; canvas.height = 225;
new Chart(canvas.getContext('2d'), {
type: 'radar',
data: {
labels: categoryStats.map(c => c.name),
datasets: [{
label: 'Balance Score (%)',
data: categoryStats.map(c => c.score),
fill: true,
backgroundColor: 'rgba(59, 130, 246, 0.2)',
borderColor: 'rgb(59, 130, 246)',
pointBackgroundColor: 'rgb(59, 130, 246)',
}]
},
options: {
scales: { r: { beginAtZero: true, max: 100, pointLabels: { font: { size: 10 } } } },
responsive: false, animation: false
}
});
await new Promise(resolve => setTimeout(resolve, 500));
const chartImage = canvas.toDataURL('image/png');
const pageWidth = doc.internal.pageSize.getWidth();
const pageHeight = doc.internal.pageSize.getHeight();
doc.setFillColor(41, 128, 185);
doc.rect(0, 0, pageWidth, 60, 'F');
doc.setFontSize(20); doc.setTextColor(255); doc.setFont('helvetica', 'bold');
doc.text('Work-Life Balance Report', 30, 38);
doc.setFontSize(10); doc.setFont('helvetica', 'normal'); doc.setTextColor(50);
doc.text(`Report Date: ${new Date().toLocaleDateString('en-US')}`, 30, 90);
doc.addImage(chartImage, 'PNG', (pageWidth - 450) / 2, 110, 450, 225);
let y = 360;
doc.setFontSize(14); doc.setFont('helvetica', 'bold'); doc.setTextColor(50);
doc.text('Personalized Tips & Actions', 30, y);
y += 10;
for (const [id, data] of Object.entries(userResults.scores)) {
const scorePercentage = (data.score / data.maxScore) * 100;
const tipsHTML = getTipsForCategory(id, scorePercentage);
const tempDiv = document.createElement('div');
tempDiv.innerHTML = tipsHTML;
const categoryName = tempDiv.querySelector('h5').innerText;
const tips = Array.from(tempDiv.querySelectorAll('li')).map(li => li.innerText);
y += 20;
if (y > pageHeight - 60) { doc.addPage(); y = 40; }
doc.setFontSize(12); doc.setFont('helvetica', 'bold');
doc.text(categoryName, 30, y);
y += 15;
tips.forEach(tip => {
const splitTip = doc.splitTextToSize(`• ${tip}`, pageWidth - 70);
if (y + (splitTip.length * 12) > pageHeight - 40) { doc.addPage(); y = 40; }
doc.setFontSize(10); doc.setFont('helvetica', 'normal');
doc.text(splitTip, 40, y);
y += (splitTip.length * 12) + 5;
});
}
const pageCount = doc.internal.getNumberOfPages();
for(let i = 1; i <= pageCount; i++) {
doc.setPage(i);
doc.setFontSize(8); doc.setTextColor(150);
doc.text(`Page ${i} of ${pageCount}`, pageWidth / 2, pageHeight - 20, { align: 'center' });
}
doc.save('Work-Life_Balance_Report.pdf');
};
// --- INITIALIZE ---
initializeApp();
});
${assessmentCategories[categoryId]}
-
${tips.map(tip => `
- ${tip} `).join('')}
