import { useState } from 'react'; import { motion } from 'framer-motion'; import { Star, Edit2, X, Check, Clock } from 'lucide-react'; import StarRating from './StarRating.js'; import toast, { Toaster } from 'react-hot-toast'; import { rateProperty, rateCustomer, getUserPropertyRating, canRateProperty } from '../../utils/ratings.js'; const RatingForm = ({ propertyId, userId, propertyOwner = false, initialRating = 0, initialComment = '', onSubmitSuccess }) => { const [rating, setRating] = useState(initialRating); const [comment, setComment] = useState(initialComment); const [loading, setLoading] = useState(false); const [showForm, setShowForm] = useState(false); const [userRating, setUserRating] = useState(null); // Check if user has already rated useState(() => { async function fetchUserRating() { try { const rating = await getUserPropertyRating(propertyId, userId); if (rating) { setUserRating(rating); setRating(rating.rating); setComment(rating.comment || ''); } } catch (error) { console.error('[RatingForm] Failed to fetch user rating:', error); } } if (propertyId && userId) { fetchUserRating(); } }, [propertyId, userId]); const handleSubmit = async (e) => { e.preventDefault(); if (!rating) { toast.error('يرجى إعطاء تقييم من 1 إلى 5 نجوم'); return; } setLoading(true); try { const ratingData = { propertyId, customerId: userId, rating, comment: comment.trim() || null }; await rateProperty(ratingData); toast.success('تم إرسال التقييم بنجاح!'); // Reset form setRating(0); setComment(''); setShowForm(false); if (onSubmitSuccess) { onSubmitSuccess(); } } catch (error) { console.error('[RatingForm] Failed to submit rating:', error); toast.error('حدث خطأ أثناء إرسال التقييم. يرجى المحاولة مرة أخرى.'); } finally { setLoading(false); } }; const handleEdit = () => { setShowForm(true); setRating(userRating?.rating || 0); setComment(userRating?.comment || ''); }; const handleCancel = () => { setShowForm(false); setRating(userRating?.rating || 0); setComment(userRating?.comment || ''); }; if (!propertyId || !userId) { return null; } return (
شارك تجربتك مع المستأجرين الآخرين