import React, { useEffect, useState, useRef, useMemo } from 'react';
import '@ant-design/compatible/assets/index.css';
import { Modal, Input, Select, message, Cascader, Form, Tabs } from 'antd';
import { connect } from 'dva';
import { isArray, result } from 'lodash';
import { useForm } from 'rc-field-form';
import { async } from '@antv/x6/lib/registry/marker/async';
import AuditDetailed from './AuditDetailed';
import AuditFlow from './AuditFlow';
import { queryDingSchema } from '@/services/boom';
import { Form as Form3x } from '@ant-design/compatible';
const { TextArea } = Input;
const { Option } = Select;
const { TabPane } = Tabs;
// 提交
function CommitAuditModal(props) {
const { visible, onClose, onOk, loading, version, versionList, flowDetail } = props;
const [auditId, setAuditId] = useState();
const [data, setData] = useState([]);
const [length, setLength] = useState(1);
const [formData, setFromData] = useState({});
const [auditList, setAuditList] = useState([]); //用于创建Tabs表单
const [formComponentValues, setFormComponentValues] = useState({}); //用于创建Tabs表单
const [form] = Form.useForm();
useEffect(() => {
if (!visible) return;
const { edges, nodes } = flowDetail;
let Id = version.template_node_id;
const currentId = flowDetail.nodes.find?.(item => item.Id == Id)?.node_id;
const data = treeData(currentId);
if (data.length <= 0) setAuditId(currentId);
setData(data);
}, [auditId, version.template_node_id, visible]);
useEffect(() => {
form.resetFields();
setAuditList([]);
}, [visible]);
const treeData = currentId => {
const list = getNextNodes(currentId, 'custom-circle');
const fun = nodes => {
const re = nodes?.forEach((item, idx) => {
const data = getNextNodes(item.Id, 'custom-circle');
if (data || data.length > 0) list.push(...data);
fun(data);
});
};
fun(list);
const fun2 = list => {
const parents = list.filter(item => list.findIndex(node => node.Id == item.parentId) == -1);
let translator = (parents, children) => {
setLength(length + 1);
parents.forEach(parent => {
children.forEach((current, index) => {
if (current.parentId === parent.Id) {
let temp = JSON.parse(JSON.stringify(children));
temp.splice(index, 1);
translator([current], temp);
if (!parent.children.find(item => item.Id == current.Id))
parent.children.push(current);
}
});
});
};
translator(parents, list);
return parents;
};
return fun2(list);
};
const currentNodeId = useMemo(() => {
let Id = version.template_node_id;
setAuditId(currentNodeId);
return flowDetail.nodes.find?.(item => item.Id == Id)?.node_id;
}, [flowDetail, version]);
/**
*
* @param {*} currentId 当前节点
* @param {*} type 下一个节点的类型 custom-circle: 审批节点 custom-rect: 业务节点
* @returns
*/
const getNextNodes = (currentId, type) => {
const { edges, nodes } = flowDetail;
if (!currentId) return [];
//删除虚线通向的节点
// let targetIds = edges
// .filter(edge => {
// let line = edge.attrs?.line?.strokeDasharray?.split(' ');
// return edge.source.cell == currentId && line && line[0] == '0';
// })
// .map(item => item.target.cell);
let targetIds = edges
.filter(edge => edge.source.cell == currentId)
.map(item => item.target.cell);
edges.filter(edge => edge.source.cell == currentId);
let auditNodes = nodes.filter(node => {
if (type && node.name != type) {
return false;
}
return targetIds.indexOf(node.id) != -1;
});
const result = auditNodes.map(item => {
return {
label: item.label,
value: item.Id,
Id: item.node_id,
parentId: currentId,
children: [],
};
});
return result || [];
};
const changeAudit = id => {
let node = flowDetail.nodes.find?.(item => item.Id == id);
setAuditId(node.node_id);
};
const onChange = value => {
changeAudit(value[value.length - 1]);
setAuditListFun();
};
//处理tabs页
const setAuditListFun = async () => {
var fieldsValue = await form.validateFields();
let addAuditList = [];
let result = Object.values(fieldsValue)
.map(item => {
if (item && Array.isArray(item)) return item;
})
.filter(item => item)
.flat(Infinity);
let codeList = [...new Set(result)]
.map(Id => {
return flowDetail.nodes.find?.(item => item.Id == Id);
})
.filter(item => item.process_code);
for (let i = 0; i < codeList.length; i++) {
let res = await queryDingSchema({ process_code: codeList[i].process_code });
if (res) {
res.data.result.nodeId = codeList[i].Id;
addAuditList.push(res.data.result);
}
}
console.log(addAuditList);
addAuditList.forEach((item, index) => {
let Components = Form3x.create({
onValuesChange: (props, changedValues, allValues) => {
const { items } = props;
console.log(item);
formComponentValues[item.nodeId] = items
.map(item => {
const itemProps = item.props;
if (!itemProps.label) return;
let val = allValues[itemProps.id];
if (val instanceof Object) {
return {
name: itemProps.label,
id: itemProps.id,
...val,
};
}
return {
name: itemProps.label,
// id: itemProps.id,
value: allValues[itemProps.id] || '',
};
})
.filter(item => item);
setFormComponentValues({ ...formComponentValues });
},
})(AuditDetailed);
item.FormComponents =