123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import React, { useRef, useState } from 'react';
  2. import { PageContainer } from '@ant-design/pro-layout';
  3. import ProTable from '@ant-design/pro-table';
  4. import { Button, Modal, Input, notification } from 'antd';
  5. import { PlusOutlined } from '@ant-design/icons';
  6. import request, { queryTable } from '@/utils/request';
  7. import School from '@/components/School';
  8. export default () => {
  9. const tableRef = useRef();
  10. const [school, setSchool] = useState();
  11. const [current, setCurrent] = useState();
  12. const [inputVal, setInputVal] = useState([]);
  13. const [show, setShow] = useState(false);
  14. const queryFunc = (params, page, sort, filter) => {
  15. if (!school.schoolId) {
  16. return Promise.resolve({ data: [], success: true, total: 0 });
  17. }
  18. const newParams = { ...params, schoolId: school.schoolId };
  19. return queryTable('/hospital')(newParams, page, sort, filter);
  20. };
  21. const handleEdit = (hospital) => {
  22. setCurrent(hospital);
  23. setInputVal(hospital.name);
  24. setShow(true);
  25. };
  26. const handleAdd = () => {
  27. setCurrent();
  28. setInputVal();
  29. setShow(true);
  30. };
  31. const changeStatus = (hospital) => {
  32. // 0 变 1, 1 变 0
  33. // eslint-disable-next-line no-bitwise
  34. const status = hospital.status ^ 1;
  35. const data = { ...hospital, status }
  36. request(`/hospital/${hospital.hospitalId}`, {
  37. method: 'put',
  38. data,
  39. })
  40. .then(() => {
  41. tableRef.current.reload();
  42. notification.success({ message: '操作成功' });
  43. })
  44. .catch((e) => {
  45. notification.error({ message: e.message });
  46. });
  47. };
  48. const renderFormItem = (item, config, form) => {
  49. const handleSchoolChange = (schoolId, schoolInfo) => {
  50. form.setFieldsValue({ schoolId });
  51. form.submit();
  52. setSchool(schoolInfo);
  53. };
  54. return <School onChange={handleSchoolChange} />;
  55. };
  56. const columns = [
  57. {
  58. title: '所属学校',
  59. key: 'schoolId',
  60. dataIndex: 'schoolId',
  61. hideInTable: true,
  62. valueType: 'select',
  63. renderFormItem,
  64. },
  65. {
  66. title: '诊室名称',
  67. key: 'name',
  68. dataIndex: 'name',
  69. },
  70. {
  71. title: '状态',
  72. key: 'status',
  73. dataIndex: 'status',
  74. valueEnum: {
  75. 0: { text: '未启用', status: 'Default' },
  76. 1: { text: '启用', status: 'Success' },
  77. },
  78. hideInSearch: true,
  79. },
  80. {
  81. title: '创建日期',
  82. key: 'createDate',
  83. dataIndex: 'createDate',
  84. valueType: 'date',
  85. hideInSearch: true,
  86. },
  87. {
  88. title: '操作',
  89. key: 'option',
  90. valueType: 'option',
  91. width: 180,
  92. render: (_, hospital) => [
  93. <a key="edit" onClick={() => handleEdit(hospital)}>
  94. 修改
  95. </a>,
  96. <a key="status" onClick={() => changeStatus(hospital)}>
  97. {hospital.status === 1 ? '弃用' : '启用'}
  98. </a>,
  99. ],
  100. },
  101. ];
  102. const handleSubmit = () => {
  103. setShow(false);
  104. if (!inputVal) {
  105. return;
  106. }
  107. if (current && current.hospitalId) {
  108. request(`/hospital/${current.hospitalId}`, {
  109. method: 'put',
  110. data: { ...current, name: inputVal },
  111. })
  112. .then(() => {
  113. tableRef.current.reload();
  114. notification.success({ message: '修改成功' });
  115. })
  116. .catch((e) => {
  117. notification.error({ message: e.message });
  118. });
  119. } else {
  120. request('/hospital', { method: 'post', data: { schoolId: school.schoolId, name: inputVal } })
  121. .then(() => {
  122. tableRef.current.reload();
  123. notification.success({ message: '修改成功' });
  124. })
  125. .catch((e) => {
  126. notification.error({ message: e.message });
  127. });
  128. }
  129. };
  130. const actions = [
  131. <Button key="button" icon={<PlusOutlined />} type="primary" onClick={handleAdd}>
  132. 新建
  133. </Button>,
  134. ];
  135. return (
  136. <PageContainer>
  137. <ProTable
  138. manualRequest
  139. actionRef={tableRef}
  140. columns={columns}
  141. request={queryFunc}
  142. rowKey="hospitalId"
  143. headerTitle="诊室列表"
  144. toolBarRender={() => actions}
  145. />
  146. <Modal
  147. title={school?.name}
  148. visible={show}
  149. onOk={handleSubmit}
  150. onCancel={() => setShow(false)}
  151. >
  152. <Input size="large" value={inputVal} onChange={(e) => setInputVal(e.target.value)} />
  153. </Modal>
  154. </PageContainer>
  155. );
  156. };