import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  ContractTemplatesIncomeExpenseTypeEnum,
  ContractTemplatesSourceEnum,
} from '@/gen/Enums';
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.ContractTemplates.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      layout="horizontal"
      labelCol={{ span: 5 }}
      wrapperCol={{ span: 19 }}
      labelAlign="left"
      width="600px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      trigger={<MyButtons.Create title={`添加${props.title}`} />}
      onFinish={async (values) =>
        Apis.Contract.ContractTemplates.Store({
          ...values,
          // is_enabled: values.is_enabled ? 1 : 0,
          is_enabled: 1,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        MyFormItems.EnumRadio({
          key: 'income_expense_type',
          title: '收支类型',
          valueEnum: ContractTemplatesIncomeExpenseTypeEnum,
          required: true,
          colProps: { span: 24 },
        }),
        Selects.ContractTypes({
          title: '合同类型',
          tooltip: '可在【配置：合同类型配置】中设置可选的合同类型',
          required: true,
        }),
        {
          key: 'name',
          title: '模板名称',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 24 },
        },
        {
          key: 'code',
          title: '模板编号',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 24 },
        },
        MyFormItems.EnumRadio({
          key: 'source',
          title: '模板来源',
          valueEnum: ContractTemplatesSourceEnum,
          required: true,
          colProps: { span: 24 },
        }),
        {
          title: '模板描述',
          key: 'description',
          valueType: 'textarea',
          colProps: { span: 24 },
        },
        MyFormItems.UploadImages({
          key: 'attachment',
          title: '上传附件',
          tooltip: '可以上传任意格式的文件',
          uploadType: 'file',
          // accept: '.docx,.doc,.pdf',
          colProps: { span: 24 },
          max: 9,
          required: true,
        }),
      ]}
    />
  );
}
