'use client'; import { useEffect, useState } from 'react'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { useAuthStore } from '@/lib/auth-store'; import { profileApi, type AgentProfile } from '@/lib/profile-api'; const KYC_STATUS_MAP: Record = { NONE: { label: 'Chưa xác minh', variant: 'outline' }, PENDING: { label: 'Đang chờ duyệt', variant: 'secondary' }, VERIFIED: { label: 'Đã xác minh', variant: 'default' }, REJECTED: { label: 'Bị từ chối', variant: 'destructive' }, }; export default function ProfilePage() { const { user, fetchProfile } = useAuthStore(); const [agentProfile, setAgentProfile] = useState(null); const [loading, setLoading] = useState(true); const [editing, setEditing] = useState(false); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); const [formData, setFormData] = useState({ fullName: '', email: '', phone: '', }); useEffect(() => { setLoading(true); profileApi .getAgentProfile() .then((agent) => setAgentProfile(agent)) .catch(() => {}) .finally(() => setLoading(false)); }, []); useEffect(() => { if (user) { setFormData({ fullName: user.fullName, email: user.email ?? '', phone: user.phone, }); } }, [user]); const handleSave = async () => { setSaving(true); setError(null); setSuccess(null); try { await profileApi.updateProfile({ fullName: formData.fullName, email: formData.email || undefined, }); await fetchProfile(); setSuccess('Cập nhật hồ sơ thành công'); setEditing(false); } catch (e) { setError(e instanceof Error ? e.message : 'Cập nhật thất bại'); } finally { setSaving(false); } }; const kycInfo = KYC_STATUS_MAP[user?.kycStatus ?? 'NONE'] ?? { label: 'Chưa xác minh', variant: 'outline' as const }; return (

Hồ sơ cá nhân

Quản lý thông tin tài khoản của bạn

{error && (
{error}
)} {success && (
{success}
)}
{/* Profile info */}
Thông tin cá nhân Thông tin cơ bản trên hồ sơ của bạn
{!editing && ( )}
{loading ? (
Đang tải...
) : ( <>
{editing ? ( setFormData((p) => ({ ...p, fullName: e.target.value }))} /> ) : (

{user?.fullName ?? '—'}

)}

{user?.phone ?? '—'}

Số điện thoại không thể thay đổi

{editing ? ( setFormData((p) => ({ ...p, email: e.target.value }))} placeholder="email@example.com" /> ) : (

{user?.email ?? 'Chưa cập nhật'}

)}

{user?.role === 'AGENT' ? 'Môi giới' : user?.role === 'ADMIN' ? 'Quản trị viên' : user?.role === 'SELLER' ? 'Người bán' : 'Người mua'}

{editing && (
)} )}
{/* Status sidebar */}
Trạng thái tài khoản
Tài khoản {user?.isActive ? 'Hoạt động' : 'Bị khóa'}
Xác minh KYC {kycInfo.label}
{user?.kycStatus !== 'VERIFIED' && ( )}
Tham gia {user?.createdAt ? new Date(user.createdAt).toLocaleDateString('vi-VN') : '—'}
{/* Agent details */} {agentProfile && ( Thông tin môi giới
Mã chứng chỉ {agentProfile.licenseNumber ?? 'Chưa có'}
Công ty {agentProfile.agency ?? 'Độc lập'}
{agentProfile.qualityScore != null && (
Điểm chất lượng {agentProfile.qualityScore}/100
)}
Xác minh {agentProfile.isVerified ? 'Đã xác minh' : 'Chưa xác minh'}
{agentProfile.serviceAreas.length > 0 && (
Khu vực hoạt động
{agentProfile.serviceAreas.map((area) => ( {area} ))}
)}
)}
); }