import { useState, useMemo } from 'react' import { useNavigate } from 'react-router-dom' import { useTranslation } from 'react-i18next' import { useForm } from 'react-hook-form' import { toast } from 'sonner' import { User, MapPin, IdCard, CreditCard, Files } from 'lucide-react' import { FormInput } from '@/components/formInput' import { FormActions } from '@/components/formActions' import { StepBarCards, type StepCardItem } from '@/components/stepBarV2' import type { StepStatus } from '@/components/stepBar' import { useCreateSberPayment, useUpdateSberPayment, } from '@/features/sberPayments/hooks/useSberPayments' import { WELAYATLAR, SAHAMCALAR, STATUSES, type SberPaymentOrder, type PaymentStatus, } from '@/features/sberPayments/api/sberPaymentsApi' import { validateStep, DEFAULT_FORM_VALUES, buildPayload, type SberPaymentFormData, } from '@/features/sberPayments/schemas/sberPayment.schema' // ─── Types ──────────────────────────────────────────────────────────────────── interface SberPaymentFormProps { mode: 'create' | 'edit' initialData?: SberPaymentOrder | null orderId?: string } type FlatErrors = Partial> // ─── Flatten RHF errors ────────────────────────────────────────────────────── function flattenErrors(errors: Record): FlatErrors { const result: FlatErrors = {} for (const key of Object.keys(errors)) { const msg = errors[key]?.message if (msg) result[key as keyof SberPaymentFormData] = msg } return result } // ─── Document lists ─────────────────────────────────────────────────────────── type AcceptedDocKey = | 'acc_sberbank_card' | 'acc_enrollment' | 'acc_summons' | 'acc_passport_tm' | 'acc_zagran_passport' | 'acc_visa_page' | 'acc_entry_stamp' | 'acc_school_letter' type SentDocKey = | 'snt_passport_tm' | 'snt_zagran_passport' | 'snt_entry_stamp' | 'snt_relation_doc' | 'snt_new_passport_series' | 'snt_old_passport_series' const ACCEPTED_DOCS: { key: AcceptedDocKey; label: string }[] = [ { key: 'acc_sberbank_card', label: 'Talýba degişli SBERBANK kartynyň rekwizitleri' }, { key: 'acc_enrollment', label: 'Daşary ýurt ÝOM-da okaýandygy baradaky güwänamasy' }, { key: 'acc_summons', label: 'Çagyrylma hatynyn göçürmesi' }, { key: 'acc_passport_tm', label: 'TM içki pasportynyň asyl görnüşi we göçürmesi' }, { key: 'acc_zagran_passport', label: 'Daşary ýurt (zagran) pasportynyň göçürmesi' }, { key: 'acc_visa_page', label: 'Daşary ýurda rugsat (wizasy) bellenen sahypasynyň göçürmesi' }, { key: 'acc_entry_stamp', label: 'Daşary ýurt döwletine girenliği baradaky ştamply sahypasynyň göçürmesi' }, { key: 'acc_school_letter', label: 'ÝOM-dan hat (daşary ýurt dilinde maglumatly)' }, ] const SENT_DOCS: { key: SentDocKey; label: string }[] = [ { key: 'snt_passport_tm', label: 'Ugradyjynyň TM içki pasportynyň asyl görnüşi we göçürmesi' }, { key: 'snt_zagran_passport', label: 'Ugradyjynyň daşary ýurt pasportynyň asyl görnüşi we göçürmesi' }, { key: 'snt_entry_stamp', label: 'Ugradyjynyň daşary döwletine girenliği ştamply sahypasynyň göçürmesi' }, { key: 'snt_relation_doc', label: 'Ugradyjy we kabul edijiniň garyndaşlyk tassyklaýjy resminamasynyn göçürmesi' }, { key: 'snt_new_passport_series', label: 'Ugradyjy/kabul ediji täze (2015+) pasport seriýasy maglumaty' }, { key: 'snt_old_passport_series', label: 'Ugradyjy/kabul ediji köne pasport seriýasy baradaky güwänamasy' }, ] // ─── Bento primitives ───────────────────────────────────────────────────────── function BentoGrid({ cols = 2, children }: { cols?: 1 | 2; children: React.ReactNode }) { return (
{children}
) } function BentoCard({ title, span, children, }: { title?: string span?: 'full' children: React.ReactNode }) { return (
{title && (

{title}

)} {children}
) } // ─── Step panels ────────────────────────────────────────────────────────────── function StepGeneral({ form, errors, set, }: { form: SberPaymentFormData errors: FlatErrors set: (k: K, v: SberPaymentFormData[K]) => void }) { return ( set('client_id', v)} options={[]} placeholder="Saýlamak üçin basyň" required /> set('status', v as PaymentStatus)} options={STATUSES} error={errors.status} required /> set('bellik', v)} placeholder="Bellik..." rows={2} /> ) } function StepLocation({ form, errors, set, }: { form: SberPaymentFormData errors: FlatErrors set: (k: K, v: SberPaymentFormData[K]) => void }) { const branches = form.welayat ? (SAHAMCALAR[form.welayat] ?? []) : [] return ( { set('welayat', v); set('sahamca', '') }} options={WELAYATLAR.map((w) => ({ value: w, label: w }))} placeholder="Aşgabat" error={errors.welayat} required />

