import React, { useState } from 'react' import { useParams, useNavigate, Link } from 'react-router-dom' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { FaArrowLeft, FaSave, FaTimes, FaBuilding, FaUser, FaMapMarkerAlt, FaIdCard, FaCreditCard, FaExclamationTriangle, } from 'react-icons/fa' import classNames from 'classnames' import { CustomerSegmentEnum, CustomerTypeEnum } from '../../../types/crm' import { mockBusinessParties, mockBusinessPartyNew } from '../../../mocks/mockBusinessParties' import { BusinessParty, BusinessPartyStatusEnum, PaymentTerms } from '../../../types/common' import { Container } from '@/components/shared' import { ROUTES_ENUM } from '@/routes/route.constant' import { getBusinessPartyStatusText, getCustomerSegmentText, getCustomerTypeText, getPaymentTermsText, } from '@/utils/erp' import { mockCurrencies } from '@/mocks/mockCurrencies' const CustomerEdit: React.FC = () => { const { id } = useParams<{ id: string }>() const navigate = useNavigate() const queryClient = useQueryClient() const [activeTab, setActiveTab] = useState('basic') const [isDirty, setIsDirty] = useState(false) const { data: customer, isLoading, error, } = useQuery({ queryKey: ['customer', id], queryFn: async () => { await new Promise((resolve) => setTimeout(resolve, 300)) const found = mockBusinessParties.find((c) => c.id === id) if (!found) throw new Error('Müşteri bulunamadı') return found }, }) const [formData, setFormData] = useState(mockBusinessPartyNew) // Initialize form data when customer is loaded React.useEffect(() => { if (customer) { setFormData(mockBusinessParties.find((c) => c.id === id) || mockBusinessPartyNew) } }, [customer, id]) const updateMutation = useMutation({ mutationFn: async (data: BusinessParty) => { await new Promise((resolve) => setTimeout(resolve, 1000)) // Simulate API call return { ...customer, ...data } }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['customers'] }) queryClient.invalidateQueries({ queryKey: ['customer', id] }) setIsDirty(false) navigate(ROUTES_ENUM.protected.crm.customersDetail.replace(':id', id || 'new')) }, }) const handleInputChange = (field: string, value: string | number) => { const keys = field.split('.') if (keys.length === 2) { const [section, subField] = keys setFormData((prev: any) => { if (section === 'primaryContact') { return { ...prev, primaryContact: { ...prev.primaryContact, [subField]: value, }, } } else if (section === 'address') { return { ...prev, address: { ...prev.address, [subField]: value, }, } } return prev }) } else { setFormData((prev) => ({ ...prev, [field]: value, })) } setIsDirty(true) } const handleSubmit = (e: React.FormEvent) => { e.preventDefault() updateMutation.mutate(formData) } const handleCancel = () => { if (isDirty) { if (window.confirm('Değişiklikleriniz kaydedilmedi. Çıkmak istediğinizden emin misiniz?')) { navigate(ROUTES_ENUM.protected.crm.customersDetail.replace(':id', id!)) } } else { navigate(ROUTES_ENUM.protected.crm.customersDetail.replace(':id', id!)) } } if (isLoading) { return (
{[...Array(8)].map((_, i) => (
))}
) } if (error || !customer) { return (

Müşteri Bulunamadı

Düzenlemek istediğiniz müşteri mevcut değil.

) } const tabs = [ { id: 'basic', label: 'Temel Bilgiler', icon: FaBuilding }, { id: 'contact', label: 'İletişim', icon: FaUser }, { id: 'business', label: 'İş Bilgileri', icon: FaIdCard }, { id: 'financial', label: 'Finansal', icon: FaCreditCard }, ] return (
{/* Header */}
{/* Breadcrumb */}
Müşteriler / {customer.name} / Düzenle
{/* Header */}

Müşteri Düzenle

{customer.name}

{/* Tabs */}
{/* Form Content */}
{activeTab === 'basic' && (

Temel Bilgiler

handleInputChange('code', e.target.value)} className="w-full px-3 py-1.5 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" required />
handleInputChange('name', e.target.value)} className="w-full px-3 py-1.5 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" required />
handleInputChange('industry', e.target.value)} className="w-full px-3 py-1.5 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="Teknoloji, İmalat, Hizmet vb." />
handleInputChange('website', e.target.value)} className="w-full px-3 py-1.5 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="https://www.ornek.com" />
)} {activeTab === 'contact' && (
{/* Primary Contact */}

Ana İletişim Kişisi

handleInputChange('primaryContact.firstName', e.target.value) } className="w-full px-3 py-1.5 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" required />
handleInputChange('primaryContact.lastName', e.target.value) } className="w-full px-3 py-1.5 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" required />
handleInputChange('primaryContact.title', e.target.value)} className="w-full px-3 py-1.5 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="Genel Müdür, Satış Direktörü vb." />
handleInputChange('primaryContact.department', e.target.value) } className="w-full px-3 py-1.5 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="Satış, Pazarlama, İnsan Kaynakları vb." />
handleInputChange('primaryContact.email', e.target.value)} className="w-full px-3 py-1.5 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" required />
handleInputChange('primaryContact.phoneNumber', e.target.value)} className="w-full px-3 py-1.5 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="+90 (212) 555 0123" />
handleInputChange('primaryContact.mobile', e.target.value)} className="w-full px-3 py-1.5 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="+90 (555) 123 4567" />
{/* Address */}

Adres Bilgileri