Blame view

src/pages/Instalment/components/payWayDetail/payWayDetail.tsx 7.28 KB
凌世锦 authored
1
import { postOrderErpOrderStagesPayWaySelect } from '@/services';
凌世锦 authored
2
3
import type { ProColumns } from '@ant-design/pro-components';
import {
凌世锦 authored
4
5
6
  EditableProTable,
  ProFormDatePicker,
  ProFormRadio,
凌世锦 authored
7
} from '@ant-design/pro-components';
凌世锦 authored
8
import React, { useEffect, useState } from 'react';
凌世锦 authored
9
import PayWayUpload from '../upload/payWayUpload';
凌世锦 authored
10
import './payWayDetail.less';
凌世锦 authored
11
凌世锦 authored
12
13
14
15
16
17
18
// const waitTime = (time: number = 100) => {
//   return new Promise((resolve) => {
//     setTimeout(() => {
//       resolve(true);
//     }, time);
//   });
// };
凌世锦 authored
19
20

type DataSourceType = {
凌世锦 authored
21
22
23
24
25
26
27
28
  id: number;
  payStep?: string;
  proportion?: string;
  ossId?: number;
  payPrice?: number;
  payDate?: Date;
  fileName?: string;
  fileUrl?: string;
凌世锦 authored
29
30
31
};

export default ({ payBody, thisId, currtSave }) => {
凌世锦 authored
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
  const defaultData: DataSourceType[] = [
    {
      id: 1,
      payStep: '预付款',
      proportion: undefined,
      payPrice: undefined,
      ossId: undefined,
      payDate: undefined,
      fileName: undefined,
      fileUrl: undefined,
    },
    {
      id: 2,
      payStep: '发贷款',
      proportion: undefined,
      payPrice: undefined,
      ossId: undefined,
      payDate: undefined,
      fileName: undefined,
      fileUrl: undefined,
    },
    {
      id: 3,
      payStep: '验收款',
      proportion: undefined,
      payPrice: undefined,
      ossId: undefined,
      payDate: undefined,
      fileName: undefined,
      fileUrl: undefined,
    },
    {
      id: 4,
      payStep: '质保金',
      proportion: undefined,
      payPrice: undefined,
      ossId: undefined,
      payDate: undefined,
      fileName: undefined,
      fileUrl: undefined,
    },
  ];
凌世锦 authored
74
凌世锦 authored
75
  const [editableKeys, setEditableRowKeys] = useState<React.Key[]>([]);
凌世锦 authored
76
  const [isRetrun, setIsRetrun] = useState(false);
凌世锦 authored
77
78
79
80
81
82
83
  const [position, setPosition] = useState<'top' | 'bottom' | 'hidden'>(
    'hidden',
  );
  const [payWayDetailBody, setPayWayDetailBody] = useState<
    readonly DataSourceType[]
  >([...defaultData]);
  // const [body, setBody] = useState([])
凌世锦 authored
84
  // const [isAccept, setIsAccept] = useState(null);
凌世锦 authored
85
  // const [isCurrtSave, setIsCurrtSave] = useState(false);
凌世锦 authored
86
凌世锦 authored
87
88
89
90
91
92
93
94
  const waitTime = (time: number = 100) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(true);
      }, time);
    });
  };
凌世锦 authored
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  async function getOther(value, arr) {
    const res = await postOrderErpOrderStagesPayWaySelect({
      data: { ossId: value },
    });
    if (res.data) {
      const context = res.data;
      const remake = arr.map((obj) => {
        let currt = obj;
        context.forEach((object) => {
          if (object.number === obj.id) {
            currt = {
              ...obj,
              ossId: value,
              payDate: object.dateRange,
              fileName: object.fileName,
              fileUrl: object.fileUrl,
            };
            return currt;
          }
凌世锦 authored
114
        });
凌世锦 authored
115
116
117
        return currt;
      });
      setPayWayDetailBody(remake);
凌世锦 authored
118
      setIsRetrun(true);
凌世锦 authored
119
    }
凌世锦 authored
120
  }
