import type React from "react"; import { Link } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { AlertTriangle, Database, Eye, ExternalLink } from "lucide-react"; import { Skeleton } from "@/components/ui/skeleton"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from "@/components/ui/dialog"; import { useGetOnlinePaymentDetail, useGetOnlinePaymentById } from "@/features/onlinePaymentHistory/hooks/useOnlinePaymentsHistory"; import { GatewayStatusBadge, PaidBadge } from "@/features/onlinePaymentHistory/components/PaymentStatusBadge"; import { formatFromApiDate } from "@/lib/utils/dateUtils"; import { rawEnumKey } from "@/constants/enumLabels"; function resolveLang(lang: string): "en" | "ru" | "tm" { if (lang === "ru") return "ru"; if (lang === "tk") return "tm"; return "en"; } interface PaymentDetailDialogProps { paymentId: number | null; open: boolean; onOpenChange: (open: boolean) => void; } export function PaymentDetailDialog({ paymentId, open, onOpenChange }: PaymentDetailDialogProps) { const { t } = useTranslation(); const { data, isLoading, isError } = useGetOnlinePaymentDetail(paymentId ?? 0, { lang: "tm" }); return (
{t("onlinePayments.paymentViewTitle", "Halk bankdan gelen netije")}: #{paymentId}
{t("onlinePayments.gatewayResponse.title", "Gateway response")}
{isLoading ? (
) : isError || !data ? (
{t("common.error", "Maglumat ýüklenende ýalňyşlyk ýüze çykdy")}
) : (
{data.gatewayResponse && (data.gatewayResponse.errorCode != null || data.gatewayResponse.errorMessage) && (
{t("onlinePayments.gatewayErrorTitle", "Gateway error")} {data.gatewayResponse.errorCode != null && ( {" "} ({t("onlinePayments.gateway.errorCode", "Error code")}: {data.gatewayResponse.errorCode}) )}
{data.gatewayResponse.errorMessage &&
{data.gatewayResponse.errorMessage}
}
)}
              {JSON.stringify(data.gatewayResponse ?? {}, null, 2)}
            
)}
); } // ─── Shared Row helper ──────────────────────────────────────────────────────── function Row({ label, children }: { label: string; children: React.ReactNode }) { return (
{label} {children}
); } // ─── LastStepDialog ─────────────────────────────────────────────────────────── export interface LastStepDialogProps { /** The ID used to fetch the step history from /payment-history/:id. */ orderId: number | null; open: boolean; onOpenChange: (open: boolean) => void; } /** * Opens a dialog showing only the most recent payment history step / operation * for the given order. Used by the Eye icon in PaymentHistoryTable. * Fetches its own data independently via useGetOnlinePaymentById (/payment-history/:id). */ export function LastStepDialog({ orderId, open, onOpenChange }: LastStepDialogProps) { const { t, i18n } = useTranslation(); const lang = resolveLang(i18n.language); const { data: rawSteps, isLoading, isError } = useGetOnlinePaymentById(orderId ?? 0); const steps: any[] = Array.isArray(rawSteps) ? rawSteps : (rawSteps as any)?.data && Array.isArray((rawSteps as any).data) ? (rawSteps as any).data : (rawSteps as any)?.items && Array.isArray((rawSteps as any).items) ? (rawSteps as any).items : rawSteps && typeof rawSteps === "object" && (rawSteps as any).id ? [rawSteps] : []; const lastStep = steps.length ? [...steps].sort((a, b) => new Date(b.createdAt || 0).getTime() - new Date(a.createdAt || 0).getTime())[0] : null; const branchDisplay = typeof lastStep?.branchName === "string" ? lastStep.branchName : lastStep?.branchName?.[lang] || lastStep?.branchName?.tm || lastStep?.branchName?.en || null; const fullName = [lastStep?.passportName, lastStep?.passportSurname].filter(Boolean).join(" "); const detailEntries = Object.entries(lastStep?.detail ?? {}); return (
{t("onlinePayments.lastStep.title", "Iň soňky işlem")} {lastStep && ( <> {(lastStep.gatewayStatus || lastStep.step) && } {typeof lastStep.paid === "boolean" && } )}
{(lastStep?.orderNumber || lastStep?.orderId || orderId) && ( {t("onlinePayments.columns.orderNumber", "Sargyt belgisi")}:{" "} {lastStep?.orderNumber || `Order #${lastStep?.orderId || orderId}`} )}
{isLoading ? (
) : isError || !lastStep ? (
{t("onlinePayments.noHistory", "Töleg taryhy tapylmady")}
) : (
{/* Section 1: General */}

{t("onlinePayments.sectionGeneral", "Esasy maglumatlar")}

{lastStep.id != null && ( {lastStep.id} )} {lastStep.orderId != null && ( #{lastStep.orderId} )} {lastStep.orderNumber && ( {lastStep.orderNumber} )} {lastStep.type && ( {lastStep.type} )} {lastStep.description && ( {lastStep.description} )} {lastStep.gatewayReference && ( {lastStep.gatewayReference} )} {(lastStep.apiClientName || lastStep.apiClient) && ( {lastStep.apiClientName || lastStep.apiClient} )} {lastStep.callbackStatus && ( {lastStep.callbackStatus} )} {lastStep.createdAt && ( {formatFromApiDate(lastStep.createdAt)} )} {lastStep.callbackAt && ( {formatFromApiDate(lastStep.callbackAt)} )} {lastStep.updatedAt && ( {formatFromApiDate(lastStep.updatedAt)} )}
{/* Section 2: Financial */} {(lastStep.amountTmt != null || lastStep.totalAmountTmt != null || lastStep.amountUsd != null) && (

{t("onlinePayments.sectionFinancial", "Maliýe maglumatlary")}

{lastStep.totalAmountTmt != null && ( {lastStep.totalAmountTmt?.toLocaleString?.() ?? lastStep.totalAmountTmt} TMT )} {lastStep.amountTmt != null && ( {lastStep.amountTmt?.toLocaleString?.() ?? lastStep.amountTmt} TMT )} {lastStep.bankFeeTmt != null && ( {lastStep.bankFeeTmt?.toLocaleString?.() ?? lastStep.bankFeeTmt} TMT )} {lastStep.gbusFeeTmt != null && ( {lastStep.gbusFeeTmt?.toLocaleString?.() ?? lastStep.gbusFeeTmt} TMT )} {lastStep.amountUsd != null && ( {lastStep.amountUsd} $ )} {lastStep.exchangeRateUsd != null && ( {lastStep.exchangeRateUsd} TMT )} {lastStep.paymentPeriod && ( {formatFromApiDate(lastStep.paymentPeriod)} )}
)} {/* Section 3: Customer & Branch */} {(fullName || lastStep.branchName || lastStep.createdByName || lastStep.userId) && (

{t("onlinePayments.sectionCustomer", "Müşderi we Şahamça")}

{fullName && ( {fullName} )} {lastStep.userId != null && ( #{lastStep.userId} )} {branchDisplay && ( {branchDisplay} )} {lastStep.createdByName && ( {lastStep.createdByName} )}
)} {/* Section 4: External URLs */} {(lastStep.formUrl || lastStep.successUrl) && (

{t("onlinePayments.sectionUrls", "Baglanyşyklar")}

{lastStep.formUrl && ( {lastStep.formUrl} )} {lastStep.successUrl && ( {lastStep.successUrl} )}
)} {/* Legacy detail entries if present */} {detailEntries.length > 0 && (

{t("onlinePayments.detail.details", "Jikme-jikler")}

{detailEntries.map(([key, value]) => ( {String(value)} ))}
)}
)}
); }