Initial commit: ERP system with advance verification fixes
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { Table, Button, Modal, Form, Input, message } from 'antd';
|
||||
import { PlusOutlined, EditOutlined, DeleteOutlined, EyeOutlined } from '@ant-design/icons';
|
||||
import { customerService } from '../services/customer.service';
|
||||
import type { Customer } from '../services/customer.service';
|
||||
|
||||
const CustomerManagement: React.FC = () => {
|
||||
const [customers, setCustomers] = useState<Customer[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [modalVisible, setModalVisible] = useState(false);
|
||||
const [modalType, setModalType] = useState<'create' | 'edit'>('create');
|
||||
const [currentCustomer, setCurrentCustomer] = useState<Customer | null>(null);
|
||||
const [form] = Form.useForm();
|
||||
|
||||
const fetchCustomers = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const data = await customerService.getCustomers();
|
||||
setCustomers(data);
|
||||
} catch (error) {
|
||||
message.error('获取客户列表失败');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchCustomers();
|
||||
}, []);
|
||||
|
||||
const handleCreate = () => {
|
||||
setModalType('create');
|
||||
setCurrentCustomer(null);
|
||||
form.resetFields();
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
const handleEdit = (customer: Customer) => {
|
||||
setModalType('edit');
|
||||
setCurrentCustomer(customer);
|
||||
form.setFieldsValue(customer);
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
const handleDelete = async (id: string) => {
|
||||
try {
|
||||
await customerService.deleteCustomer(id);
|
||||
message.success('删除成功');
|
||||
fetchCustomers();
|
||||
} catch (error) {
|
||||
message.error('删除失败');
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmit = async () => {
|
||||
try {
|
||||
const values = await form.validateFields();
|
||||
if (modalType === 'create') {
|
||||
await customerService.createCustomer(values);
|
||||
message.success('创建成功');
|
||||
} else if (modalType === 'edit' && currentCustomer) {
|
||||
await customerService.updateCustomer(currentCustomer.id, values);
|
||||
message.success('更新成功');
|
||||
}
|
||||
setModalVisible(false);
|
||||
fetchCustomers();
|
||||
} catch (error) {
|
||||
message.error('操作失败');
|
||||
}
|
||||
};
|
||||
|
||||
const columns = [
|
||||
{
|
||||
title: '客户名称',
|
||||
dataIndex: 'name',
|
||||
key: 'name',
|
||||
},
|
||||
{
|
||||
title: '联系人',
|
||||
dataIndex: 'contact_person',
|
||||
key: 'contact_person',
|
||||
},
|
||||
{
|
||||
title: '电话',
|
||||
dataIndex: 'phone',
|
||||
key: 'phone',
|
||||
},
|
||||
{
|
||||
title: '邮箱',
|
||||
dataIndex: 'email',
|
||||
key: 'email',
|
||||
},
|
||||
{
|
||||
title: '地址',
|
||||
dataIndex: 'address',
|
||||
key: 'address',
|
||||
ellipsis: true,
|
||||
},
|
||||
{
|
||||
title: '创建时间',
|
||||
dataIndex: 'created_at',
|
||||
key: 'created_at',
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
render: (_: any, record: Customer) => (
|
||||
<div>
|
||||
<Button
|
||||
type="primary"
|
||||
icon={<EyeOutlined />}
|
||||
style={{ marginRight: 8 }}
|
||||
onClick={() => handleEdit(record)}
|
||||
>
|
||||
查看
|
||||
</Button>
|
||||
<Button
|
||||
icon={<EditOutlined />}
|
||||
style={{ marginRight: 8 }}
|
||||
onClick={() => handleEdit(record)}
|
||||
>
|
||||
编辑
|
||||
</Button>
|
||||
<Button
|
||||
danger
|
||||
icon={<DeleteOutlined />}
|
||||
onClick={() => handleDelete(record.id)}
|
||||
>
|
||||
删除
|
||||
</Button>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div style={{ marginBottom: 16, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
||||
<h1>客户管理</h1>
|
||||
<Button type="primary" icon={<PlusOutlined />} onClick={handleCreate}>
|
||||
新增客户
|
||||
</Button>
|
||||
</div>
|
||||
<Table
|
||||
columns={columns}
|
||||
dataSource={customers}
|
||||
rowKey="id"
|
||||
loading={loading}
|
||||
pagination={{ pageSize: 10 }}
|
||||
/>
|
||||
<Modal
|
||||
title={modalType === 'create' ? '新增客户' : '编辑客户'}
|
||||
open={modalVisible}
|
||||
onOk={handleSubmit}
|
||||
onCancel={() => setModalVisible(false)}
|
||||
>
|
||||
<Form form={form} layout="vertical">
|
||||
<Form.Item
|
||||
name="name"
|
||||
label="客户名称"
|
||||
rules={[{ required: true, message: '请输入客户名称' }]}
|
||||
>
|
||||
<Input placeholder="请输入客户名称" />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
name="contact_person"
|
||||
label="联系人"
|
||||
>
|
||||
<Input placeholder="请输入联系人" />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
name="phone"
|
||||
label="电话"
|
||||
>
|
||||
<Input placeholder="请输入电话" />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
name="email"
|
||||
label="邮箱"
|
||||
>
|
||||
<Input placeholder="请输入邮箱" />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
name="address"
|
||||
label="地址"
|
||||
>
|
||||
<Input.TextArea placeholder="请输入地址" rows={3} />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default CustomerManagement;
|
||||
Reference in New Issue
Block a user