index.tsx
6.6 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
import React, { memo, useState, forwardRef, useImperativeHandle, useEffect } from 'react'
import type { FC, ReactNode, Ref } from 'react'
import { Input, Modal, Upload } from 'antd'
import { MinusCircleOutlined, PlusCircleOutlined, PlusOutlined } from '@ant-design/icons'
import type { RcFile, UploadProps } from 'antd/es/upload'
import type { UploadFile } from 'antd/es/upload/interface'
import { SendPrescriptionWrapper } from '../style'
import { BASE_URL } from '@/service/config'
import type { Send_prescription_formType } from '@/types'
import GHandler from '@/utils/methods'
// /sys/common/appUpload
const { TextArea } = Input
interface IProps {
id?: string | number
children?: ReactNode
ref?: any
}
const getBase64 = (file: RcFile): Promise<string> =>
new Promise((resolve, reject) => {
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => resolve(reader.result as string)
reader.onerror = (error) => reject(error)
})
const SendPrescription: FC<IProps> = memo(
forwardRef<HTMLDivElement, IProps>((props, ref: Ref<any>) => {
const [previewOpen, setPreviewOpen] = useState(false)
const [previewImage, setPreviewImage] = useState('')
const [previewTitle, setPreviewTitle] = useState('')
const [fileList, setFileList] = useState<UploadFile[]>([])
const [Send_prescription_formData, setSend_prescription_formTypeData] = useState<Send_prescription_formType>({
drugList: [{ amount: '', des: '', name: '', num: '' }],
prescriptionForm: '',
prescriptionAmount: 0
})
const addFormItemHandler = () => {
const setdata = { ...Send_prescription_formData, drugList: [...Send_prescription_formData.drugList, { amount: '', des: '', name: '', num: '' }] }
setSend_prescription_formTypeData(setdata)
}
const removeFormItemHandler = (index: number) => {
if (Send_prescription_formData.drugList.length <= 1) return
const setdata = { ...Send_prescription_formData, drugList: [...Send_prescription_formData.drugList].filter((_, idx) => idx !== index) }
setSend_prescription_formTypeData(setdata)
}
const changeFormItemValueHandler = (index: number, value: string | number, key: string) => {
const setdata = {
...Send_prescription_formData,
drugList: Send_prescription_formData.drugList.map((_, idx) => (idx === index ? { ..._, [key]: value } : _))
}
setSend_prescription_formTypeData(setdata)
}
useEffect(() => {
let prescriptionAmount = 0
prescriptionAmount = Send_prescription_formData.drugList.reduce((accumulator, item) => {
const amount = item.amount ? parseFloat(item.amount) : 0
const num = item.num ? parseFloat(item.num) : 0
if (amount !== 0 && num !== 0) accumulator += amount * num
return accumulator
}, 0)
setSend_prescription_formTypeData({ ...Send_prescription_formData, prescriptionAmount })
}, [Send_prescription_formData.drugList])
const handleCancel = () => setPreviewOpen(false)
const handlePreview = async (file: UploadFile) => {
if (!file.url && !file.preview) {
file.preview = await getBase64(file.originFileObj as RcFile)
}
setPreviewImage(file.url || (file.preview as string))
setPreviewOpen(true)
setPreviewTitle(file.name || file.url!.substring(file.url!.lastIndexOf('/') + 1))
}
const handleChange: UploadProps['onChange'] = ({ fileList: newFileList }) => setFileList(newFileList)
useImperativeHandle(ref, () => ({
Send_prescription_formData,
fileList,
setSend_prescription_formTypeData,
setFileList
}))
const uploadButton = (
<div>
<PlusOutlined />
<div style={{ marginTop: 8 }}>上传证明</div>
</div>
)
return (
<SendPrescriptionWrapper ref={ref}>
<div className="title">问诊病情</div>
<TextArea
value={Send_prescription_formData.prescriptionForm}
onChange={(e) => setSend_prescription_formTypeData({ ...Send_prescription_formData, prescriptionForm: e.target.value })}
placeholder="请输入问诊病情"
autoSize={{ minRows: 3, maxRows: 6 }}
className="title"
/>
<div className="title">处方药</div>
{Send_prescription_formData.drugList.map((_, index) => (
<div className="flexX" key={index}>
<div className="lefticon" onClick={() => removeFormItemHandler(index)}>
<MinusCircleOutlined className="grayIcon" />
</div>
<div>
<Input
value={Send_prescription_formData.drugList[index].name}
onChange={(e) => changeFormItemValueHandler(index, e.target.value, 'name')}
className="title"
placeholder="请输入药品名称"
/>
<div className="flexA">
<Input
value={Send_prescription_formData.drugList[index].amount}
onChange={(e) => changeFormItemValueHandler(index, e.target.value, 'amount')}
className="title"
placeholder="请输入药品价格"
/>
<Input
value={Send_prescription_formData.drugList[index].num}
onChange={(e) => changeFormItemValueHandler(index, e.target.value, 'num')}
className="title"
placeholder="请输入药品数量"
/>
</div>
<TextArea
value={Send_prescription_formData.drugList[index].des}
onChange={(e) => changeFormItemValueHandler(index, e.target.value, 'des')}
className="title"
placeholder="请输入说明"
autoSize={{ minRows: 3, maxRows: 6 }}
/>
</div>
</div>
))}
<div className="add flexC" onClick={addFormItemHandler}>
<PlusCircleOutlined className="blueIcon" />
<span>添加处方药</span>
</div>
<div className="title">处方单证明:</div>
<Upload action={BASE_URL + '/sys/common/appUpload'} listType="picture-card" fileList={fileList} onPreview={handlePreview} onChange={handleChange}>
{fileList.length >= 3 ? null : uploadButton}
</Upload>
<Modal open={previewOpen} title={previewTitle} footer={null} onCancel={handleCancel}>
<img alt="example" style={{ width: '100%' }} src={GHandler.downFile(previewImage)} />
</Modal>
<div className="title">总金额:¥{Send_prescription_formData.prescriptionAmount}</div>
</SendPrescriptionWrapper>
)
})
)
export default SendPrescription