import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { FileSpreadsheet, FolderArchive, ArrowRight, RefreshCw, X, } from 'lucide-react'; import * as XLSX from 'xlsx'; import { Button } from '@/components/ui/button'; import { Progress } from '@/components/ui/progress'; import { cn } from '@/lib/utils'; import { api } from '@/services/api'; interface DownloadJob { fileName: string; progress: number; processedFiles: number; totalFiles: number; } export default function ExportSection() { const { t } = useTranslation(); const [activeDownload, setActiveDownload] = useState(null); const handleCancelDownload = () => { setActiveDownload(null); }; const handleExportExcel = async () => { const dateStr = new Date().toISOString().split('T')[0]; const fileName = `ICC_Transactions_${dateStr}.xlsx`; setActiveDownload({ fileName, progress: 10, processedFiles: 0, totalFiles: 0 }); try { // Use fast export endpoint — no pagination overhead const result = await api.get<{ transactions: any[]; total: number }>('/transactions/export'); const rows = result.transactions; setActiveDownload({ fileName, progress: 50, processedFiles: rows.length, totalFiles: rows.length }); const wsData = rows.map((tx: any) => ({ [t('table.date')]: tx.date ? new Date(tx.date).toLocaleDateString('fr-CA', { timeZone: 'America/Toronto' }) : '', [t('table.sender')]: tx.sender || '', [t('table.envelopeNumber')]: tx.envelopeNumber || '', [t('table.amount')]: tx.amount, 'Currency': tx.currency || 'CAD', [t('table.reference')]: tx.reference || '', [t('table.branch')]: tx.branch || 'Montreal', [t('table.status')]: tx.reviewed ? t('status.verified') : t('status.notVerified'), 'Email': tx.recipientEmail || '', 'Message': tx.message || '', })); setActiveDownload({ fileName, progress: 80, processedFiles: rows.length, totalFiles: rows.length }); const ws = XLSX.utils.json_to_sheet(wsData); ws['!cols'] = [ { wch: 12 }, // Date { wch: 25 }, // Sender { wch: 16 }, // Envelope Number { wch: 12 }, // Amount { wch: 6 }, // Currency { wch: 20 }, // Reference { wch: 20 }, // Branch { wch: 12 }, // Status { wch: 35 }, // Email { wch: 30 }, // Message ]; const wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, 'Transactions'); XLSX.writeFile(wb, fileName); setActiveDownload({ fileName, progress: 100, processedFiles: rows.length, totalFiles: rows.length }); setTimeout(() => setActiveDownload(null), 2000); } catch (err) { console.error('Export failed:', err); setActiveDownload(null); } }; const handleExportPdf = () => { // TODO: Trigger PDF batch download console.log('Export PDF batch'); }; return (

{t('reports.export.title')}

{/* Active Download Progress */} {activeDownload && (
{t('reports.export.downloading')}
{activeDownload.progress}%

{t('reports.export.generating')}{' '} {activeDownload.fileName} {' '} ({activeDownload.processedFiles}/{activeDownload.totalFiles}{' '} {t('reports.export.filesProcessed')})

)} {/* Export Cards */}
{/* Excel Export */}

{t('reports.export.excelTitle')}

{t('reports.export.excelDescription')}

{/* PDF Batch Export */}

{t('reports.export.pdfTitle')}

{t('reports.export.pdfDescription')}

); }