凌世锦 authored
121
凌世锦 authored
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  function getUploadFile(value) {
    let remakeBody = [];
    let remakeBodyItem = {};
    payWayDetailBody.forEach((item) => {
      if (item.id === value.id) {
        remakeBodyItem = {
          ...item,
          fileUrl: value.url,
          fileName: value.name,
        };
      } else {
        remakeBodyItem = { ...item };
      }
      remakeBody.push(remakeBodyItem);
    });
    setPayWayDetailBody(remakeBody);
    currtSave(remakeBody);
  }

  async function setPayWay(value) {
凌世锦 authored
142
143
144
145
146
147
148
    const remakeData = payWayDetailBody.map((obj) => {
      return {
        ...obj,
        proportion: value[obj.id - 1]?.proportion,
        payPrice: value[obj.id - 1]?.payPrice,
      };
    });
凌世锦 authored
149
凌世锦 authored
150
    setPayWayDetailBody(remakeData);
凌世锦 authored
151
凌世锦 authored
152
153
    if (thisId !== null) {
      getOther(thisId, remakeData);
凌世锦 authored
154
    }
凌世锦 authored
155
  }
凌世锦 authored
156
凌世锦 authored
157
158
159
  useEffect(() => {
    setPayWay(payBody);
  }, [payBody]);
凌世锦 authored
160
凌世锦 authored
161
162
163
  // function setCurrtSave(value) {
  //     setIsCurrtSave(payWayDetailBody)
  // }
凌世锦 authored
164
凌世锦 authored
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
  const columns: ProColumns<DataSourceType>[] = [
    {
      title: '编号',
      dataIndex: 'id',
      hideInTable: true,
      editable: false,
    },
    {
      title: '付款信息',
      dataIndex: 'payStep',
      editable: false,
    },
    {
      title: '付款比例',
      dataIndex: 'proportion',
      editable: false,
    },
    {
      title: '付款金额',
      dataIndex: 'payPrice',
      editable: false,
    },
    {
      title: '对应的订单',
      dataIndex: 'ossId',
      editable: false,
      hideInTable: true,
    },
    {
      title: '付款时间',
      dataIndex: 'payDate',
      editable: false,
      render: (text, record) => {
        const handleChange = (value) => {
          const updatedDataSource = payWayDetailBody.map((item) => {
            if (item.id === record.id) {
              return {
                ...item,
                payDate: value,
              };
凌世锦 authored
205
            }
凌世锦 authored
206
207
            return item;
          });
凌世锦 authored
208
凌世锦 authored
209
210
211
          setPayWayDetailBody(updatedDataSource);
          currtSave(updatedDataSource);
        };
凌世锦 authored
212
凌世锦 authored
213
214
        return (
          <ProFormDatePicker
凌世锦 authored
215
            className="payWaydataChoose"
凌世锦 authored
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
            initialValue={record.payDate}
            value={record.payDate}
            placeholder={'请填写时间'}
            fieldProps={{
              format: (value) => value.format('YYYY-MM-DD'),
              onChange: handleChange,
            }}
          />
        );
      },
    },
    {
      title: '付款单回执',
      dataIndex: 'fileName',
      render: (text, record) => {
凌世锦 authored
231
232
233
234
235
236
237
238
239
240
241
242
        if (record.fileUrl !== undefined) {
          return (
            <PayWayUpload
              natureModel={{
                id: record.id,
                fileName: record.fileName,
                fileUrl: record.fileUrl,
              }}
              setCurryFile={getUploadFile}
              key={isRetrun}
            ></PayWayUpload>
          );
凌世锦 authored
243
244
        } else {
          return (
凌世锦 authored
245
246
247
248
249
            <PayWayUpload
              natureModel={{
                id: record.id,
                fileName: undefined,
                fileUrl: undefined,
凌世锦 authored
250
              }}
凌世锦 authored
251
252
253
              setCurryFile={getUploadFile}
              key={isRetrun}
            ></PayWayUpload>
凌世锦 authored
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
          );
        }
      },
    },
  ];

  return (
    <>
      <EditableProTable<DataSourceType>
        rowKey="id"
        className="payway-detail-index"
        toolbar={{ style: { display: 'none' } }}
        ghost={true}
        scroll={{
          x: 960,
        }}
        recordCreatorProps={
          position !== 'hidden'
            ? {
                position: position as 'top',
                record: () => ({ id: (Math.random() * 1000000).toFixed(0) }),
              }
            : false
        }
        loading={false}
        toolBarRender={() => [
          <ProFormRadio.Group
            key="render"
            fieldProps={{
              value: position,
              onChange: (e) => setPosition(e.target.value),
            }}
          />,
        ]}
        columns={columns}
        request={payWayDetailBody}
        value={payWayDetailBody}
        onChange={(value) => {
          setPayWayDetailBody(value);
          // setCurrtSave(payWayDetailBody)
        }}
        editable={{
          type: 'multiple',
          editableKeys,
          onSave: async () => {
            await waitTime(2000);
          },
          onChange: setEditableRowKeys,
        }}
      />
    </>
  );
};