` : ''}
`;
if(journalTextarea) journalTextarea.value = journalEntry || '';
// Rule-based suggestions
suggestionsEl.innerHTML = '';
let suggestions = [];
if (['Stressed', 'Anxious', 'Overwhelmed', 'Frustrated'].includes(primaryEmotion) && emotionIntensity >= 3) {
suggestions.push("Acknowledge these feelings. It's okay to feel this way given the circumstances.");
if (stressorFactors.includes('sf_workload') || stressorFactors.includes('sf_deadline')) {
suggestions.push("Try breaking down large tasks. Can anything be delegated or postponed? The Eisenhower Matrix might help prioritize.");
}
suggestions.push("Consider a short mindfulness exercise or deep breathing to center yourself.");
const effectiveCoping = loggedCoping.filter(lc => lc.effectiveness >= 4);
if(effectiveCoping.length > 0) {
suggestions.push(`Remember that ${effectiveCoping.map(lc=>lc.name).join(' and ')} felt helpful.`);
} else {
suggestions.push("Explore some coping mechanisms from the Resource Hub like taking a walk or listening to calming music.");
}
}
if (['Happy', 'Content', 'Motivated', 'Grateful'].includes(primaryEmotion) && emotionIntensity >=3) {
suggestions.push("It's wonderful you're feeling positive! Acknowledge what contributed to this feeling.");
if(positiveFactors.length > 0) suggestions.push(`Lean into positive factors like '${ewb_positiveWorkFactors.find(pf=>pf.id===positiveFactors[0])?.name || positiveFactors[0]}'.`);
}
if (primaryEmotion === 'Tired' && emotionIntensity >=3) {
suggestions.push("Ensure you're getting enough rest. A short break or ensuring good sleep tonight might help.");
}
if(suggestions.length === 0) suggestions.push("Continue to be mindful of your well-being. Regular check-ins can help you understand your patterns.");
suggestions.forEach(s => {const li = document.createElement('li'); li.textContent = s; suggestionsEl.appendChild(li);});
document.getElementById('ewb-downloadPdfBtn').style.display = 'inline-block';
}
function ewb_saveJournalEntry() {
if (ewb_latestFullCheckInResult) {
ewb_latestFullCheckInResult.journalEntry = document.getElementById('ewb-journalEntry').value;
const idx = ewb_checkIns.findIndex(a => a.id === ewb_latestFullCheckInResult.id);
if (idx > -1) ewb_checkIns[idx].journalEntry = ewb_latestFullCheckInResult.journalEntry;
ewb_saveData();
alert("Journal entry saved for this check-in.");
} else {
alert("Please complete a check-in first to save a journal entry for it.");
}
}
// --- Well-being Log & Trends Tab ---
function ewb_initLogTrendsTab() {
const todayForPicker = EWB_TODAY_CONTEXT;
document.getElementById('ewb-logFilterStartDate').value = new Date(todayForPicker.getFullYear(), todayForPicker.getMonth(), 1).toISOString().split('T')[0]; // Default to start of current month
document.getElementById('ewb-logFilterEndDate').value = todayForPicker.toISOString().split('T')[0];
ewb_renderCheckInLog();
ewb_renderEmotionTrendChart();
}
function ewb_renderCheckInLog(){
const listEl = document.getElementById('ewb-checkInHistoryList'); listEl.innerHTML = '';
const startDate = document.getElementById('ewb-logFilterStartDate').value;
const endDate = document.getElementById('ewb-logFilterEndDate').value;
const filtered = ewb_checkIns.filter(ci => {
let match = true;
if(startDate && ci.date < startDate) match = false;
if(endDate && ci.date > endDate) match = false;
return match;
});
if(filtered.length === 0){ listEl.innerHTML = '
No check-ins found for the selected period.
'; return;}
filtered.forEach(ci => {
const li = document.createElement('li');
li.innerHTML = `${new Date(ci.date+"T00:00:00Z").toLocaleDateString()}: Felt ${ci.primaryEmotion} (Intensity: ${ci.emotionIntensity})
Situation: ${(ci.workSituation || "N/A").substring(0,50)}...
`;
listEl.appendChild(li);
});
}
function ewb_viewCheckInDetail(id){
const checkIn = ewb_checkIns.find(ci => ci.id === id);
if(checkIn) {
ewb_latestFullCheckInResult = checkIn; // Set it as the one to view/edit
// Populate check-in form
document.getElementById('ewb-checkInId').value = checkIn.id;
document.getElementById('ewb-checkInDate').value = checkIn.date;
document.getElementById('ewb-primaryEmotion').value = checkIn.primaryEmotion;
document.getElementById('ewb-emotionIntensity').value = checkIn.emotionIntensity;
document.getElementById('val-emotionIntensity').textContent = checkIn.emotionIntensity;
document.getElementById('ewb-workSituation').value = checkIn.workSituation;
Array.from(document.querySelectorAll('input[name="positiveFactors"]')).forEach(cb => cb.checked = (checkIn.positiveFactors || []).includes(cb.value));
Array.from(document.querySelectorAll('input[name="stressorFactors"]')).forEach(cb => cb.checked = (checkIn.stressorFactors || []).includes(cb.value));
ewb_currentCheckInForLog.loggedCoping = [...(checkIn.loggedCoping || [])]; // Load coping logs
const displayArea = document.getElementById('ewb-todaysCopingLogs');
displayArea.innerHTML = 'Logged for this check-in: