Files
fe_supplier_frontend/src/components/CompanyInfo/component/BankFormModal.tsx

217 lines
8.1 KiB
TypeScript
Raw Normal View History

2025-06-27 10:41:33 +08:00
import React, { useEffect, useState } from 'react';
2025-07-24 09:10:24 +08:00
import { Modal, Form, Input, message, Row, Col, Descriptions, Select } from 'antd';
2025-07-15 17:04:54 +08:00
import { getDictList } from '@/servers/api/dicts';
2025-07-16 08:59:49 +08:00
import { bankView, bankAdd, bankEdit } from '../services';
2025-07-15 17:04:54 +08:00
import type { DictItem } from '@/servers/api/dicts';
2025-07-24 09:10:24 +08:00
import DictRegionSelect from '@/components/CommonSelect/DictRegionSelect'
2025-07-15 17:04:54 +08:00
2025-06-27 10:41:33 +08:00
interface props {
visible: boolean;
onOk: () => void;
onCancel: () => void;
initialValues?: any;
readOnly?: boolean;
}
interface viewDataData {
id?: string | null;
account?: string;
accountName?: string;
bank?: string;
city?: string;
2025-07-22 13:17:34 +08:00
cityName?: string;
2025-06-27 10:41:33 +08:00
currency?: string;
interbankNumber?: string;
2025-07-22 13:17:34 +08:00
nationName?: string;
2025-06-27 10:41:33 +08:00
nation?: string;
2025-07-22 13:17:34 +08:00
provinceName?: string;
2025-06-27 10:41:33 +08:00
province?: string;
supplierId?: string;
swiftCode?: null;
attachment?: {
uid: string;
name: string;
url: string;
status: string;
}[];
}
const InvoiceFormModal: React.FC<props> = ({
visible,
onOk,
onCancel,
initialValues,
readOnly = false,
}) => {
const userId = sessionStorage.getItem('userId') || '';
2025-06-27 10:41:33 +08:00
// 新增与修改
const [form] = Form.useForm();
//查看
const [viewData, setViewData] = useState<viewDataData>({});
2025-07-22 13:17:34 +08:00
//提交防抖
const [submitting, setSubmitting] = useState(false);
2025-07-15 17:04:54 +08:00
const [currency, setCurrency] = useState<DictItem[]>([]);
2025-06-27 10:41:33 +08:00
useEffect(() => {
if (visible) {
if (initialValues) {
bankView(initialValues.id).then((res) => {
const { code, data } = res;
if (code === 200) {
const fields = {
...data,
id: data.id ? data.id : null,
2025-07-15 17:04:54 +08:00
address: [
2025-07-16 08:33:38 +08:00
Number(data.nation),
2025-07-15 17:04:54 +08:00
Number(data.province),
Number(data.city),
]
2025-06-27 10:41:33 +08:00
};
2025-07-02 16:18:03 +08:00
console.log(fields);
2025-06-27 10:41:33 +08:00
form.setFieldsValue(fields);
setViewData(fields);
}
});
} else {
2025-07-24 09:10:24 +08:00
form.resetFields();
2025-06-27 10:41:33 +08:00
}
2025-07-15 17:04:54 +08:00
getDictList('currency').then((res) => {
if (res.code === 200) {
setCurrency(res.data);
}
});
2025-06-27 10:41:33 +08:00
}
}, [visible, initialValues]);
// 提交
const handleFinish = async () => {
2025-07-22 13:17:34 +08:00
if (submitting) return; // 防重复提交
setSubmitting(true);
2025-06-27 10:41:33 +08:00
try {
const values = await form.validateFields();
const payload = {
...values,
supplierId: userId,
2025-06-27 10:41:33 +08:00
};
2025-07-15 17:04:54 +08:00
payload.province = payload.address[1];
payload.city = payload.address[2];
payload.nation = payload.address[0];
2025-06-27 10:41:33 +08:00
if (!values.id) {
bankAdd(payload).then((res) => {
if (res.code == 200) {
message.success('新增成功');
onOk();
}
})
} else {
bankEdit(payload).then((res) => {
if (res.code == 200) {
message.success('修改成功');
onOk();
}
})
}
} catch (error) {
console.error('表单校验失败:', error);
2025-07-22 13:17:34 +08:00
} finally {
setSubmitting(false); // 无论成功失败都解锁
2025-06-27 10:41:33 +08:00
}
};
return (
<Modal
title={readOnly ? '查看' : initialValues ? '修改' : '新增'}
visible={visible}
onCancel={onCancel}
onOk={readOnly ? onCancel : handleFinish}
footer={readOnly ? null : undefined}
destroyOnClose
width={600}
>
{readOnly ? (
<Descriptions
column={1}
bordered
size="middle"
labelStyle={{ width: 160 }}
>
<Descriptions.Item label="开户账号">{viewData.account}</Descriptions.Item>
<Descriptions.Item label="账户名称">{viewData.accountName}</Descriptions.Item>
<Descriptions.Item label="开户银行">{viewData.bank}</Descriptions.Item>
<Descriptions.Item label="联行号">{viewData.interbankNumber}</Descriptions.Item>
2025-07-22 13:17:34 +08:00
<Descriptions.Item label="国家/地区">{viewData.nationName}</Descriptions.Item>
<Descriptions.Item label="省份">{viewData.provinceName}</Descriptions.Item>
<Descriptions.Item label="城市">{viewData.cityName}</Descriptions.Item>
2025-06-27 10:41:33 +08:00
<Descriptions.Item label="币种">{viewData.currency}</Descriptions.Item>
</Descriptions>
) : (
<Form form={form} labelCol={{ flex: '120px' }} wrapperCol={{ flex: 1 }}>
<Row gutter={24}>
<Col span={24}>
<Form.Item name="account" label="开户账号" rules={[{ required: true }]}>
2025-07-18 10:37:13 +08:00
<Input placeholder='请输入开户账号' />
2025-06-27 10:41:33 +08:00
</Form.Item>
</Col>
<Col span={24}>
<Form.Item name="accountName" label="账户名称" rules={[{ required: true }]}>
2025-07-18 10:37:13 +08:00
<Input placeholder='请输入账户名称' />
2025-06-27 10:41:33 +08:00
</Form.Item>
</Col>
<Col span={24}>
<Form.Item name="bank" label="开户银行" rules={[{ required: true }]}>
2025-07-18 10:37:13 +08:00
<Input placeholder='请输入开户银行' />
2025-06-27 10:41:33 +08:00
</Form.Item>
</Col>
<Col span={24}>
<Form.Item name="interbankNumber" label="联行号" rules={[{ required: true }]}>
2025-07-18 10:37:13 +08:00
<Input placeholder='请输入联行号' />
2025-06-27 10:41:33 +08:00
</Form.Item>
</Col>
2025-07-15 15:10:21 +08:00
2025-06-27 10:41:33 +08:00
<Col span={24}>
2025-07-15 15:10:21 +08:00
<Form.Item name="address" label="地址" rules={[{ required: true }]}>
2025-07-24 09:10:24 +08:00
<DictRegionSelect />
2025-07-15 15:10:21 +08:00
</Form.Item>
</Col>
{/* <Col span={24}>
2025-06-27 10:41:33 +08:00
<Form.Item name="province" label="省份" rules={[{ required: true }]}>
<Input />
</Form.Item>
</Col>
<Col span={24}>
<Form.Item name="city" label="城市" rules={[{ required: true }]}>
<Input />
</Form.Item>
</Col>
<Col span={24}>
<Form.Item name="nation" label="国家/地区" rules={[{ required: true }]}>
<Input />
</Form.Item>
2025-07-15 15:10:21 +08:00
</Col> */}
2025-06-27 10:41:33 +08:00
<Col span={24}>
<Form.Item name="currency" label="币种" rules={[{ required: true }]}>
2025-07-15 17:04:54 +08:00
<Select placeholder="请选择币种">
{currency.map((item) => (
<Select.Option key={item.code} value={item.code}>
{item.dicName}
</Select.Option>
))}
</Select>
2025-06-27 10:41:33 +08:00
</Form.Item>
</Col>
2025-07-02 16:18:03 +08:00
<Form.Item name="id" noStyle>
<Input type="hidden" />
</Form.Item>
2025-06-27 10:41:33 +08:00
</Row>
</Form>
)}
</Modal>
);
};
export default InvoiceFormModal;