PointsExchangeRecordsModal.tsx
8.83 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import { postIntegralGroupExchangeRecords } from '@/services/request';
import { ModalForm, ProTable } from '@ant-design/pro-components';
import { Button, DatePicker, Space, Tabs, message } from 'antd';
import dayjs from 'dayjs';
import React, { useState } from 'react';
import '../index.less';
interface PointsExchangeRecordsModalProps {
setVisible: (visible: boolean) => void;
record: any;
}
// Define a type for the records data
type RecordsDataType = {
exchangeRecords: any[];
collectedRecords: any[];
pendingRecords: any[];
};
const PointsExchangeRecordsModal: React.FC<PointsExchangeRecordsModalProps> = ({
setVisible,
record,
}) => {
const [activeTab, setActiveTab] = useState('1');
// Separate date ranges for each tab
const [exchangeDateRange, setExchangeDateRange] = useState<any[]>([]);
const [collectedDateRange, setCollectedDateRange] = useState<any[]>([]);
const [pendingDateRange, setPendingDateRange] = useState<any[]>([]);
const [loading, setLoading] = useState(false);
const [recordsData, setRecordsData] = useState<RecordsDataType>({
exchangeRecords: [], // 兑换记录
collectedRecords: [], // 已领取积分
pendingRecords: [], // 待领取积分
});
// Function to fetch records data from API with date parameters
const fetchRecordsData = async (params: {
startTime?: string | null;
endTime?: string | null;
tabKey: string;
}) => {
try {
setLoading(true);
const { startTime, endTime, tabKey } = params;
// Prepare API request data
const requestDto: any = {
id: Number(record.id),
};
// Add date range if provided
if (startTime) requestDto.startTime = startTime;
if (endTime) requestDto.endTime = endTime;
console.log(`Fetching data for tab: ${tabKey}`);
// Use postIntegralGroupExchangeRecords instead of postIntegralUserExchangeRecords
const response = await postIntegralGroupExchangeRecords({
data: requestDto,
});
if (response && response.data) {
// Update only the data for the active tab
setRecordsData((prevData: RecordsDataType) => {
const newData = { ...prevData };
switch (tabKey) {
case '1': // 兑换记录
newData.exchangeRecords = response.data.exchangeRecords || [];
break;
case '2': // 已领取积分
newData.collectedRecords = response.data.collectedRecords || [];
break;
case '3': // 待领取积分
newData.pendingRecords = response.data.pendingRecords || [];
break;
}
return newData;
});
} else {
message.error('获取积分记录失败');
}
} catch (error) {
console.error(error);
message.error('获取积分记录失败');
} finally {
setLoading(false);
}
};
// Initial data fetch for all tabs
React.useEffect(() => {
const loadAllTabs = async () => {
try {
setLoading(true);
const requestDto = {
id: Number(record.id),
};
const response = await postIntegralGroupExchangeRecords({
data: requestDto,
});
if (response && response.data) {
setRecordsData({
exchangeRecords: response.data.exchangeRecords || [],
collectedRecords: response.data.collectedRecords || [],
pendingRecords: response.data.pendingRecords || [],
});
} else {
message.error('获取积分记录失败');
}
} catch (error) {
console.error(error);
message.error('获取积分记录失败');
} finally {
setLoading(false);
}
};
loadAllTabs();
}, [record.id]);
const handleTabChange = (key: string) => {
setActiveTab(key);
};
// Get current date range based on active tab
const getCurrentDateRange = () => {
switch (activeTab) {
case '1':
return exchangeDateRange;
case '2':
return collectedDateRange;
case '3':
return pendingDateRange;
default:
return [];
}
};
// Set date range for current tab
const handleDateRangeChange = (dates: any) => {
switch (activeTab) {
case '1':
setExchangeDateRange(dates);
break;
case '2':
setCollectedDateRange(dates);
break;
case '3':
setPendingDateRange(dates);
break;
}
};
const handleSearchClick = () => {
const dateRange = getCurrentDateRange();
// Only get the dates that are actually selected
const startTime = dateRange?.[0]
? dayjs(dateRange[0]).format('YYYY-MM-DD')
: null;
const endTime = dateRange?.[1]
? dayjs(dateRange[1]).format('YYYY-MM-DD')
: null;
fetchRecordsData({
startTime,
endTime,
tabKey: activeTab,
});
};
const handleClearClick = () => {
// Clear date range for current tab
switch (activeTab) {
case '1':
setExchangeDateRange([]);
break;
case '2':
setCollectedDateRange([]);
break;
case '3':
setPendingDateRange([]);
break;
}
// Fetch data without date filters
fetchRecordsData({
startTime: null,
endTime: null,
tabKey: activeTab,
});
};
// 兑换记录表格列定义
const exchangeColumns = [
{
title: '兑换日期',
dataIndex: 'createTime',
key: 'createTime',
width: 30,
},
{
title: '扣除积分',
dataIndex: 'delta',
key: 'delta',
width: 20,
},
{
title: '操作人',
dataIndex: 'createByName',
key: 'createByName',
width: 20,
},
{
title: '积分用途',
dataIndex: 'remark',
key: 'remark',
width: 100,
},
];
// 已领取积分表格列定义
const collectedColumns = [
{
title: '订单号',
dataIndex: 'sourceId',
key: 'sourceId',
},
{
title: '领取积分',
dataIndex: 'delta',
key: 'delta',
},
{
title: '领取日期',
dataIndex: 'createTime',
key: 'createTime',
},
];
// 待领取积分表格列定义
const pendingColumns = [
{
title: '订单号',
dataIndex: 'sourceId',
key: 'sourceId',
},
{
title: '领取积分',
dataIndex: 'delta',
key: 'delta',
},
{
title: '过期日期',
dataIndex: 'createTime',
key: 'createTime',
},
];
// Render date picker and search buttons for current tab
const renderDateRangePicker = () => {
return (
<Space style={{ marginBottom: 16 }}>
<DatePicker.RangePicker
value={getCurrentDateRange()}
onChange={handleDateRangeChange}
allowEmpty={[true, true]}
/>
<Button type="primary" onClick={handleSearchClick}>
搜索
</Button>
<Button onClick={handleClearClick}>重置</Button>
</Space>
);
};
return (
<div className="prepaid-index">
<ModalForm
width={1000}
open
title="积分兑换记录"
submitter={false}
modalProps={{
destroyOnClose: true,
onCancel: () => {
setVisible(false);
},
}}
>
<Tabs activeKey={activeTab} onChange={handleTabChange}>
<Tabs.TabPane tab="兑换记录" key="1">
{renderDateRangePicker()}
<ProTable
headerTitle={false}
search={false}
options={false}
pagination={{
pageSize: 10,
}}
loading={loading && activeTab === '1'}
dataSource={recordsData.exchangeRecords}
columns={exchangeColumns}
rowKey="id"
/>
</Tabs.TabPane>
<Tabs.TabPane tab="已领取积分" key="2">
{renderDateRangePicker()}
<ProTable
headerTitle={false}
search={false}
options={false}
pagination={{
pageSize: 10,
}}
loading={loading && activeTab === '2'}
dataSource={recordsData.collectedRecords}
columns={collectedColumns}
rowKey="id"
/>
</Tabs.TabPane>
<Tabs.TabPane tab="待领取积分" key="3">
{renderDateRangePicker()}
<ProTable
headerTitle={false}
search={false}
options={false}
pagination={{
pageSize: 10,
}}
loading={loading && activeTab === '3'}
dataSource={recordsData.pendingRecords}
columns={pendingColumns}
rowKey="id"
/>
</Tabs.TabPane>
</Tabs>
</ModalForm>
</div>
);
};
export default PointsExchangeRecordsModal;