Blame view

src/pages/Instalment/components/read/readPayWay.tsx 6.97 KB
PurelzMgnead authored
1
import { postOrderErpOrderStagesPayWaySelect } from '@/services';
PurelzMgnead authored
2
3
import type { ProColumns } from '@ant-design/pro-components';
import {
PurelzMgnead authored
4
5
6
  EditableProTable,
  ProFormRadio,
  ProFormUploadButton,
PurelzMgnead authored
7
8
} from '@ant-design/pro-components';
import { message } from 'antd';
PurelzMgnead authored
9
import React, { useEffect, useState } from 'react';
PurelzMgnead authored
10
11

const waitTime = (time: number = 100) => {
PurelzMgnead authored
12
13
14
15
16
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
PurelzMgnead authored
17
18
19
};

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

export default ({ payBody, thisId, currtSave }) => {
PurelzMgnead authored
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
  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,
    },
  ];
PurelzMgnead authored
73
PurelzMgnead authored
74
75
76
77
78
79
80
81
82
  const [editableKeys, setEditableRowKeys] = useState<React.Key[]>([]);
  const [position, setPosition] = useState<'top' | 'bottom' | 'hidden'>(
    'hidden',
  );
  const [payWayDetailBody, setPayWayDetailBody] = useState<
    readonly DataSourceType[]
  >([...defaultData]);
  const [isAccept] = useState(null);
  // const [isCurrtSave, setIsCurrtSave] = useState(false);
PurelzMgnead authored
83
PurelzMgnead authored
84
85
86
87
88
89
90
91
  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;
92
93
94
95
96
97
98
99
100
101
102
103
104
105
        if (Array.isArray(context)) {
          context.forEach((object) => {
            if (object.number === obj.id) {
              currt = {
                ...obj,
                ossId: value,
                payDate: object.dateRange,
                fileName: object.fileName,
                fileUrl: object.fileUrl,
              };
              return currt;
            }
          });
        }
PurelzMgnead authored
106
107
108
        return currt;
      });
      setPayWayDetailBody(remake);
PurelzMgnead authored
109
    }
PurelzMgnead authored
110
  }
PurelzMgnead authored
111
PurelzMgnead authored
112
113
114
115
116
117
118
119
  function setPayWay(value) {
    const remakeData = payWayDetailBody.map((obj) => {
      return {
        ...obj,
        proportion: value[obj.id - 1]?.proportion,
        payPrice: value[obj.id - 1]?.payPrice,
      };
    });
PurelzMgnead authored
120
PurelzMgnead authored
121
    setPayWayDetailBody(remakeData);
PurelzMgnead authored
122
PurelzMgnead authored
123
124
    if (thisId !== null) {
      getOther(thisId, remakeData);
PurelzMgnead authored
125
    }
PurelzMgnead authored
126
127
128
129
  }
  // useEffect(() => {
  //     getOther(thisId)
  // }, [thisId])
PurelzMgnead authored
130
PurelzMgnead authored
131
132
133
  useEffect(() => {
    setPayWay(payBody);
  }, [payBody]);
PurelzMgnead authored
134
PurelzMgnead authored
135
136
137
  // function setCurrtSave(_value) {
  //     setIsCurrtSave(payWayDetailBody)
  // }
PurelzMgnead authored
138
PurelzMgnead authored
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
  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) => {
PurelzMgnead authored
172
173
174
175
176
177
178
179
180
181
182
183
184
        if (record.payDate) {
          return (
            <span>
              {new Intl.DateTimeFormat('zh-CN', {
                year: 'numeric',
                month: 'long',
                day: 'numeric',
              }).format(record.payDate)}
            </span>
          );
        } else {
          return <span></span>;
        }
PurelzMgnead authored
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
      },
    },
    {
      title: '付款单回执',
      dataIndex: 'fileName',
      render: (_text, record) => {
        if (isAccept !== record.id) {
          if (
            typeof record.fileUrl === 'object' &&
            record.fileUrl instanceof File
          ) {
            return (
              <a onClick={() => message.error('请先保存')}>{record.fileName}</a>
            );
          } else {
            return <a href={record.fileUrl}>{record.fileName}</a>;
          }
        } else {
          return (
            <ProFormUploadButton
              name={record.id}
              onChange={(value) => {
                let remakeBody = [];
                let remakeBodyItem = {};
                payWayDetailBody.forEach((item) => {
                  if (item.id === record.id) {
                    remakeBodyItem = {
                      ...item,
                      fileUrl: value.file.originFileObj,
                      fileName: value.file.name,
                    };
                  } else {
                    remakeBodyItem = { ...item };
                  }
                  if (value.fileList.length === 0) {
                    remakeBodyItem = {
                      ...item,
                      fileUrl: undefined,
                      fileName: undefined,
                    };
                  }
                  remakeBody.push(remakeBodyItem);
                });
                setPayWayDetailBody(remakeBody);
                currtSave(remakeBody);
              }}
              width="md"
              max={1}
            />
          );
PurelzMgnead authored
235
        }
PurelzMgnead authored
236
237
238
      },
    },
  ];
PurelzMgnead authored
239
PurelzMgnead authored
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
  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,
        }}
      />
    </>
  );
};