ThresholdModal.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import {
  2. Button,
  3. Card,
  4. Col,
  5. Form,
  6. Input,
  7. InputNumber,
  8. Modal,
  9. Row,
  10. Select,
  11. } from 'antd';
  12. import { useEffect, useState } from 'react';
  13. const { Option } = Select;
  14. export default function ThresholdModal(props) {
  15. const { data, onOk, visible, onClose, form, edit = false, loading } = props;
  16. const [jsonNumThreshold, setJsonNumThreshold] = useState({});
  17. const renderThresholdItem = (item, index, key) => {
  18. // const { ThresholdType } = this.state;
  19. const ThresholdType =
  20. form.getFieldValue(`JsonNumThreshold[${key}][${index}].ThresholdType`) ||
  21. item?.ThresholdType;
  22. return (
  23. <Col span={24}>
  24. <Card
  25. size="small"
  26. style={{ marginBottom: 16 }}
  27. title={`阈值${index + 1}`}
  28. >
  29. <Form.Item
  30. label="阈值类型"
  31. name={`JsonNumThreshold[${key}][${index}].ThresholdType`}
  32. initialValue={item.ThresholdType}
  33. rules={[{ required: true, message: '请选择阈值类型' }]}
  34. >
  35. <Select disabled={!edit} style={{ width: '100%' }}>
  36. <Option value={0}>无</Option>
  37. <Option value={1}>大于</Option>
  38. <Option value={2}>小于</Option>
  39. <Option value={3}>等于</Option>
  40. <Option value={4}>区间</Option>
  41. <Option value={5}>区间外</Option>
  42. <Option value={6}>区间左包含</Option>
  43. <Option value={7}>区间右包含</Option>
  44. <Option value={8}>区间全包含</Option>
  45. <Option value={9}>区间外左包含</Option>
  46. <Option value={10}>区间外右包含</Option>
  47. <Option value={11}>区间外全包含</Option>
  48. <Option value={12}>大于等于</Option>
  49. <Option value={13}>小于等于</Option>
  50. </Select>
  51. </Form.Item>
  52. {ThresholdType !== 0 && (
  53. <Form.Item
  54. label="阈值"
  55. name={`JsonNumThreshold[${key}][${index}].ThresholdValue`}
  56. initialValue={item.ThresholdValue}
  57. rules={[{ required: true, message: '请输入阈值' }]}
  58. >
  59. <InputNumber
  60. style={{ width: '100%' }}
  61. disabled={!edit}
  62. placeholder="请输入阈值"
  63. />
  64. </Form.Item>
  65. )}
  66. {ThresholdType >= 4 && ThresholdType < 12 && (
  67. <Form.Item
  68. label="阈值2"
  69. name={`JsonNumThreshold[${key}][${index}].ThresholdValue2`}
  70. initialValue={item.ThresholdValue2}
  71. rules={[{ required: true, message: '请输入阈值' }]}
  72. >
  73. <InputNumber
  74. style={{ width: '100%' }}
  75. disabled={!edit}
  76. placeholder="请输入阈值"
  77. />
  78. </Form.Item>
  79. )}
  80. <Form.Item
  81. label="处理意见"
  82. name={`JsonNumThreshold[${key}][${index}].ThresholdDesc`}
  83. initialValue={item.ThresholdDesc}
  84. >
  85. <Input style={{ width: '100%' }} disabled={!edit} />
  86. </Form.Item>
  87. </Card>
  88. </Col>
  89. );
  90. };
  91. const addThreshold = (key) => {
  92. if (!jsonNumThreshold[key]) jsonNumThreshold[key] = [];
  93. jsonNumThreshold[key].push({});
  94. setJsonNumThreshold({ ...jsonNumThreshold });
  95. };
  96. const deleteThreshold = (index, key) => {
  97. jsonNumThreshold[key].splice(index, 1);
  98. setJsonNumThreshold({ ...jsonNumThreshold });
  99. };
  100. const handleOk = () => {
  101. form.validateFields((err, fieldsValue) => {
  102. if (err) return;
  103. onOk(fieldsValue.JsonNumThreshold);
  104. });
  105. };
  106. useEffect(() => {
  107. // console.log(data);
  108. setJsonNumThreshold(JSON.parse(JSON.stringify(data || {})));
  109. }, [data]);
  110. return (
  111. <Modal
  112. title="阈值详情"
  113. onOk={handleOk}
  114. open={visible}
  115. footer={!edit ? null : undefined}
  116. onCancel={onClose}
  117. width={800}
  118. confirmLoading={loading}
  119. >
  120. <Form.Item
  121. className="btn-item"
  122. labelCol={{ span: 7 }}
  123. wrapperCol={{ span: 16 }}
  124. label="警告阈值范围"
  125. >
  126. {edit && (
  127. <Button
  128. style={{ marginBottom: 20 }}
  129. onClick={() => addThreshold('exception')}
  130. >
  131. 增加阈值
  132. </Button>
  133. )}
  134. <Row gutter={16}>
  135. {jsonNumThreshold.exception?.map((item, index) =>
  136. renderThresholdItem(item, index, 'exception'),
  137. )}
  138. </Row>
  139. </Form.Item>
  140. <Form.Item
  141. className="btn-item"
  142. labelCol={{ span: 7 }}
  143. wrapperCol={{ span: 16 }}
  144. label="异常阈值范围"
  145. >
  146. {edit && (
  147. <Button
  148. style={{ marginBottom: 20 }}
  149. onClick={() => addThreshold('breakdown')}
  150. >
  151. 增加阈值
  152. </Button>
  153. )}
  154. <Row gutter={16}>
  155. {jsonNumThreshold.breakdown?.map((item, index) =>
  156. renderThresholdItem(item, index, 'breakdown'),
  157. )}
  158. </Row>
  159. </Form.Item>
  160. </Modal>
  161. );
  162. }