import { useState, useCallback, useMemo } from "react"; import { useNavigate } from "react-router-dom"; import { useTranslation } from "react-i18next"; import type { TFunction } from "i18next"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { CircleDot, CreditCard, MapPin, User, Wallet, FileText, Phone, Briefcase, FolderOpen, ShieldCheck } from "lucide-react"; import type { LucideIcon } from "lucide-react"; import { FormInput } from "@/components/formInput"; import { FormActions } from "@/components/formActions"; import { StepBarCards, type StepCardItem } from "@/components/stepBarV2"; import { BentoGrid, BentoCard } from "@/components/bento"; import { useCreateLoanOrder, useUpdateLoanOrder } from "@/features/loanOrders/hooks/useLoanOrders"; import type { LoanOrder } from "@/features/loanOrders/api/loanOrdersApi"; import { BRANCHES_BY_REGION } from "@/features/loanOrders/api/loanOrdersApi"; import { validateStep, DEFAULT_FORM_VALUES, buildPayload } from "@/features/loanOrders/schemas/loanOrder.schema"; import type { LoanOrderFormData } from "@/features/loanOrders/schemas/loanOrder.schema"; import { PASSPORT_SERIES_OPTIONS, getStatusOptions } from "@/constants"; // ─── Static options ─────────────────────────────────────────────────────────── const REGION_OPTIONS = [ { value: "Aşgabat", label: "Aşgabat" }, { value: "Ahal", label: "Ahal" }, { value: "Balkan", label: "Balkan" }, { value: "Daşoguz", label: "Daşoguz" }, { value: "Lebap", label: "Lebap" }, { value: "Mary", label: "Mary" }, { value: "Arkadag", label: "Arkadag" }, ]; const EDUCATION_OPTIONS = [ { value: "high", label: "Ýokary bilim" }, { value: "unfinished_high", label: "Gutarylmadyk ýokary bilim" }, { value: "masters", label: "Magistr" }, { value: "phd", label: "Ylymlaryň doktory" }, { value: "middle", label: "Orta mekdep" }, { value: "school", label: "Orta bilim" }, { value: "school_dropout", label: "Gutarylmadyk orta bilim" }, ]; const MARRIAGE_OPTIONS = [ { value: "MARRIED", label: "Öýlenen / Durmuşa çykan" }, { value: "SINGLE", label: "Öýlenmedik / Durmuşa çykmadyk" }, { value: "DIVORCED", label: "Aýrylşan" }, { value: "WIDOW", label: "Adamsy ýa-da aýaly aradan çykan" }, { value: "LEGAL", label: "Raýat nika" }, ]; const LOAN_TYPE_OPTIONS = [ { value: "consumer", label: "Sarp ediş karzy" }, { value: "mortgage", label: "Ipoteka" }, { value: "car", label: "Awtoulag karzy" }, { value: "student", label: "Talyplar üçin" }, { value: "business", label: "Işewürlik karzy" }, ]; const MONTH_OPTIONS = Array.from({ length: 12 }, (_, i) => ({ value: String(i + 1).padStart(2, "0"), label: String(i + 1).padStart(2, "0"), })); const YEAR_OPTIONS = Array.from({ length: 15 }, (_, i) => ({ value: String(new Date().getFullYear() + i), label: String(new Date().getFullYear() + i), })); // ─── Form errors helper ────────────────────────────────────────────────────── type FlatErrors = Partial>; 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 LoanOrderFormData] = msg; } return result; } // ─── Step definitions ───────────────────────────────────────────────────────── interface StepDef { id: string; titleKey: string; titleFallback: string; subtitleKey: string; subtitleFallback: string; icon: LucideIcon; validate: (form: LoanOrderFormData, mode: "create" | "edit") => FlatErrors; } const STEPS: StepDef[] = [ { id: "status", titleKey: "Status", titleFallback: "Status", subtitleKey: "loanOrderForm.steps.status.subtitle", subtitleFallback: "Ýagdaý we bellik", icon: CircleDot, validate: (form, mode) => validateStep(0, form, mode), }, { id: "loan", titleKey: "Loan", titleFallback: "Karz", subtitleKey: "loanOrderForm.steps.loan.subtitle", subtitleFallback: "Görnüş we möçber", icon: Wallet, validate: (form, mode) => validateStep(1, form, mode), }, { id: "location", titleKey: "Location", titleFallback: "Lokasiýa", subtitleKey: "loanOrderForm.steps.location.subtitle", subtitleFallback: "Welaýat we şahamça", icon: MapPin, validate: (form, mode) => validateStep(2, form, mode), }, { id: "personal", titleKey: "Personal data", titleFallback: "Şahsy maglumatlar", subtitleKey: "loanOrderForm.steps.personal.subtitle", subtitleFallback: "At, bilim, maşgala", icon: User, validate: (form, mode) => validateStep(3, form, mode), }, { id: "card", titleKey: "Card (Salary)", titleFallback: "Kart (Zähmet haky)", subtitleKey: "loanOrderForm.steps.card.subtitle", subtitleFallback: "Zähmet haky karty", icon: CreditCard, validate: (form, mode) => validateStep(4, form, mode), }, { id: "passport", titleKey: "Passport", titleFallback: "Pasport", subtitleKey: "loanOrderForm.steps.passport.subtitle", subtitleFallback: "Seriýa, belgisi, senesi", icon: FileText, validate: (form, mode) => validateStep(5, form, mode), }, { id: "contact", titleKey: "Contact data", titleFallback: "Habarlaşmak", subtitleKey: "loanOrderForm.steps.contact.subtitle", subtitleFallback: "Telefon we e-poçta", icon: Phone, validate: (form, mode) => validateStep(6, form, mode), }, { id: "job", titleKey: "Job", titleFallback: "Iş", subtitleKey: "loanOrderForm.steps.job.subtitle", subtitleFallback: "Kärhana we wezipe", icon: Briefcase, validate: (form, mode) => validateStep(7, form, mode), }, { id: "files", titleKey: "Passport files", titleFallback: "Pasport faýllar", subtitleKey: "loanOrderForm.steps.files.subtitle", subtitleFallback: "Pasport suratlary", icon: FolderOpen, validate: (form, mode) => validateStep(8, form, mode), }, { id: "guarantor", titleKey: "1. Guarantor", titleFallback: "1. Zamun", subtitleKey: "loanOrderForm.steps.guarantor.subtitle", subtitleFallback: "Zamun maglumatlary", icon: ShieldCheck, validate: (form, mode) => validateStep(9, form, mode), }, ]; // ─── Shared step content props ──────────────────────────────────────────────── interface StepContentProps { form: LoanOrderFormData; errors: FlatErrors; set: (k: K, v: LoanOrderFormData[K]) => void; t: TFunction; } // ─── Step panels ────────────────────────────────────────────────────────────── function StepStatus({ form, errors, set, t }: StepContentProps) { const statusOptions = useMemo(() => getStatusOptions(t, "loanOrderStatus"), [t]); return ( set("status", v)} options={statusOptions} error={errors.status} placeholder={t("loanOrderForm.placeholders.select") || "Saýlaň"} /> set("note", v)} placeholder={t("loanOrderForm.placeholders.note") || "Bellik..."} rows={3} /> ); } function StepLoan({ form, errors, set, t }: StepContentProps) { return ( set("loanType", v)} options={LOAN_TYPE_OPTIONS} placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"} error={errors.loanType} /> set("loanAmount", v)} placeholder={t("loanOrderForm.placeholders.amount") || "0.00"} error={errors.loanAmount} /> ); } function StepLocation({ form, errors, set, t }: StepContentProps) { const branchOptions = BRANCHES_BY_REGION[form.region] ?? []; return ( { set("region", v); set("branch", ""); }} options={REGION_OPTIONS} placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"} error={errors.region} /> set("branch", v)} options={branchOptions} placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"} disabled={!form.region} error={errors.branch} /> ); } function StepPersonal({ form, errors, set, t }: StepContentProps) { return (
set("firstName", v)} placeholder={t("Name") || "Ady"} error={errors.firstName} /> set("lastName", v)} placeholder={t("Surname") || "Familiýasy"} error={errors.lastName} /> set("patronicName", v)} placeholder={t("Patronic name") || "Atasynyň ady"} /> set("education", v)} options={EDUCATION_OPTIONS} placeholder={t("loanOrderForm.placeholders.select") || "Saýlaň"} error={errors.education} /> set("marriageStatus", v)} options={MARRIAGE_OPTIONS} placeholder={t("loanOrderForm.placeholders.select") || "Saýlaň"} error={errors.marriageStatus} /> set("dateOfBirth", v)} error={errors.dateOfBirth} /> set("residence", v)} placeholder={t("loanOrderForm.placeholders.residence") || "Ýazgy edilen salgy"} error={errors.residence} /> set("currentResidence", v)} placeholder={t("loanOrderForm.placeholders.currentResidence") || "Häzirki ýaşaýyş ýeri"} /> set("loanHistory", v)} options={[]} placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"} />
); } function StepCard({ form, errors, set, t }: StepContentProps) { return ( set("cardNumber", v)} placeholder={t("Card number") || "Kart belgisi"} error={errors.cardNumber} /> set("cardName", v)} placeholder={t("Name on card") || "Kartdaky ady"} error={errors.cardName} /> set("cardExpMonth", v)} options={MONTH_OPTIONS} placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"} error={errors.cardExpMonth} /> set("cardExpYear", v)} options={YEAR_OPTIONS} placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"} error={errors.cardExpYear} /> ); } function StepPassport({ form, errors, set, t }: StepContentProps) { return (
set("passportSerie", v)} options={PASSPORT_SERIES_OPTIONS} placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"} error={errors.passportSerie} /> set("passportNumber", v)} placeholder={t("Passport id") || "Pasport belgisi"} error={errors.passportNumber} /> set("passportDateOfIssue", v)} error={errors.passportDateOfIssue} /> set("passportGivenBy", v)} placeholder={t("Passport given by") || "Kim tarapyndan berildi"} error={errors.passportGivenBy} /> set("bornPlace", v)} placeholder={t("Born place (passport)") || "Doglan ýeri (pasport)"} />
); } function StepContact({ form, errors, set, t }: StepContentProps) { return ( set("phone", v)} placeholder={t("loanOrderForm.placeholders.phone") || "61 097 651"} error={errors.phone} /> set("email", v)} placeholder={t("Email") || "E-poçta"} /> set("phoneAdditional", v)} placeholder={t("loanOrderForm.placeholders.phone") || "61 097 651"} error={errors.phoneAdditional} /> set("homePhone", v)} placeholder={t("loanOrderForm.placeholders.phone") || "61 097 651"} error={errors.homePhone} /> ); } function StepJob({ form, errors, set, t }: StepContentProps) { return (
set("workCompany", v)} placeholder={t("loanOrderForm.placeholders.companyName") || "Kärhananyň ady"} error={errors.workCompany} /> set("workHrPhone", v)} placeholder={t("loanOrderForm.placeholders.phone") || "61 097 651"} error={errors.workHrPhone} /> set("workRegion", v)} options={REGION_OPTIONS} placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"} /> set("workProvince", v)} options={[]} placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"} /> set("position", v)} placeholder={t("Position") || "Wezipe"} error={errors.position} /> set("salary", v)} placeholder={t("Salary") || "Zähmet haky"} error={errors.salary} /> set("workStartedAt", v)} error={errors.workStartedAt} />
); } function StepFiles({ form, errors, set, t, mode, initialData }: StepContentProps & { mode: "create" | "edit"; initialData?: LoanOrder }) { const existingFiles = mode === "edit" && initialData ? [ { url: initialData.passportPage1Url, label: t("Passport (page 1)") || "Pasport (sahypa 1)" }, { url: initialData.passportPage23Url, label: t("Passport (page 2-3)") || "Pasport (2-3-nji sahypa)" }, { url: initialData.passportPage89Url, label: t("Passport (page 8-9)") || "Pasport (8-9 sahypa)" }, { url: initialData.passportPage32Url, label: t("Passport (page 32)") || "Pasport (32-nji sahypa)" }, ].filter((f): f is { url: string; label: string } => !!f.url) : []; return (
{existingFiles.length > 0 && ( {existingFiles.map(({ url, label }) => ( {t("loanOrderForm.fileLabels.viewFile") || "Faýly gör"} ))} )} set("passportPage1", f)} error={errors.passportPage1} /> set("passportPage23", f)} error={errors.passportPage23} /> set("passportPage89", f)} /> set("passportPage32", f)} />
); } function StepGuarantor({ form, errors, set, t }: StepContentProps) { return (
set("guarantor1Name", v)} placeholder={t("Guarantor name") || "Zamunyň ady"} error={errors.guarantor1Name} /> set("guarantor1Surname", v)} placeholder={t("Guarantor Surname") || "Zamunyň familiýasy"} error={errors.guarantor1Surname} /> set("guarantor1Patronic", v)} placeholder={t("Guarantor Patronic name") || "Zamunyň atasynyň ady"} /> set("guarantor1PassportSerie", v)} options={PASSPORT_SERIES_OPTIONS} placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"} error={errors.guarantor1PassportSerie} /> set("guarantor1PassportNumber", v)} placeholder={t("Passport id") || "Pasport belgisi"} error={errors.guarantor1PassportNumber} /> set("guarantor1CardNumber", v)} placeholder={t("Card number") || "Kart belgisi"} error={errors.guarantor1CardNumber} /> set("guarantor1CardName", v)} placeholder={t("Name on card") || "Kartdaky ady"} error={errors.guarantor1CardName} /> set("guarantor1CardExpMonth", v)} options={MONTH_OPTIONS} placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"} error={errors.guarantor1CardExpMonth} /> set("guarantor1CardExpYear", v)} options={YEAR_OPTIONS} placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"} error={errors.guarantor1CardExpYear} /> set("guarantor1Salary", v)} placeholder={t("loanOrderForm.labels.guarantor1Salary") || "Ortaca zähmet haky"} error={errors.guarantor1Salary} />
); } // ─── Props ──────────────────────────────────────────────────────────────────── interface LoanOrderFormProps { mode: "create" | "edit"; initialData?: LoanOrder; } // ─── Main Component ─────────────────────────────────────────────────────────── export function LoanOrderForm({ mode, initialData }: LoanOrderFormProps) { const { t, i18n } = useTranslation(); const navigate = useNavigate(); const createMutation = useCreateLoanOrder(); const updateMutation = useUpdateLoanOrder(); const isPending = createMutation.isPending || updateMutation.isPending; const { watch, setValue, getValues, formState: { errors: rhfErrors }, clearErrors, setError, } = useForm({ defaultValues: initialData ? { ...DEFAULT_FORM_VALUES, ...mapInitial(initialData) } : DEFAULT_FORM_VALUES, }); const form = watch(); const [currentStep, setCurrentStep] = useState(0); const [visited, setVisited] = useState>(() => (mode === "edit" ? new Set(STEPS.map((_, i) => i)) : new Set())); const [submittedSteps, setSubmittedSteps] = useState>(new Set()); const stepsWithErrors = useMemo(() => { const out = new Set(); visited.forEach((i) => { if (Object.keys(STEPS[i].validate(form, mode)).length > 0) out.add(i); }); return out; }, [form, mode, visited, i18n.language]); const set = useCallback( (key: K, value: LoanOrderFormData[K]) => { (setValue as (name: K, val: LoanOrderFormData[K]) => void)(key, value); clearErrors(key); }, [setValue, clearErrors], ); const allSubmittedErrors = useMemo(() => { const result: FlatErrors = {}; for (const stepIdx of submittedSteps) { Object.assign(result, STEPS[stepIdx].validate(form, mode)); } return result; }, [form, mode, submittedSteps, i18n.language]); const errors = useMemo(() => { const fromRHF = flattenErrors(rhfErrors as Record); return { ...fromRHF, ...allSubmittedErrors }; }, [rhfErrors, allSubmittedErrors]); const stepProps = useMemo(() => ({ form, errors, set, t }), [form, errors, set, t]); // ── Navigation ────────────────────────────────────────────────────────────── const markVisited = (i: number) => setVisited((prev) => new Set([...prev, i])); const markSubmitted = (i: number) => setSubmittedSteps((prev) => new Set([...prev, i])); const handleNext = () => { markVisited(currentStep); markSubmitted(currentStep); const errs = STEPS[currentStep].validate(form, mode); if (Object.keys(errs).length > 0) { Object.entries(errs).forEach(([key, msg]) => { setError(key as keyof LoanOrderFormData, { type: "manual", message: msg }); }); toast.error(t("common.errors.fillRequiredCorrectly", "Dogry maglumat girizmegiňizi haýyş edýäris.")); return; } if (currentStep < STEPS.length - 1) { setCurrentStep(currentStep + 1); window.scrollTo({ top: 0, behavior: "smooth" }); } }; const handleBack = () => { if (currentStep > 0) { markVisited(currentStep); setCurrentStep(currentStep - 1); window.scrollTo({ top: 0, behavior: "smooth" }); } }; const handleGoTo = (i: number) => { markVisited(currentStep); setCurrentStep(i); window.scrollTo({ top: 0, behavior: "smooth" }); }; // ── Submit ────────────────────────────────────────────────────────────────── const doSubmit = () => { setVisited(new Set(STEPS.map((_, i) => i))); setSubmittedSteps(new Set(STEPS.map((_, i) => i))); const allErrors: FlatErrors = {}; for (const step of STEPS) Object.assign(allErrors, step.validate(form, mode)); if (Object.keys(allErrors).length > 0) { Object.entries(allErrors).forEach(([key, msg]) => { setError(key as keyof LoanOrderFormData, { type: "manual", message: msg }); }); toast.error(t("common.errors.requiredFieldsMissing", "Käbir hökmany meýdanlar doldurylan däldir.")); for (let i = 0; i < STEPS.length; i++) { if (Object.keys(STEPS[i].validate(form, mode)).length > 0) { setCurrentStep(i); break; } } return; } const payload = buildPayload(getValues()); if (mode === "create") { createMutation.mutate(payload, { onSuccess: () => navigate("/loan-orders") }); } else if (initialData) { updateMutation.mutate({ id: initialData.id, payload }, { onSuccess: () => navigate("/loan-orders") }); } }; // ── StepBar items ─────────────────────────────────────────────────────────── const stepBarItems: StepCardItem[] = STEPS.map((s, i) => { const isActive = i === currentStep; const hasErrors = stepsWithErrors.has(i); const isDone = visited.has(i) && !hasErrors; return { id: s.id, title: t(s.titleKey) || s.titleFallback, subtitle: t(s.subtitleKey) || s.subtitleFallback, icon: s.icon, status: isActive ? "active" : hasErrors ? "error" : isDone ? "done" : "idle", }; }); const isLastStep = currentStep === STEPS.length - 1; // ── Render ────────────────────────────────────────────────────────────────── return (
{/* Page header */}

