File size: 8,175 Bytes
149698e
 
 
 
 
 
 
 
 
e85d815
149698e
 
 
e85d815
149698e
 
 
 
 
 
 
 
 
 
e85d815
149698e
 
 
 
 
e85d815
 
 
 
 
 
 
61094eb
 
e85d815
 
 
 
 
 
 
7b00df2
e85d815
 
 
fe203ef
e85d815
 
 
 
 
 
 
 
61094eb
 
 
7b00df2
61094eb
 
 
 
 
 
 
 
e85d815
 
 
 
 
 
 
 
 
 
 
149698e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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<DownloadJob | null>(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 (
    <div className="mt-4 pb-12">
      <h2 className="text-xl font-bold text-foreground mb-6 flex items-center gap-2">
        <span className="w-1.5 h-6 bg-primary rounded-full" />
        {t('reports.export.title')}
      </h2>

      <div className="bg-card rounded-xl border border-border p-6 md:p-8 shadow-[0_1px_3px_0_rgba(0,0,0,0.1),0_1px_2px_0_rgba(0,0,0,0.06)]">
        {/* Active Download Progress */}
        {activeDownload && (
          <div className="flex flex-col md:flex-row gap-8 items-start md:items-center justify-between bg-slate-50 p-6 rounded-xl border border-slate-100 mb-8">
            <div className="flex-1 w-full">
              <div className="flex items-center justify-between mb-3">
                <div className="flex items-center gap-2">
                  <div className="bg-primary/10 p-1.5 rounded-full">
                    <RefreshCw className="h-3.5 w-3.5 text-primary animate-spin" />
                  </div>
                  <span className="text-sm font-bold text-foreground">
                    {t('reports.export.downloading')}
                  </span>
                </div>
                <span className="text-sm font-bold text-primary">
                  {activeDownload.progress}%
                </span>
              </div>
              <Progress value={activeDownload.progress} className="h-2 mb-2" />
              <p className="text-xs text-muted-foreground font-medium">
                {t('reports.export.generating')}{' '}
                <span className="text-foreground font-bold">
                  {activeDownload.fileName}
                </span>{' '}
                ({activeDownload.processedFiles}/{activeDownload.totalFiles}{' '}
                {t('reports.export.filesProcessed')})
              </p>
            </div>
            <div className="flex gap-2 w-full md:w-auto">
              <Button
                variant="ghost"
                size="sm"
                className="w-full md:w-auto text-red-600 hover:bg-red-50 hover:text-red-700 font-semibold"
                onClick={handleCancelDownload}
              >
                <X className="h-3.5 w-3.5 mr-1" />
                {t('common.cancel')}
              </Button>
            </div>
          </div>
        )}

        {/* Export Cards */}
        <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
          {/* Excel Export */}
          <div
            className="group relative flex flex-col p-6 rounded-xl border border-slate-200 hover:border-green-400 hover:shadow-md transition-all cursor-pointer bg-white"
            onClick={handleExportExcel}
          >
            <div className="flex items-start justify-between mb-4">
              <div className="p-3 bg-green-50 rounded-lg text-green-600 ring-1 ring-green-100 group-hover:bg-green-100 transition-colors">
                <FileSpreadsheet className="h-7 w-7" />
              </div>
              <div className="p-2 rounded-full hover:bg-slate-50 transition-colors">
                <ArrowRight className="h-5 w-5 text-slate-300 group-hover:text-green-500 transition-colors" />
              </div>
            </div>
            <h3 className="text-lg font-bold text-foreground mb-2 group-hover:text-green-700 transition-colors">
              {t('reports.export.excelTitle')}
            </h3>
            <p className="text-sm text-muted-foreground mb-8 leading-relaxed">
              {t('reports.export.excelDescription')}
            </p>
            <Button
              variant="outline"
              className={cn(
                'mt-auto w-full py-3 border-2 border-slate-100 font-bold',
                'group-hover:border-green-500 group-hover:text-green-600 transition-all'
              )}
            >
              {t('reports.export.generateReport')}
            </Button>
          </div>

          {/* PDF Batch Export */}
          <div
            className="group relative flex flex-col p-6 rounded-xl border border-slate-200 hover:border-primary/50 hover:shadow-md transition-all cursor-pointer bg-white"
            onClick={handleExportPdf}
          >
            <div className="flex items-start justify-between mb-4">
              <div className="p-3 bg-blue-50 rounded-lg text-primary ring-1 ring-blue-100 group-hover:bg-blue-100 transition-colors">
                <FolderArchive className="h-7 w-7" />
              </div>
              <div className="p-2 rounded-full hover:bg-slate-50 transition-colors">
                <ArrowRight className="h-5 w-5 text-slate-300 group-hover:text-primary transition-colors" />
              </div>
            </div>
            <h3 className="text-lg font-bold text-foreground mb-2 group-hover:text-primary transition-colors">
              {t('reports.export.pdfTitle')}
            </h3>
            <p className="text-sm text-muted-foreground mb-8 leading-relaxed">
              {t('reports.export.pdfDescription')}
            </p>
            <Button className="mt-auto w-full py-3 font-bold shadow-lg shadow-blue-500/20">
              {t('reports.export.startDownload')}
            </Button>
          </div>
        </div>
      </div>
    </div>
  );
}