'use client'; import { useState, useEffect } from 'react'; import { useTranslation } from '@/lib/i18n'; import { useAuthStore } from '@/lib/store'; import { inquiryApi, Inquiry, InquiryWithMessages } from '@/lib/api'; const CATEGORY_LABELS: Record> = { ko: { general: '일반 문의', vehicle: '차량 문의', payment: '결제 문의', shipping: '배송 문의', dealer: '딜러 문의', account: '계정 문의', other: '기타', }, en: { general: 'General', vehicle: 'Vehicle', payment: 'Payment', shipping: 'Shipping', dealer: 'Dealer', account: 'Account', other: 'Other', }, }; const STATUS_LABELS: Record> = { ko: { pending: '대기중', in_progress: '처리중', resolved: '해결됨', closed: '종료', }, en: { pending: 'Pending', in_progress: 'In Progress', resolved: 'Resolved', closed: 'Closed', }, }; const STATUS_COLORS: Record = { pending: 'bg-yellow-100 text-yellow-800', in_progress: 'bg-blue-100 text-blue-800', resolved: 'bg-green-100 text-green-800', closed: 'bg-gray-100 text-gray-800', }; export default function ContactPage() { const { t, language } = useTranslation(); const { user } = useAuthStore(); const [formData, setFormData] = useState({ name: '', email: '', phone: '', category: 'general', subject: '', message: '', }); const [isSubmitting, setIsSubmitting] = useState(false); const [isSubmitted, setIsSubmitted] = useState(false); const [error, setError] = useState(''); // Inquiry history state const [myInquiries, setMyInquiries] = useState([]); const [loadingInquiries, setLoadingInquiries] = useState(false); const [selectedInquiry, setSelectedInquiry] = useState(null); const [showDetailModal, setShowDetailModal] = useState(false); const [replyMessage, setReplyMessage] = useState(''); const [sendingReply, setSendingReply] = useState(false); const [activeTab, setActiveTab] = useState<'form' | 'history'>('form'); // Fetch user's inquiries useEffect(() => { if (user) { fetchMyInquiries(); } }, [user]); const fetchMyInquiries = async () => { setLoadingInquiries(true); try { const response = await inquiryApi.getMyInquiries(1, 50); setMyInquiries(response.inquiries); } catch (error) { console.error('Failed to fetch inquiries:', error); } finally { setLoadingInquiries(false); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); setError(''); try { await inquiryApi.createInquiry({ category: formData.category, subject: formData.subject || `${CATEGORY_LABELS[language]?.[formData.category] || formData.category} ${language === 'ko' ? '문의' : 'Inquiry'}`, message: formData.message, contact_email: formData.email || user?.email, contact_phone: formData.phone || user?.phone, }); setIsSubmitted(true); setFormData({ name: '', email: '', phone: '', category: 'general', subject: '', message: '' }); if (user) { fetchMyInquiries(); } } catch (err: any) { console.error('Failed to submit inquiry:', err); setError(language === 'ko' ? '문의 등록에 실패했습니다. 다시 시도해주세요.' : 'Failed to submit inquiry. Please try again.'); } finally { setIsSubmitting(false); } }; const openInquiryDetail = async (inquiry: Inquiry) => { try { const detail = await inquiryApi.getInquiryDetail(inquiry.id); setSelectedInquiry(detail); setShowDetailModal(true); } catch (error) { console.error('Failed to fetch inquiry detail:', error); } }; const handleSendReply = async () => { if (!selectedInquiry || !replyMessage.trim()) return; setSendingReply(true); try { await inquiryApi.addMessage(selectedInquiry.inquiry.id, replyMessage.trim()); const detail = await inquiryApi.getInquiryDetail(selectedInquiry.inquiry.id); setSelectedInquiry(detail); setReplyMessage(''); } catch (error) { console.error('Failed to send reply:', error); } finally { setSendingReply(false); } }; const formatDate = (dateString: string) => { return new Date(dateString).toLocaleDateString(language === 'ko' ? 'ko-KR' : 'en-US', { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }); }; const categoryLabels = CATEGORY_LABELS[language] || CATEGORY_LABELS.en; const statusLabels = STATUS_LABELS[language] || STATUS_LABELS.en; return (
{/* Hero Section */}

{t.contactUs}

{language === 'ko' ? '궁금한 점이 있으시면 언제든지 문의해 주세요' : 'Feel free to contact us anytime'}

{/* Tabs for logged in users */} {user && (
)} {/* Inquiry History Tab */} {user && activeTab === 'history' && (

{language === 'ko' ? '내 문의 내역' : 'My Inquiries'}

{loadingInquiries ? (
) : myInquiries.length === 0 ? (
{language === 'ko' ? '문의 내역이 없습니다.' : 'No inquiries yet.'}
) : (
{myInquiries.map((inquiry) => (
openInquiryDetail(inquiry)} className="border rounded-lg p-4 hover:bg-gray-50 cursor-pointer transition" >
{categoryLabels[inquiry.category] || inquiry.category} {statusLabels[inquiry.status] || inquiry.status}
{formatDate(inquiry.created_at)}

{inquiry.subject || (language === 'ko' ? '제목 없음' : 'No subject')}

{inquiry.message}

{inquiry.admin_response && (

{language === 'ko' ? '답변 완료' : 'Answered'}

)}
))}
)}
)} {/* Form Tab */} {(!user || activeTab === 'form') && (
{/* Left Column - Company Info */}

{t.getInTouch}

{language === 'ko' ? '주소' : 'Address'}

{t.companyAddress}

{t.telephone}

+82-2-552-0773

{t.emailAddress}

sshong@grantech.kr

{t.businessHours}

{t.businessHoursValue}

{/* Map */}