{mode === "create" ? t("loanOrders.create") || "Karz sargyt döredüň" : t("loanOrders.edit") || "Karz sargydy üýtget"}

{mode === "create" ? t("loanOrders.createDescription") || "Ähli meýdanlary dolduryp, ädim-ädim öň geçiň." : t("loanOrders.editDescription") || "Ähli meýdanlary dolduryp, ädim-ädim öň geçiň."}

{/* Step bar */}
{/* Step content */} {currentStep === 0 && } {currentStep === 1 && } {currentStep === 2 && } {currentStep === 3 && } {currentStep === 4 && } {currentStep === 5 && } {currentStep === 6 && } {currentStep === 7 && } {currentStep === 8 && } {currentStep === 9 && } {/* Actions */} navigate(-1) : undefined} onPrev={currentStep > 0 ? handleBack : undefined} onNext={!isLastStep ? handleNext : undefined} showSubmit={isLastStep} onSubmit={isLastStep ? doSubmit : undefined} submitLabel={mode === "create" ? t("loanOrders.createButton") || "Karz sargyt döredüň" : t("common.save") || "Ýatda sakla"} loadingLabel={t("common.loading") || "Ýüklenilýär..."} cancelLabel={t("common.cancel") || "Ýatyr"} prevLabel={t("common.back") || "Yza"} nextLabel={t("common.next") || "Indiki"} />
); } function mapInitial(order: LoanOrder): Partial { return { status: order.status ?? "pending", loanType: order.loanType ?? "", region: order.region ?? "Aşgabat", branch: order.branch ?? "", firstName: order.firstName ?? "", lastName: order.lastName ?? "", patronicName: order.patronicName ?? "", education: order.education ?? "", marriageStatus: order.marriageStatus ?? "", dateOfBirth: order.dateOfBirth ?? "", residence: order.residence ?? "", currentResidence: order.currentResidence ?? "", passportSerie: order.passportSerie ?? "", passportNumber: order.passportNumber ?? "", passportDateOfIssue: order.passportDateOfIssue ?? "", passportGivenBy: order.passportGivenBy ?? "", bornPlace: order.bornPlace ?? "", email: order.email ?? "", phone: order.phone ?? "", phoneAdditional: order.phoneAdditional ?? "", homePhone: order.homePhone ?? "", workCompany: order.workCompany ?? "", workHrPhone: order.workHrPhone ?? "", workRegion: order.workRegion ?? "Aşgabat", workProvince: order.workProvince ?? "", position: order.position ?? "", salary: order.salary != null ? String(order.salary) : "", workStartedAt: order.workStartedAt ?? "", note: order.note ?? "", loanAmount: order.loanAmount != null ? String(order.loanAmount) : "", loanHistory: order.loanHistory ?? "", cardNumber: order.cardNumber ?? "", cardName: order.cardName ?? "", cardExpMonth: order.cardExpMonth ?? "", cardExpYear: order.cardExpYear ?? "", guarantor1Name: order.guarantor1Name ?? "", guarantor1Surname: order.guarantor1Surname ?? "", guarantor1Patronic: order.guarantor1Patronic ?? "", guarantor1PassportSerie: order.guarantor1PassportSerie ?? "", guarantor1PassportNumber: order.guarantor1PassportNumber ?? "", guarantor1CardNumber: order.guarantor1CardNumber ?? "", guarantor1CardName: order.guarantor1CardName ?? "", guarantor1CardExpMonth: order.guarantor1CardExpMonth ?? "", guarantor1CardExpYear: order.guarantor1CardExpYear ?? "", guarantor1Salary: order.guarantor1Salary != null ? String(order.guarantor1Salary) : "", }; }