class MoodLogger extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }); this.moods = [ { emoji: '😊', label: 'āļŠāļ”āļŠāļ·āđˆāļ™', value: 'happy' }, { emoji: '😌', label: 'āļŠāļ‡āļš', value: 'calm' }, { emoji: '😐', label: 'āļ˜āļĢāļĢāļĄāļ”āļē', value: 'neutral' }, { emoji: '😔', label: 'āđ€āļĻāļĢāđ‰āļē', value: 'sad' }, { emoji: '😠', label: 'āļŦāļ‡āļļāļ”āļŦāļ‡āļīāļ”', value: 'angry' }, { emoji: 'ðŸ˜ī', label: 'āļ‡āđˆāļ§āļ‡', value: 'tired' }, { emoji: '😰', label: 'āđ€āļ„āļĢāļĩāļĒāļ”', value: 'stressed' }, { emoji: 'ðŸĪĐ', label: 'āļ•āļ·āđˆāļ™āđ€āļ•āđ‰āļ™', value: 'excited' } ]; this.currentMood = null; } connectedCallback() { this.render(); this.loadTodaysMood(); } render() { this.shadowRoot.innerHTML = `
āļ­āļēāļĢāļĄāļ“āđŒāļ›āļĢāļ°āļˆāļģāļ§āļąāļ™
ðŸ”Ĩ 7 āļ§āļąāļ™
${this.moods.map(mood => `
${mood.emoji}
${mood.label}
`).join('')}
āļ„āļļāļ“āļšāļąāļ™āļ—āļķāļāļ­āļēāļĢāļĄāļ“āđŒāļ•āļīāļ”āļ•āđˆāļ­āļāļąāļ™āļĄāļē 7 āļ§āļąāļ™ āđāļĨāđ‰āļ§!
`; this.setupEventListeners(); } setupEventListeners() { const moodItems = this.shadowRoot.querySelectorAll('.mood-item'); const saveBtn = this.shadowRoot.getElementById('saveMoodBtn'); const historyBtn = this.shadowRoot.getElementById('historyBtn'); const moodNote = this.shadowRoot.getElementById('moodNote'); moodItems.forEach(item => { item.addEventListener('click', () => { // Remove previous selection moodItems.forEach(i => i.classList.remove('selected')); // Select current item.classList.add('selected'); this.currentMood = { value: item.dataset.value, emoji: item.dataset.emoji, label: item.dataset.label }; // Show note textarea moodNote.classList.add('show'); moodNote.focus(); }); }); saveBtn.addEventListener('click', () => { if (!this.currentMood) { this.showNotification('āļāļĢāļļāļ“āļēāđ€āļĨāļ·āļ­āļāļ­āļēāļĢāļĄāļ“āđŒāļāđˆāļ­āļ™āļšāļąāļ™āļ—āļķāļ', 'warning'); return; } const note = moodNote.value.trim(); this.saveMood(this.currentMood, note); }); historyBtn.addEventListener('click', () => { this.showMoodHistory(); }); } loadTodaysMood() { // Simulate loading today's mood from localStorage const savedMood = localStorage.getItem('todayMood'); if (savedMood) { const mood = JSON.parse(savedMood); const moodItem = this.shadowRoot.querySelector(`[data-value="${mood.value}"]`); if (moodItem) { moodItem.classList.add('selected'); this.currentMood = mood; this.shadowRoot.getElementById('moodNote').value = mood.note || ''; this.shadowRoot.getElementById('moodNote').classList.add('show'); } } } saveMood(mood, note) { const data = { ...mood, note, timestamp: new Date().toISOString(), date: new Date().toLocaleDateString('th-TH') }; // Save to localStorage localStorage.setItem('todayMood', JSON.stringify(data)); // Add to history const history = JSON.parse(localStorage.getItem('moodHistory') || '[]'); history.unshift(data); localStorage.setItem('moodHistory', JSON.stringify(history.slice(0, 30))); this.showNotification(`āļšāļąāļ™āļ—āļķāļāļ­āļēāļĢāļĄāļ“āđŒ ${mood.emoji} ${mood.label} āļŠāļģāđ€āļĢāđ‡āļˆ!`, 'success'); // Update streak this.updateStreak(); // Send event to update AI coach this.dispatchEvent(new CustomEvent('mood-updated', { detail: data, bubbles: true, composed: true })); } updateStreak() { const history = JSON.parse(localStorage.getItem('moodHistory') || '[]'); let streak = 0; const today = new Date(); for (let i = 0; i < history.length; i++) { const moodDate = new Date(history[i].timestamp); const daysDiff = Math.floor((today - moodDate) / (1000 * 60 * 60 * 24)); if (daysDiff === streak) { streak++; } else { break; } } const streakBadge = this.shadowRoot.getElementById('streakBadge'); const streakCount = this.shadowRoot.querySelector('.streak-count'); if (streak > 1) { streakBadge.textContent = `ðŸ”Ĩ ${streak} āļ§āļąāļ™`; streakCount.textContent = `${streak} āļ§āļąāļ™`; } else { streakBadge.textContent = 'āđ€āļĢāļīāđˆāļĄāļ•āđ‰āļ™āđƒāļŦāļĄāđˆ'; streakCount.textContent = '1 āļ§āļąāļ™'; } } showMoodHistory() { const history = JSON.parse(localStorage.getItem('moodHistory') || '[]'); const modal = document.createElement('div'); modal.className = 'fixed inset-0 bg-black/50 z-50 flex items-center justify-center p-4'; modal.innerHTML = `

āļ›āļĢāļ°āļ§āļąāļ•āļīāļ­āļēāļĢāļĄāļ“āđŒ

${history.length > 0 ? `
${history.slice(0, 7).map(record => `
${record.emoji} ${record.label}
${record.date}
${record.note ? `

${record.note}

` : ''}
`).join('')}
` : '

āļĒāļąāļ‡āđ„āļĄāđˆāļĄāļĩāļ›āļĢāļ°āļ§āļąāļ•āļīāļāļēāļĢāļšāļąāļ™āļ—āļķāļ

'}
`; document.body.appendChild(modal); } showNotification(message, type = 'info') { if (window.showNotification) { window.showNotification(message, type); } else { alert(message); } } } customElements.define('mood-logger', MoodLogger);