Files
fe_supplier_frontend/src/pages/supplier/supplierExit/supplierExitManage/components/SupplierSelectModal.tsx

227 lines
6.9 KiB
TypeScript
Raw Normal View History

2025-06-24 10:52:30 +08:00
import React, { useEffect, useState } from "react";
2025-06-27 10:41:33 +08:00
import {
Modal, Table, Button, Checkbox, Row, Col, Input, Select, Form, Space, message
} from "antd";
import { getSupplierCategoryPage } from '../services';
2025-06-24 10:52:30 +08:00
const { Option } = Select;
2025-06-27 10:41:33 +08:00
interface Supplier {
id: number;
supplierId: string;
categoryId: string;
supplierName: string;
unit: string; // 准入部门
accessTime: string; // 准入时间
categoryName: string; // 准入品类
lastEval: string; // 最近一次评价
lastEvalDate: string; // 评价时间
}
interface SupplierSelectModalProps {
visible: boolean;
selectedSuppliers: Supplier[];
onOk: (suppliers: Supplier[]) => void;
onCancel: () => void;
}
const SupplierSelectModal: React.FC<SupplierSelectModalProps> = ({
2025-06-24 10:52:30 +08:00
visible,
2025-06-27 10:41:33 +08:00
selectedSuppliers,
2025-06-24 10:52:30 +08:00
onOk,
onCancel,
}) => {
const [form] = Form.useForm();
2025-06-27 10:41:33 +08:00
const [leftSelected, setLeftSelected] = useState<number[]>([]);
const [rightSelected, setRightSelected] = useState<number[]>([]);
const [rightData, setRightData] = useState<Supplier[]>([]);
const [data, setData] = useState<Supplier[]>([]);
const [pagination, setPagination] = useState({ current: 1, pageSize: 5, total: 0 });
const [loading, setLoading] = useState(false);
2025-06-24 10:52:30 +08:00
useEffect(() => {
2025-06-27 10:41:33 +08:00
if (visible) {
setRightData(selectedSuppliers || []);
fetchData();
}
}, [visible]);
const fetchData = async (params: any = {}, pageNo = 1, pageSize = 5) => {
setLoading(true);
try {
const res = await getSupplierCategoryPage({ ...params, pageNo, pageSize });
if (res.code === 200) {
setData(res.data.records);
setPagination({ current: pageNo, pageSize: pageSize, total: res.data.total });
} else {
message.error('获取数据失败');
}
} catch {
message.error('获取数据异常');
} finally {
setLoading(false);
}
};
2025-06-24 10:52:30 +08:00
const handleSearch = () => {
2025-06-27 10:41:33 +08:00
const values = form.getFieldsValue();
fetchData(values, 1, pagination.pageSize);
2025-06-24 10:52:30 +08:00
};
2025-06-27 10:41:33 +08:00
2025-06-24 10:52:30 +08:00
const handleReset = () => {
form.resetFields();
2025-06-27 10:41:33 +08:00
fetchData({}, 1, pagination.pageSize);
};
const handleAdd = () => {
const added = data.filter(s => leftSelected.includes(s.id));
const combined = [...rightData, ...added.filter(i => !rightData.some(r => r.id === i.id))];
setRightData(combined);
2025-06-24 10:52:30 +08:00
setLeftSelected([]);
};
2025-06-27 10:41:33 +08:00
const handleRemove = () => {
setRightData(rightData.filter(i => !rightSelected.includes(i.id)));
setRightSelected([]);
};
const handleOk = () => {
onOk(rightData);
};
const columns = [
2025-06-24 10:52:30 +08:00
{
title: <Checkbox
2025-06-27 10:41:33 +08:00
checked={leftSelected.length === data.length && data.length > 0}
indeterminate={leftSelected.length > 0 && leftSelected.length < data.length}
2025-06-24 10:52:30 +08:00
onChange={e => {
2025-06-27 10:41:33 +08:00
setLeftSelected(e.target.checked ? data.map(i => i.id) : []);
2025-06-24 10:52:30 +08:00
}}
></Checkbox>,
dataIndex: "select",
2025-06-27 10:41:33 +08:00
width: 80,
render: (_: any, record: Supplier) => (
2025-06-24 10:52:30 +08:00
<Checkbox
checked={leftSelected.includes(record.id)}
onChange={e => {
setLeftSelected(e.target.checked
? [...leftSelected, record.id]
: leftSelected.filter(id => id !== record.id));
}}
></Checkbox>
)
},
2025-06-27 10:41:33 +08:00
{ title: "供应商名称", dataIndex: "supplierName" },
{ title: "准入单位", dataIndex: "deptId" },
{ title: "准入部门", dataIndex: "deptId" },
{ title: "准入品类", dataIndex: "categoryName" }
2025-06-24 10:52:30 +08:00
];
const rightColumns = [
{
title: <Checkbox
checked={rightSelected.length === rightData.length && rightData.length > 0}
indeterminate={rightSelected.length > 0 && rightSelected.length < rightData.length}
onChange={e => {
setRightSelected(e.target.checked ? rightData.map(i => i.id) : []);
}}
></Checkbox>,
dataIndex: "select",
2025-06-27 10:41:33 +08:00
width: 80,
render: (_: any, record: Supplier) => (
2025-06-24 10:52:30 +08:00
<Checkbox
checked={rightSelected.includes(record.id)}
onChange={e => {
setRightSelected(e.target.checked
? [...rightSelected, record.id]
: rightSelected.filter(id => id !== record.id));
}}
></Checkbox>
)
},
2025-06-27 10:41:33 +08:00
{ title: "供应商名称", dataIndex: "supplierName" },
{ title: "准入单位", dataIndex: "deptId" },
{ title: "准入部门", dataIndex: "deptId" },
{ title: "准入品类", dataIndex: "categoryName" }
2025-06-24 10:52:30 +08:00
];
return (
<Modal
title="选择供应商"
visible={visible}
onCancel={onCancel}
onOk={handleOk}
width={1000}
destroyOnClose
okText="确认"
cancelText="取消"
bodyStyle={{ padding: 24 }}
>
<Form layout="inline" form={form} style={{ marginBottom: 16 }}>
<Form.Item name="name" label="供应商名称">
<Input style={{ width: 180 }} allowClear />
</Form.Item>
<Form.Item name="evalLevel" label="最近一次评价等级">
2025-06-27 10:41:33 +08:00
<Select style={{ width: 140 }} allowClear>
{['A', 'B', 'C', 'D'].map(level => (
<Option key={level} value={level}>{level}</Option>
))}
2025-06-24 10:52:30 +08:00
</Select>
</Form.Item>
<Form.Item name="category" label="准入品类">
2025-06-27 10:41:33 +08:00
<Select style={{ width: 140 }} allowClear>
{['燃油', '润滑油', '备件'].map(cat => (
<Option key={cat} value={cat}>{cat}</Option>
))}
2025-06-24 10:52:30 +08:00
</Select>
</Form.Item>
<Form.Item>
<Space>
<Button type="primary" onClick={handleSearch}></Button>
<Button onClick={handleReset}></Button>
</Space>
</Form.Item>
</Form>
2025-06-27 10:41:33 +08:00
2025-06-24 10:52:30 +08:00
<Row gutter={32}>
<Col span={11}>
2025-06-27 10:41:33 +08:00
<div style={{ marginBottom: 8 }}></div>
2025-06-24 10:52:30 +08:00
<Table
rowKey="id"
2025-06-27 10:41:33 +08:00
dataSource={data}
columns={columns}
loading={loading}
pagination={pagination}
onChange={(pag) => {
const values = form.getFieldsValue();
fetchData(values, pag.current!, pag.pageSize!);
}}
2025-06-24 10:52:30 +08:00
size="small"
bordered
/>
</Col>
<Col span={2} style={{ textAlign: "center", display: "flex", flexDirection: "column", justifyContent: "center" }}>
<Button type="primary" disabled={!leftSelected.length} onClick={handleAdd} style={{ marginBottom: 16 }}>{">"}</Button>
<Button disabled={!rightSelected.length} onClick={handleRemove}>{"<"}</Button>
</Col>
<Col span={11}>
<div style={{ marginBottom: 8 }}></div>
<Table
rowKey="id"
2025-06-27 10:41:33 +08:00
dataSource={rightData}
columns={rightColumns}
2025-06-24 10:52:30 +08:00
pagination={false}
size="small"
bordered
/>
</Col>
</Row>
</Modal>
);
};
export default SupplierSelectModal;