addDrug.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import React, {
  2. useContext
  3. } from 'react';
  4. import { Form, Input, Button, message, Space } from 'antd';
  5. import apiObj from '@api/index';
  6. import { getCookie,setCookie } from '@utils/index';
  7. import DrugContext from './Drug-context';
  8. const { post, api } = apiObj;
  9. function AddDrug(props) {
  10. const [form] = Form.useForm();
  11. const { type, formData} = useContext(DrugContext);
  12. const initialValues = formData;
  13. const onFinish = values => {
  14. let params = values
  15. if (type == 3) {
  16. params.id=initialValues.id
  17. upDrugById(params)
  18. } else {
  19. addDrug(params)
  20. }
  21. };
  22. function addDrug(param) {
  23. const hisId = getCookie("hospitalId");
  24. const params ={
  25. hospitalId:hisId,
  26. ...param
  27. }
  28. post(api.addDrug, params).then((res) => {
  29. if (res.data.code === 200) {
  30. props.DrugChange()
  31. message.success(res.data.message);
  32. } else {
  33. message.error(res.data.message);
  34. }
  35. })
  36. }
  37. function upDrugById(param) {
  38. const hisId = getCookie("hospitalId");
  39. const params ={
  40. hospitalId:hisId,
  41. ...param
  42. }
  43. post(api.upDrugById, params).then((res) => {
  44. if (res.data.code === 200) {
  45. props.DrugChange()
  46. message.success(res.data.message);
  47. } else {
  48. message.error(res.data.message);
  49. }
  50. })
  51. }
  52. function cancel() {
  53. props.cancel()
  54. }
  55. function onValuesChange() {
  56. props.isChange(form.isFieldsTouched())
  57. }
  58. return (
  59. <>
  60. <Form
  61. labelCol={{ span: 6 }}
  62. wrapperCol={{ span: 16 }}
  63. form={form}
  64. name="register"
  65. onFinish={onFinish}
  66. initialValues={initialValues}
  67. onValuesChange={onValuesChange}
  68. >
  69. <Form.Item
  70. name="name"
  71. label="医院药品名称"
  72. rules={[
  73. {
  74. required: true,
  75. message: '请输入医院药品名称',
  76. },
  77. ]}
  78. >
  79. {type == 3 ?
  80. <Input autoComplete='off'/>
  81. :
  82. <Input placeholder="请输入" autoComplete='off'/>
  83. }
  84. </Form.Item>
  85. <Form.Item
  86. name="approvalNum"
  87. label="国药准字"
  88. >
  89. {type == 3 ?
  90. <Input autoComplete='off'/>
  91. :
  92. <Input placeholder="请输入" autoComplete='off'/>
  93. }
  94. </Form.Item>
  95. <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
  96. <Space size="middle">
  97. <Button htmlType="button" onClick={e => cancel()}>
  98. 取消
  99. </Button>
  100. <Button type="primary" htmlType="submit">
  101. 保存
  102. </Button>
  103. </Space>
  104. </Form.Item>
  105. </Form>
  106. </>
  107. );
  108. }
  109. export default AddDrug;