'use client'; import { useState, useEffect } from 'react'; import { motion } from 'framer-motion'; import { useRouter } from 'next/navigation'; import Link from 'next/link'; import { User, Mail, Phone, MessageCircle, Check, X, ArrowLeft, Building, Home, Calendar, MapPin, Loader2, Pencil } from 'lucide-react'; import toast, { Toaster } from 'react-hot-toast'; import AuthService from '../services/AuthService'; import { getCustomerByUserId, getOwnerByUserId } from '../utils/api'; export default function ProfilePage() { const router = useRouter(); const [user, setUser] = useState(null); const [isLoading, setIsLoading] = useState(true); const [editingBio, setEditingBio] = useState(false); const [bioDraft, setBioDraft] = useState(''); const [formData, setFormData] = useState({ name: '', email: '', phone: '', whatsapp: '', bio: '', location: '', joinedDate: '' }); const [avatarPreview, setAvatarPreview] = useState(''); useEffect(() => { const authUser = AuthService.getUser(); if (authUser) { const userData = { id: authUser.id, name: authUser.name || '', email: authUser.email || '', phone: authUser.phone || '', role: AuthService.isOwner() ? 'owner' : 'customer', }; setUser(userData); async function fetchProfile() { try { const fetchFn = userData.role === 'owner' ? getOwnerByUserId : getCustomerByUserId; const profile = await fetchFn(userData.id); if (profile) { const profileData = { name: profile.fullName || profile.name || `${profile.firstName || ''} ${profile.lastName || ''}`.trim() || userData.name || '', email: profile.email || userData.email || '', phone: profile.phone || profile.phoneNumber || userData.phone || '', whatsapp: profile.whatsAppNumber || profile.whatsapp || '', bio: profile.bio || '', location: profile.address || profile.location || '', joinedDate: profile.createdAt ? new Date(profile.createdAt).toLocaleDateString('ar-SA', { month: 'long', year: 'numeric' }) : new Date().toLocaleDateString('ar-SA', { month: 'long', year: 'numeric' }), }; setFormData(profileData); setBioDraft(profileData.bio); localStorage.setItem('userProfile', JSON.stringify(profileData)); setIsLoading(false); return; } } catch (err) { // Ignore API errors and fall back to local data. } const savedProfile = localStorage.getItem('userProfile'); let profileData; if (savedProfile) { profileData = JSON.parse(savedProfile); } else { profileData = { name: userData.name || '', email: userData.email || '', phone: '', whatsapp: '', bio: '', location: '', joinedDate: new Date().toLocaleDateString('ar-SA', { month: 'long', year: 'numeric' }) }; } setFormData(profileData); setBioDraft(profileData.bio || ''); setIsLoading(false); } const savedAvatar = localStorage.getItem('userAvatar'); if (savedAvatar) { setAvatarPreview(savedAvatar); } fetchProfile(); } else { router.push('/login'); } }, [router]); const startBioEditing = () => { setBioDraft(formData.bio || ''); setEditingBio(true); }; const cancelBioEditing = () => { setBioDraft(formData.bio || ''); setEditingBio(false); }; const saveBio = () => { const updatedData = { ...formData, bio: bioDraft }; setFormData(updatedData); localStorage.setItem('userProfile', JSON.stringify(updatedData)); setEditingBio(false); toast.success('تم تحديث نبذة عني بنجاح'); }; const fadeInUp = { initial: { opacity: 0, y: 20 }, animate: { opacity: 1, y: 0 }, transition: { duration: 0.5 } }; if (isLoading) { return (

جاري تحميل الملف الشخصي...

); } return (
العودة للرئيسية
{avatarPreview ? ( {formData.name} ) : ( formData.name?.charAt(0).toUpperCase() || 'U' )}

{formData.name}

{formData.location || 'الموقع غير محدد'}
عضو منذ {formData.joinedDate}
{formData.email}
{formData.phone || 'غير محدد'}
{formData.whatsapp || 'غير محدد'}
{user?.role === 'owner' ? ( <> مالك عقار ) : ( <> مستأجر )}
{!editingBio && ( )}
{editingBio ? (