154 lines
3.4 KiB
TypeScript
154 lines
3.4 KiB
TypeScript
import React, { useState } from 'react';
|
|
import {
|
|
Card, Table, Button, Typography, Space, Modal, Form, Tree, message,
|
|
Input, Popconfirm, Select, TreeSelect
|
|
} from 'antd';
|
|
import {
|
|
PlusOutlined, EditOutlined, DeleteOutlined, BuildOutlined
|
|
} from '@ant-design/icons';
|
|
|
|
const { Title } = Typography;
|
|
const { Option } = Select;
|
|
|
|
// 模拟组织数据
|
|
const orgData = [
|
|
{
|
|
id: 1,
|
|
name: '华东区域总部',
|
|
code: 'EAST_REGION',
|
|
parentId: 0,
|
|
status: '0',
|
|
|
|
},
|
|
{
|
|
id: 2,
|
|
name: '华北区域总部',
|
|
code: 'NORTH_REGION',
|
|
parentId: 0,
|
|
status: '0',
|
|
},
|
|
];
|
|
|
|
// 转平数组用于表格
|
|
const flatten = (list) => {
|
|
let arr = [];
|
|
const loop = (data) => {
|
|
data.forEach(item => {
|
|
arr.push(item);
|
|
if (item.children) loop(item.children);
|
|
});
|
|
};
|
|
loop(list);
|
|
return arr;
|
|
};
|
|
|
|
export default function Organization() {
|
|
const [orgList, setOrgList] = useState(flatten(orgData));
|
|
const [treeData] = useState(orgData);
|
|
|
|
const [modalVisible, setModalVisible] = useState(false);
|
|
const [form] = Form.useForm();
|
|
const [editRecord, setEditRecord] = useState(null);
|
|
|
|
// 打开新增
|
|
const handleAdd = () => {
|
|
form.resetFields();
|
|
setEditRecord(null);
|
|
setModalVisible(true);
|
|
};
|
|
|
|
// 打开编辑
|
|
const handleEdit = (record) => {
|
|
form.setFieldsValue(record);
|
|
setEditRecord(record);
|
|
setModalVisible(true);
|
|
};
|
|
|
|
// 删除
|
|
const handleDelete = (id) => {
|
|
setOrgList(orgList.filter(i => i.id !== id));
|
|
message.success('删除成功');
|
|
};
|
|
|
|
// 保存
|
|
const handleSave = () => {
|
|
form.validateFields().then(values => {
|
|
if (editRecord) {
|
|
setOrgList(orgList.map(i => i.id === editRecord.id ? { ...i, ...values } : i));
|
|
message.success('编辑成功');
|
|
} else {
|
|
setOrgList([...orgList, { ...values, id: Date.now() }]);
|
|
message.success('新增成功');
|
|
}
|
|
setModalVisible(false);
|
|
});
|
|
};
|
|
|
|
const columns = [
|
|
{ title: '组织名称', dataIndex: 'name' },
|
|
{ title: '组织编码', dataIndex: 'code' },
|
|
{
|
|
title: '状态',
|
|
dataIndex: 'status',
|
|
render: s => s === '0' ?
|
|
<Button type="text" size="small">正常</Button> :
|
|
<Button type="text" danger size="small">禁用</Button>
|
|
},
|
|
{
|
|
title: '操作',
|
|
render: (_, r) => (
|
|
<Space>
|
|
<Button type="link" icon={<EditOutlined />} onClick={() => handleEdit(r)}>编辑</Button>
|
|
<Popconfirm title="确定删除?" onConfirm={() => handleDelete(r.id)}>
|
|
<Button type="link" danger>删除</Button>
|
|
</Popconfirm>
|
|
</Space>
|
|
)
|
|
}
|
|
];
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<div className="flex justify-between items-center">
|
|
<Title level={5}>组织管理</Title>
|
|
<Button type="primary" icon={<PlusOutlined />} onClick={handleAdd}>
|
|
新增组织
|
|
</Button>
|
|
</div>
|
|
|
|
<Card>
|
|
<Table
|
|
rowKey="id"
|
|
columns={columns}
|
|
dataSource={orgList}
|
|
pagination={false}
|
|
/>
|
|
</Card>
|
|
|
|
<Modal
|
|
title={editRecord ? '编辑组织' : '新增组织'}
|
|
open={modalVisible}
|
|
onCancel={() => setModalVisible(false)}
|
|
onOk={handleSave}
|
|
width={500}
|
|
>
|
|
<Form form={form} layout="vertical">
|
|
<Form.Item name="name" label="组织名称" rules={[{ required: true }]}>
|
|
<Input />
|
|
</Form.Item>
|
|
<Form.Item name="code" label="组织编码" rules={[{ required: true }]}>
|
|
<Input />
|
|
</Form.Item>
|
|
|
|
<Form.Item name="status" label="状态">
|
|
<Select>
|
|
<Option value="0">正常</Option>
|
|
<Option value="1">禁用</Option>
|
|
</Select>
|
|
</Form.Item>
|
|
</Form>
|
|
</Modal>
|
|
</div>
|
|
);
|
|
}
|