import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Contract.ContractTypes.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      layout="horizontal"
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 18 }}
      labelAlign="left"
      width="500px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      trigger={<MyButtons.Create title={`${props.title}`} />}
      onFinish={async (values) =>
        Apis.Contract.ContractTypes.Store({
          ...values,
          is_enabled: 1,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'name',
          title: '类型名称',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 24 },
        },
      ]}
    />
  );
}
