class MealLoggerEnhanced extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }); // Thai food database this.thaiFoodDatabase = [ { name: 'ข้าวมันไก่', calories: 580, protein: 25, carbs: 65, fat: 20, image: 'http://static.photos/food/200x200/567' }, { name: 'ส้มตำไทย + ไก่ย่าง', calories: 450, protein: 30, carbs: 35, fat: 18, image: 'http://static.photos/food/200x200/789' }, { name: 'กะเพราไก่ไข่ดาว', calories: 520, protein: 28, carbs: 45, fat: 25, image: 'http://static.photos/food/200x200/133' }, { name: 'ข้าวขาหมู', calories: 620, protein: 35, carbs: 55, fat: 28, image: 'http://static.photos/food/200x200/234' }, { name: 'บะหมี่เกี๊ยว', calories: 480, protein: 22, carbs: 58, fat: 16, image: 'http://static.photos/food/200x200/345' }, { name: 'มาม่าต้มยำกุ้ง', calories: 320, protein: 12, carbs: 42, fat: 10, image: 'http://static.photos/food/200x200/456' }, { name: 'ข้าวผัดกระเพรากุ้ง', calories: 580, protein: 25, carbs: 65, fat: 22, image: 'http://static.photos/food/200x200/567' }, { name: 'สุกี้แห้งหมู', calories: 520, protein: 20, carbs: 68, fat: 18, image: 'http://static.photos/food/200x200/678' }, { name: 'ชานมไข่มุก', calories: 280, protein: 4, carbs: 45, fat: 8, image: 'http://static.photos/food/200x200/789' }, { name: 'ข้าวเหนียวมะม่วง', calories: 350, protein: 3, carbs: 58, fat: 12, image: 'http://static.photos/food/200x200/890' } ]; } connectedCallback() { this.render(); } render() { this.shadowRoot.innerHTML = `
มื้ออาหารล่าสุด
AI
คำแนะนำอัจฉริยะ
${this.thaiFoodDatabase.slice(0, 3).map(food => this.renderMealItem(food)).join('')}
ข้าวมันไก่
ส้มตำ
กะเพรา
ชานมไข่มุก
ข้าวเหนียวมะม่วง
`; this.setupEventListeners(); } renderMealItem(food) { return `
${food.name}
${food.name}
${food.calories} แคล
P:${food.protein}g C:${food.carbs}g F:${food.fat}g
`; } setupEventListeners() { const searchInput = this.shadowRoot.getElementById('searchInput'); const searchResults = this.shadowRoot.getElementById('searchResults'); const aiPhotoBtn = this.shadowRoot.getElementById('aiPhotoBtn'); const barcodeBtn = this.shadowRoot.getElementById('barcodeBtn'); const photoInput = this.shadowRoot.getElementById('photoInput'); const barcodeInput = this.shadowRoot.getElementById('barcodeInput'); const quickAddItems = this.shadowRoot.querySelectorAll('.quick-add-item'); // Search functionality searchInput.addEventListener('input', (e) => { const query = e.target.value.toLowerCase().trim(); if (query.length < 2) { searchResults.classList.remove('show'); return; } const matches = this.thaiFoodDatabase.filter(food => food.name.toLowerCase().includes(query) || food.name.replace(/\s/g, '').includes(query.replace(/\s/g, '')) ); if (matches.length > 0) { searchResults.innerHTML = matches.slice(0, 5).map(food => `
${food.name}
${food.name}
${food.calories} แคลอรี
`).join(''); searchResults.classList.add('show'); // Add click handlers searchResults.querySelectorAll('.search-item').forEach(item => { item.addEventListener('click', () => { const food = JSON.parse(item.dataset.food); this.addMeal(food); searchResults.classList.remove('show'); searchInput.value = ''; }); }); } else { searchResults.innerHTML = '
ไม่พบอาหาร ลองสร้างเมนูเอง?
'; searchResults.classList.add('show'); } }); // Hide search results when clicking outside document.addEventListener('click', (e) => { if (!this.shadowRoot.contains(e.target)) { searchResults.classList.remove('show'); } }); // AI Photo logging aiPhotoBtn.addEventListener('click', () => { photoInput.click(); }); photoInput.addEventListener('change', (e) => { const file = e.target.files[0]; if (file) { this.processFoodPhoto(file); } }); // Barcode scanning barcodeBtn.addEventListener('click', () => { barcodeInput.click(); }); barcodeInput.addEventListener('change', (e) => { const file = e.target.files[0]; if (file) { this.processBarcode(file); } }); // Quick add buttons quickAddItems.forEach(item => { item.addEventListener('click', () => { const mealName = item.dataset.meal; const food = this.thaiFoodDatabase.find(f => f.name.includes(mealName)); if (food) { this.addMeal(food); } }); }); // Meal item clicks this.shadowRoot.addEventListener('click', (e) => { const mealItem = e.target.closest('.meal-item'); if (mealItem) { const foodName = mealItem.dataset.food; const food = this.thaiFoodDatabase.find(f => f.name === foodName); if (food) { this.addMeal(food); } } }); } processFoodPhoto(file) { this.showNotification('กำลังวิเคราะห์รูปภาพด้วย AI...', 'info'); // Simulate AI processing setTimeout(() => { const detectedFood = this.thaiFoodDatabase[Math.floor(Math.random() * this.thaiFoodDatabase.length)]; this.showAIsuggestion( `ระบบตรวจพบ "${detectedFood.name}" ด้วยความมั่นใจ 85%`, `คลิกเพื่อบันทึก ${detectedFood.calories} แคลอรี หรือแก้ไขถ้าไม่ถูกต้อง` ); // Auto-select the detected food setTimeout(() => { this.addMeal(detectedFood); }, 2000); }, 3000); } processBarcode(file) { this.showNotification('กำลังสแกนบาร์โค้ด...', 'info'); setTimeout(() => { this.showNotification('พบสินค้า: โยเกิร์ตไขมันต่ำ 180 แคลอรี', 'success'); }, 2000); } showAIsuggestion(title, message) { const suggestion = this.shadowRoot.getElementById('aiSuggestion'); const aiMessage = this.shadowRoot.getElementById('aiMessage'); suggestion.classList.add('show'); aiMessage.innerHTML = `${title}
${message}`; setTimeout(() => { suggestion.classList.remove('show'); }, 5000); } addMeal(food) { this.showNotification(`บันทึก ${food.name} ${food.calories} แคลอรี สำเร็จ!`, 'success'); // Add to recent meals const recentMeals = this.shadowRoot.getElementById('recentMeals'); const newMeal = document.createElement('div'); newMeal.className = 'meal-item'; newMeal.innerHTML = this.renderMealItem(food); recentMeals.insertBefore(newMeal, recentMeals.firstChild); // Keep only 3 items while (recentMeals.children.length > 3) { recentMeals.removeChild(recentMeals.lastChild); } // Save to localStorage const todayMeals = JSON.parse(localStorage.getItem('todayMeals') || '[]'); todayMeals.unshift({ ...food, timestamp: new Date().toISOString(), mealType: this.getCurrentMealType() }); localStorage.setItem('todayMeals', JSON.stringify(todayMeals.slice(0, 10))); // Dispatch event this.dispatchEvent(new CustomEvent('meal-logged', { detail: food, bubbles: true, composed: true })); } getCurrentMealType() { const hour = new Date().getHours(); if (hour < 11) return 'เช้า'; if (hour < 16) return 'เที่ยง'; if (hour < 19) return 'เย็น'; return 'ดึก'; } showNotification(message, type = 'info') { if (window.showNotification) { window.showNotification(message, type); } else { alert(message); } } } customElements.define('meal-logger-enhanced', MealLoggerEnhanced);