tsg-web/src/views/rcgl/jdkh/khzbgl/form.js

318 lines
8.3 KiB
JavaScript
Raw Normal View History

2024-09-20 15:02:50 +08:00
import React,{useEffect,useState,useMemo,useRef} from 'react';
import { Form, Button, Input, Row,Table, Col, Popconfirm, InputNumber,Image,Modal,Typography} from 'antd';
import { DeleteOutlined,FileWordOutlined,FilePdfOutlined,FileZipOutlined,FileExcelOutlined } from '@ant-design/icons';
import { formItemLayout, btnItemLayout } from '../../../../components/crud/FormLayoutProps';
import { httpget2, httppost2,xyt_httpget2 } from '../../../../utils/request';
import apiurl from '../../../../service/apiurl';
import NormalSelect from '../../../../components/Form/NormalSelect';
import moment from 'moment';
const EditableCell = ({
editing,
dataIndex,
title,
inputType,
record,
index,
children,
...restProps
}) => {
const inputNode = inputType === 'number' ?
<InputNumber style={{ textAlign: "center", flex: 1 }} min={0} /> :
<Input style={{ textAlign: "center" }} />;
return (
<td {...restProps}>
{editing ? (
<Form.Item
name={dataIndex}
wrapperCol={[24]}
style={{
margin: 0,
}}
>
{inputNode}
</Form.Item>
) : (
children
)}
</td>
);
};
const ModalForm = ({ mode, record, onEdit, onSave, onSimilarSave }) => {
const isEditing = (record) => {
return record.id === editingKey;
}
const columns = [
{
title: "序号",
dataIndex: "index",
width: 60,
render: (text, record, index) => <span>{ index + 1}</span>,
align: "center"
},
{
title: '描述',
key: 'ratingDesc',
dataIndex: 'ratingDesc',
width: 250
, align: "center",
editable: true,
},
{
title: '标准评分',
key: 'standardScore',
dataIndex: 'standardScore',
width: 100,
align: "center",
editable: true,
},
{
title: '操作', key: 'operation', width: 240, fixed: 'right',align: 'center',
render: (_, record) => {
const editable = isEditing(record);
return editable ? (
<span>
<Typography.Link
onClick={() => save(record.id)}
style={{
marginRight: 8,
}}
>
完成
</Typography.Link>
</span>
) : (
<div style={{display:"flex",justifyContent:"center",columnGap:10}}>
<Typography.Link
disabled={editingKey !== ''}
onClick={() => edit1(record)}
>
编辑
</Typography.Link>
<Popconfirm
title="确定要删除?"
onConfirm={() => handleDelete(record.id)}
>
<a>删除</a>
</Popconfirm>
</div>
);
},
},
]
const [form] = Form.useForm();
const [form1] = Form.useForm();
const [editingKey, setEditingKey] = useState('');
const [disabled, setDisabled] = useState(false)
const [details, setDetails] = useState([])
const [standardScore1, setStandardScore] = useState('')
const detailsRef = useRef(null)
detailsRef.current = details
const newColumns = useMemo(() => {
let data = columns;
if (mode == "view") {
data.pop();
return data
} else {
return data;
}
}, [mode,editingKey])
const edit1 = (record) => {
setDisabled(true)
form1.setFieldsValue({
month: '',
drp:'',
...record,
});
setEditingKey(record.id);
};
const handleAddRow = () => {
setDisabled(true)
const newData = {
id: (details.length + 1).toString(),
ratingDesc: '',
standardScore:''
};
form1.setFieldsValue(newData)
setDetails([...details, newData]);
detailsRef.current = [...details, newData]
setEditingKey(newData.id);
};
const handleDelete = (key) => {
const newData = detailsRef.current.filter((item) => item.id !== key);
setDetails(newData);
detailsRef.current = newData
let total = newData.reduce((total,cur) => total + Number(cur.standardScore),0)
setStandardScore(total)
};
const save = async (key) => {
setDisabled(false)
try {
const row = await form1.validateFields();
const newData = [...details];
const index = newData.findIndex((item) => key === item.id);
if (index > -1) {
const item = newData[index];
newData.splice(index, 1, {
...item,
...row,
});
setDetails(newData);
detailsRef.current = newData
setEditingKey('');
let total = newData.reduce((total,cur) => total + Number(cur.standardScore),0)
setStandardScore(total)
} else {
newData.push(row);
setDetails(newData);
detailsRef.current = newData
setEditingKey('');
}
} catch (errInfo) {
console.log('Validate Failed:', errInfo);
}
};
const mergedColumns = newColumns.map((col) => {
if (!col.editable) {
return col;
}
return {
...col,
onCell: (record) => ({
record,
inputType: col.dataIndex === 'standardScore' ? 'number' : "text",
dataIndex: col.dataIndex,
title: col.title,
editing: isEditing(record),
}),
};
});
const onfinish = () => {
let values = form.getFieldsValue();
values.indicatorRatings = details;
values.categoryId = record.code;
values.standardScore = standardScore1;
if (mode === 'edit') {
values.id = record.id;
2024-10-16 17:52:43 +08:00
values.status = record.status;
2024-09-20 15:02:50 +08:00
onEdit(apiurl.rcgl.jdkh.khzbgl.edit,values)
}
if (mode === 'save') {
onSave(apiurl.rcgl.jdkh.khzbgl.save,values)
}
}
useEffect(() => {
if (mode !== "save") {
2024-10-21 11:13:02 +08:00
let total = record?.indicatorRatings?.reduce((total, cur) => total + cur.standardScore, 0)
2024-09-20 15:02:50 +08:00
setStandardScore(total)
setDetails(record.indicatorRatings)
detailsRef.current = record.indicatorRatings
}
}, [record,mode])
return (
<>
<Form
form={form}
{...formItemLayout}
onFinish={onfinish}
initialValues={record}
>
<Row>
<Col span={12}>
<Form.Item
label="指标名称"
name="indicatorName"
rules={[
{
required: true,
},
]}
>
<Input disabled={mode==='view'} style={{width:'100%'}} allowClear />
</Form.Item>
</Col>
<Col span={12}>
<Form.Item
label="指标编号"
name="indicatorCode"
>
<Input disabled={mode !='save'} style={{width:'100%'}} allowClear />
</Form.Item>
</Col>
</Row>
<Row>
<Col span={12}>
<Form.Item
label="排序号"
name="orderIndex"
>
<Input disabled={mode==='view'} style={{width:'100%'}} allowClear />
</Form.Item>
</Col>
</Row>
<Row>
<Col span={12}>
<Form.Item
label="评分细则"
>
<Button
type="primary"
disabled={disabled || mode == "view"}
onClick={handleAddRow}
style={{ width: '23%' }}>新增</Button>
</Form.Item>
</Col>
</Row>
</Form>
<div style={{display:"flex",justifyContent:"space-between"}}>
<span style={{ marginLeft: "auto" }}>标准分数{ standardScore1}</span>
</div>
<Form form={form1} component={false}>
<Table
rowKey="id"
components={{
body: {
// row: EditableRow,
cell: EditableCell,
},
}}
columns={mergedColumns}
dataSource={details}
// scroll={{ x: width}}
pagination={false}
/>
{
mode==='view'?null:(
<div style={{marginTop:20}}>
<Form.Item wrapperCol={{span:2,offset:22}}>
<Button type="primary" onClick={onfinish} disabled={disabled}>
{mode === 'save' ? '保存' :
'修改'}
</Button>
</Form.Item>
</div>
)
}
</Form>
</>
);
}
export default ModalForm;