import React, { useState, useEffect } from 'react'; import { Box, useTheme, useMediaQuery, Typography } from '@mui/material'; import KitchPlusAppBar from '../AppBar'; import Sidebar from '../SideHome'; import AccountSettings from './contect/AccountSettings'; import MealsByCateg from './contect/MealsByCateg'; import AllMeals from './contect/AllMeals'; import ProductDetail from './contect/ProductDetail'; import authService from '../../../services/authService'; import { useRestaurant } from '../../../contexts/RestaurantContext'; const drawerWidth = 230; const Meal = () => { const theme = useTheme(); const isMobile = useMediaQuery(theme.breakpoints.down('sm')); const [sidebarOpen, setSidebarOpen] = useState(!isMobile); const { restaurantId } = useRestaurant(); const [showAllPopular, setShowAllPopular] = useState(false); const [categories, setCategories] = useState([]); const [selectedCategoryId, setSelectedCategoryId] = useState(null); const [categoryName, setCategoryName] = useState(''); const [selectedProduct, setSelectedProduct] = useState(null); const [meals, setMeals] = useState([]); const [loadingMeals, setLoadingMeals] = useState(false); // جلب الفئات عند التحميل useEffect(() => { const fetchCategories = async () => { if (!restaurantId) return; const result = await authService.getCategoriesByRestaurant(restaurantId); if (result.success) setCategories(result.data); else setCategories([]); }; fetchCategories(); }, [restaurantId]); // تحديث اسم الفئة عند تغيير selectedCategoryId أو categories useEffect(() => { if (selectedCategoryId && categories.length > 0) { const selected = categories.find(c => c.id === selectedCategoryId); setCategoryName(selected ? selected.name : ''); } else { setCategoryName(''); } }, [selectedCategoryId, categories]); // جلب الوجبات عند اختيار الفئة useEffect(() => { const fetchMeals = async () => { if (selectedCategoryId && restaurantId) { setLoadingMeals(true); try { const result = await authService.getMealsByCategory(restaurantId, selectedCategoryId); if (result.success) { const selectedCategory = categories.find(c => c.id === selectedCategoryId); const mealsWithImages = result.data.map(meal => ({ id: meal.id, name: meal.name, price: meal.price, unit: meal.unit || '/pcs', image: meal.photo || '/images/default-product.png', category: selectedCategory ? selectedCategory.name : '', // اسم الفئة category_id: meal.category_id, restaurant_id: meal.restaurant_id, description: meal.description || [], additions: meal.additions || [] })); setMeals(mealsWithImages); } else setMeals([]); } catch (err) { console.error(err); setMeals([]); } finally { setLoadingMeals(false); } } else setMeals([]); }; fetchMeals(); }, [selectedCategoryId, restaurantId, categories]); const handleDrawerToggle = () => setSidebarOpen(!sidebarOpen); const toggleShowAllPopular = () => setShowAllPopular(prev => !prev); return ( {selectedProduct ? ( setSelectedProduct(null)} /> ) : ( <> {selectedCategoryId ? ( setSelectedCategoryId(null)} onProductClick={(meal) => { const category = categories.find(c => c.id === meal.category_id); setSelectedProduct({ ...meal, category: category ? category.name : '', category_id: meal.category_id, restaurant_id: restaurantId }); }} /> ) : !showAllPopular ? ( { const category = categories.find(c => c.id === meal.category_id); setSelectedProduct({ ...meal, category: category ? category.name : '', category_id: meal.category_id, restaurant_id: restaurantId }); }} /> ) : null} )} ); }; export default Meal;