import { useEffect, useRef, useState } from 'react'; import { Drawer, Form, Input, InputNumber, Select, Button, Space, Row, Col, Tag, Card, App, } from 'antd'; import { useTranslation } from 'react-i18next'; import { getCodeRule, createCodeRule, updateCodeRule } from '@/api/mdm/coding-system'; import type { CodeRuleVO, CodeRuleSaveParams } from '@/types/mdm/coding-system'; import { CODING_SCOPE_OPTIONS } from '@/types/mdm/coding-system'; interface CodeRuleDrawerProps { open: boolean; id: number | null; onSuccess: () => void; onClose: () => void; } const VARIABLE_TAGS = [ '{PREFIX}', '{YYYY}', '{YY}', '{MM}', '{DD}', '{长度:N}', '{合金牌号}', '{状态代号}', '{厚度}', '{宽度}', '{用途}', '{基地}', '{车间}', '{Region}', '{合金系数字}', '{业务码}', ]; const CodeRuleDrawer: React.FC = ({ open, id, onSuccess, onClose }) => { const { t } = useTranslation('mdm/coding-system'); const { t: tc } = useTranslation('common'); const { message } = App.useApp(); const [form] = Form.useForm(); const [confirmLoading, setConfirmLoading] = useState(false); const isEdit = id != null; const originData = useRef>({}); useEffect(() => { if (!open) return; form.resetFields(); originData.current = {}; if (id != null) { getCodeRule(id).then(({ data }) => { originData.current = { ...data }; form.setFieldsValue(data); }); } else { form.setFieldsValue({ separator: '-', resetCycle: '永不', maxSeq: 999999, length: 20 }); } }, [open, id, form]); const handleOk = async () => { let values: CodeRuleSaveParams; try { values = await form.validateFields(); } catch { return; } setConfirmLoading(true); try { const params = { ...originData.current, ...values, id: id ?? undefined, } as CodeRuleSaveParams; if (isEdit) { await updateCodeRule(params); message.success(tc('success')); } else { await createCodeRule(params); message.success(tc('saveSuccess')); } onSuccess(); } catch (err) { message.error((err as Error).message || tc('fail')); } finally { setConfirmLoading(false); } }; const insertVariable = (v: string) => { const current = form.getFieldValue('pattern') || ''; form.setFieldsValue({ pattern: current + v }); }; return ( } >
{VARIABLE_TAGS.map((v) => ( insertVariable(v)} > {v} ))}