Welaýaty saýlasaňyz şahamçalar güncellenar.

set('sahamca', v)} options={branches.map((b) => ({ value: b, label: b }))} placeholder="Saýlamak üçin basyň" disabled={!form.welayat} error={errors.sahamca} required />
) } function StepPersonal({ form, errors, set, }: { form: SberPaymentFormData errors: FlatErrors set: (k: K, v: SberPaymentFormData[K]) => void }) { return ( set('lastName', v)} placeholder="NURYYEW" error={errors.lastName} required /> set('firstName', v)} placeholder="HAÝDAR" error={errors.firstName} required /> set('phone', v)} error={errors.phone} required /> set('email', v)} placeholder="email@example.com" /> set('address', v)} placeholder="Köçe, jaý belgisi..." error={errors.address} required /> ) } function StepPayment({ form, errors, set, }: { form: SberPaymentFormData errors: FlatErrors set: (k: K, v: SberPaymentFormData[K]) => void }) { return ( set('passportSeries', v)} options={[ { value: 'II-MA', label: 'II-MA' }, { value: 'II-MB', label: 'II-MB' }, { value: 'II-MC', label: 'II-MC' }, { value: 'II-MD', label: 'II-MD' }, ]} placeholder="Saýlamak üçin basyň" error={errors.passportSeries} required /> set('passportNumber', v)} placeholder="A123456" error={errors.passportNumber} required /> set('fullName', v)} placeholder="Doly ady..." error={errors.fullName} required /> set('accountNumber', v)} placeholder="1234 5678 ..." error={errors.accountNumber} required /> ) } function StepDocs({ form, set, }: { form: SberPaymentFormData set: (k: K, v: SberPaymentFormData[K]) => void }) { return (
{ACCEPTED_DOCS.map(({ key, label }) => ( set(key, f)} fileValue={form[key] as File | null} accept="image/*,.pdf" /> ))}
{SENT_DOCS.map(({ key, label }) => ( set(key, f)} fileValue={form[key] as File | null} accept="image/*,.pdf" /> ))}
) } // ─── Step definitions ───────────────────────────────────────────────────────── type StepId = 'general' | 'location' | 'personal' | 'payment' | 'docs' const STEPS: { id: StepId; title: string; subtitle: string }[] = [ { id: 'general', title: 'Esasy', subtitle: 'Status, müşderi' }, { id: 'location', title: 'Lokasiýa', subtitle: 'Welaýat, şahamça' }, { id: 'personal', title: 'Şahsy', subtitle: 'Pasport, kontakt' }, { id: 'payment', title: 'Töleg', subtitle: 'Hasap, ugradyjy' }, { id: 'docs', title: 'Resminamalar', subtitle: '14 resminama' }, ] // ─── Initial data mapper ────────────────────────────────────────────────────── function mapInitialData(data: SberPaymentOrder): Partial { return { welayat: data.welayat, sahamca: data.sahamca, firstName: data.firstName, lastName: data.lastName, phone: data.phone, email: data.email, address: data.address, status: data.status, bellik: data.bellik, accountNumber: data.accountNumber, passportSeries: data.passportSeries, passportNumber: data.passportNumber, fullName: data.fullName, } } // ─── Main component ─────────────────────────────────────────────────────────── export function SberPaymentForm({ mode, initialData, orderId }: SberPaymentFormProps) { const { t } = useTranslation() const navigate = useNavigate() const createMutation = useCreateSberPayment() const updateMutation = useUpdateSberPayment() const isLoading = createMutation.isPending || updateMutation.isPending const { watch, setValue, getValues, formState: { errors: rhfErrors }, clearErrors, } = useForm({ defaultValues: mode === 'edit' && initialData ? { ...DEFAULT_FORM_VALUES, ...mapInitialData(initialData) } : DEFAULT_FORM_VALUES, }) const form = watch() const errors = useMemo(() => flattenErrors(rhfErrors as Record), [rhfErrors]) const [currentStep, setCurrentStep] = useState(0) const [stepStatuses, setStepStatuses] = useState( () => mode === 'edit' ? STEPS.map((_, i) => (i === 0 ? 'active' : i < 4 ? 'done' : 'idle')) : STEPS.map((_, i) => (i === 0 ? 'active' : 'idle')), ) // ── Field setter ── const set = (key: K, value: SberPaymentFormData[K]) => { (setValue as (name: K, val: SberPaymentFormData[K]) => void)(key, value) clearErrors(key) } // ── Step validation ── const doValidateAll = (): FlatErrors => { const errs: FlatErrors = {} for (let i = 0; i <= 3; i++) { Object.assign(errs, validateStep(i, form, mode)) } return errs } // ── Navigation ── const goStep = (idx: number) => { if (idx < 0 || idx >= STEPS.length) return setStepStatuses((prev) => { const next = [...prev] if (idx > currentStep) next[currentStep] = 'done' next[idx] = 'active' if (idx < currentStep) { for (let i = idx + 1; i <= currentStep; i++) { if (next[i] !== 'done') next[i] = 'idle' } } return next }) clearErrors() setCurrentStep(idx) } const handleNext = () => { const stepErrors = validateStep(currentStep, form, mode) if (Object.keys(stepErrors).length > 0) { setStepStatuses((prev) => { const next = [...prev]; next[currentStep] = 'error'; return next }) return } if (currentStep === STEPS.length - 1) { handleSubmit() } else { goStep(currentStep + 1) } } // ── Submit ── const handleSubmit = async () => { const allErrors = doValidateAll() if (Object.keys(allErrors).length > 0) { toast.error(t('common.errors.requiredFieldsMissing', 'Meýdanlary dolduryň')) return } const payload = buildPayload(getValues()) if (mode === 'create') { await createMutation.mutateAsync(payload) toast.success(t('sberPayments.toast.created', 'Töleg üstünlikli döredildi')) navigate('/sber-payments') } else if (orderId) { await updateMutation.mutateAsync({ ...payload, id: orderId }) toast.success(t('sberPayments.toast.updated', 'Töleg üstünlikli täzelendi')) navigate(`/sber-payments/${orderId}`) } } // ── StepBarCards data ── const stepCardItems: StepCardItem[] = STEPS.map((s, i) => ({ id: s.id, title: s.title, subtitle: s.subtitle, status: stepStatuses[i], icon: ([User, MapPin, IdCard, CreditCard, Files] as const)[i], })) const isLastStep = currentStep === STEPS.length - 1 const submitLabel = mode === 'create' ? 'Sber töleg (talyplar üçin) dörediň' : 'Ýatda sakla' return (

{mode === 'create' ? 'Sber töleg dörediň' : 'Sber töleg redaktirläň'}

{/* ── Step bar ──────────────────────────────────────────────────────── */}
{ if (stepStatuses[i] !== 'idle') goStep(i) }} />
{/* ── Step content ─────────────────────────────────────────────────── */} {STEPS[currentStep].id === 'general' && } {STEPS[currentStep].id === 'location' && } {STEPS[currentStep].id === 'personal' && } {STEPS[currentStep].id === 'payment' && } {STEPS[currentStep].id === 'docs' && } {/* ── Actions ───────────────────────────────────────────────────────── */} navigate('/sber-payments')} onPrev={currentStep > 0 ? () => goStep(currentStep - 1) : undefined} onNext={!isLastStep ? handleNext : undefined} showSubmit={isLastStep} onSubmit={isLastStep ? handleNext : undefined} submitLabel={submitLabel} loadingLabel="Ýüklenýär..." />
) }