import React, { useRef, useState, useEffect, forwardRef, useImperativeHandle, } from "react"; import { Form, Input, Select, message } from "antd"; import { Modal, Button, Table, Radio } from "antd"; import Formular from "./components/Formular"; import API2 from "../../../api/alarm"; import styles from "./complex.module.less"; import oneNode from "./components/oneNode"; import { useMemoizedFn } from "ahooks"; const { Search } = Input; function TranslateText(arr) { const dtLanguage = localStorage.getItem("dtLanguage"); if (!Array.isArray(arr)) { return arr; } if (arr.length < 2 && arr.length > 0) { return arr?.[0]; } if (dtLanguage === "en") { return arr?.[1]; } return arr?.[0]; } const Complex = (props, ref) => { const { editId, groupList = [], showList } = props; const [form] = Form.useForm(); const formuRef = useRef(); // const [alarmId, setAlarmId] = useState(searchParams.get('alarm_id')) // const [token, setToken] = useState(searchParams.get('token')) // const [apiUrl, setApiUrl] = useState(decodeURIComponent(searchParams.get('api_url')) + '/') const [data, setData] = useState(); const [loading, setLoading] = useState(false); const [deviceList, setDeviceList] = useState([]); const [paginationProps, setPaginationProps] = useState({ pageSize: 10, current: 1, showTotal: (total) => `共${total}条`, // showQuickJumper: true, // showSizeChanger:true, // size:'Large', }); const [alarmId, setAlarmId] = useState(null); const [type, setType] = useState(0); const [ruleOptions, setRuleOptions] = useState([]); const handleTableChange = async (pagination) => { setPaginationProps({ ...pagination, showTotal: (total) => `共${total}条` }); // setCurrentPage(pagination.current); // setPageSize(pagination.pageSize) }; useEffect(() => { onSearch(""); }, []); useEffect(() => { async function run() { if (alarmId && alarmId != -1) { const { state, data } = await API2.detailAlarm(Number(alarmId)); form.setFieldsValue({ name: data.name, alarm_level: data.alarm_level, group_id: data.group_id, }); formuRef.current.backEndInput(data.formula); setData(data); } else { form.resetFields(); form.setFieldsValue({ alarm_level: 1, }); setData(null); } } run(); }, [alarmId]); const onModalOk = useMemoizedFn(async () => { if (type === 0 && showList) { props.onConfirm(null); return; } if (alarmId == -1) { const value = await form.validateFields(); const obj = { ...value, formula: formuRef.current.getFinalStr(), ext_notify_list: [], is_complex: true, type: 1, sub_type: 12, status: 1, defer_unit: 2, }; const { state, data } = await API2.addRule(obj); if (state === 0) { props?.onConfirm?.(data?.id); form.resetFields(); message.success("保存成功"); } else { message.error("保存失败"); } } else { const value = await form.validateFields(); const obj = { ...data, ...value, formula: formuRef.current.getFinalStr(), ext_notify_list: [], }; const { state } = await API2.editRule(obj); if (state === 0) { props?.onConfirm?.(data.id); form.resetFields(); message.success("保存成功"); } else { message.error("保存失败"); } } }); useImperativeHandle(ref, () => { return { ok: onModalOk, }; }); const symbClick = (e) => { let innerText = e.target.innerText; if (innerText === "≥") { innerText = ">="; } else if (innerText === "≤") { innerText = "<="; } else if (innerText === "&&(and)") { innerText = "&&"; } else if (innerText === "||(or)") { innerText = "||"; } formuRef.current.dtInsertFormular(innerText); if (innerText === "()") { //判断为括号则把光标移到括号中间 formuRef.current.moveInTextNode(); } }; const insertToFomular = (poi) => { const { point_id, name } = poi; const res = oneNode(point_id, name); formuRef.current.dtInsertFormular(res); }; const columns = [ { title: TranslateText(["点位编号", "Point id"]), dataIndex: "point_id", ellipsis: true, }, { title: TranslateText(["点位名称", "Point name"]), dataIndex: "name", ellipsis: true, }, ]; const onSearch = async (v) => { const res = await API2.searchPoint({ key_word: v ?? "", count: 50, type: 1, no_child: true, }); if (res.state === 0) { setDeviceList(res.data || []); return; } setDeviceList([]); }; const clickTableRow = (record) => { // child.current.handlePickPoint({name:record.name, point_id:record.point_id}) insertToFomular({ name: record.name, point_id: record.point_id }); }; const onModalCancel = () => { props.onConfirm(); form.resetFields(); }; useEffect(() => { function run() { if (editId && editId !== -1) { setAlarmId(editId); if (showList) { setType(1); } } else if (showList) { setType(0); } else if (!showList) { setAlarmId(-1); } } run(); }, [editId, showList]); useEffect(() => { async function run() { if (showList) { const res = await API2.getRuleList(); setRuleOptions( res?.data?.rules?.map((item) => ({ label: item.name, value: item.id, })) ); } } run(); }, [showList]); useEffect(() => { if (showList) { if (type === 2) { setAlarmId(-1); } else if (alarmId === -1 && type !== 2) { setAlarmId(null); } } }, [type, alarmId, showList]); return (