'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import Link from 'next/link'; import { motion } from 'framer-motion'; import { User, Shield, Trash2, LogOut, ChevronLeft, Bell, Lock, HelpCircle, MessageCircle, FileText, Eye, Loader2, AlertTriangle, X } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import toast, { Toaster } from 'react-hot-toast'; import AuthService from '../services/AuthService'; import { changePassword, deleteMyAccount, submitReport } from '../utils/api'; export default function SettingsPage() { const { t, i18n } = useTranslation(); const router = useRouter(); const [showDeleteDialog, setShowDeleteDialog] = useState(false); const [deletePassword, setDeletePassword] = useState(''); const [isDeleting, setIsDeleting] = useState(false); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const [showContactDialog, setShowContactDialog] = useState(false); const [contactSubject, setContactSubject] = useState(''); const [contactBody, setContactBody] = useState(''); const [isSendingContact, setIsSendingContact] = useState(false); const handleSignOut = () => { AuthService.deleteToken(); toast.success(t('logout-success')); router.push('/'); }; const handleDeleteAccount = async () => { if (!deletePassword) { toast.error(t('enter-password-prompt')); return; } setIsDeleting(true); try { await deleteMyAccount(deletePassword); AuthService.deleteToken(); toast.success(t('delete-account-success')); router.push('/'); } catch (err) { console.error('Delete account error:', err); toast.error(err.message || t('delete-account-failed')); } finally { setIsDeleting(false); setShowDeleteDialog(false); setDeletePassword(''); } }; const handleSendContact = async () => { if (!contactSubject.trim() || !contactBody.trim()) { toast.error(t('fill-subject-and-message')); return; } setIsSendingContact(true); try { await submitReport(contactSubject.trim(), contactBody.trim()); toast.success(t('send-success')); setShowContactDialog(false); setContactSubject(''); setContactBody(''); } catch (err) { toast.error(err.message || t('send-error')); } finally { setIsSendingContact(false); } }; const sections = [ { title: t('account'), items: [ { icon: User, label: t('profile'), href: '/profile', desc: t('profileDesc') }, { icon: Lock, label: t('change-password'), href: '/change-password', desc: t('change-password-desc') }, { icon: Shield, label: t('account-verification'), href: '/account-verification', desc: t('account-verification-desc') }, ] }, { title: t('notifications'), items: [ { icon: Bell, label: t('notifications'), href: '/notifications', desc: t('notification-preferences-desc') }, ] }, { title: t('support'), items: [ { icon: HelpCircle, label: t('faq'), href: '/faq', desc: t('faq-desc') }, { icon: MessageCircle, label: t('contactUs'), desc: t('contact-us-desc'), action: () => setShowContactDialog(true) }, { icon: FileText, label: t('terms-and-conditions'), href: '/terms', desc: t('terms-desc') }, { icon: Eye, label: t('privacy'), href: '/privacy', desc: t('privacy-desc') }, ] }, ]; const containerVariants = { hidden: { opacity: 0 }, visible: { opacity: 1, transition: { staggerChildren: 0.08 } } }; const itemVariants = { hidden: { opacity: 0, y: 20 }, visible: { opacity: 1, y: 0 } }; return (
{item.label}
{item.desc}
{t('delete-account-warning')}
{t('deleteDialogWarning')}
{t('delete-account-confirm-desc')}
setDeletePassword(e.target.value)} placeholder={t('enter-password-prompt')} className="w-full px-4 py-3 border border-gray-300 rounded-xl mb-4 focus:ring-2 focus:ring-red-500 focus:border-transparent outline-none" />{t('contact-reply-prompt')}