import React, { useState } from 'react'; import { Box, Typography, Grid, useTheme, useMediaQuery, IconButton, Skeleton } from '@mui/material'; import { useNavigate } from 'react-router-dom'; import AddToPhotosIcon from '@mui/icons-material/AddToPhotos'; import InfoIcon from '@mui/icons-material/Info'; import { useRestaurant } from '../../../contexts/RestaurantContext'; import RestaurantDetailsModal from './RestaurantDetailsModal'; import authService from '../../../services/authService'; const RestaurantSelection = ({ restaurants = [], loading = false }) => { const theme = useTheme(); const isSmall = useMediaQuery(theme.breakpoints.down('sm')); const navigate = useNavigate(); // استخدم navigate للتوجيه const { setRestaurantId } = useRestaurant(); const [selectedRestaurant, setSelectedRestaurant] = useState(null); const [modalOpen, setModalOpen] = useState(false); const handleSelectRestaurant = (id) => { setRestaurantId(id); navigate('/dashboard'); }; const fetchRestaurantDetails = async (id) => { try { const result = await authService.getRestaurantById(id); if (result.success) { setSelectedRestaurant(result.data); setModalOpen(true); } else { alert(result.message || 'Failed to fetch restaurant details'); } } catch (error) { console.error('Error fetching restaurant details:', error); } }; // ✅ دالة التوجيه عند الضغط على زر الإنشاء const handleCreateClick = () => { navigate('/create-restaurant'); // توجه مباشرة إلى صفحة الإنشاء }; return ( {/* زر إنشاء مطعم */} Create New Restaurant Start from scratch and build your restaurant profile {/* قائمة المطاعم */} Existing Restaurants {restaurants.length === 0 && !loading ? ( No restaurants found. Create one to get started. ) : ( {(loading ? Array.from(new Array(3)) : restaurants).map((restaurant, index) => ( {loading ? ( ) : ( handleSelectRestaurant(restaurant.id)} > {/* أيقونة فتح المودال */} { e.stopPropagation(); fetchRestaurantDetails(restaurant.id); }} sx={{ position: 'absolute', top: 8, right: 8, backgroundColor: 'rgba(255,255,255,0.8)', '&:hover': { backgroundColor: 'rgba(255,255,255,1)', transform: 'scale(1.1)', transition: 'transform 0.2s' }, zIndex: 2, padding: 0.5 }} size="small" > {/* صورة المطعم */} {/* باقي الكارت */} {restaurant.name} {restaurant.location || ' '} )} ))} )} {/* مودال المطعم */} setModalOpen(false)} restaurant={selectedRestaurant} /> ); }; export default RestaurantSelection;