import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { TreeSelects } from '@/components/TreeSelect';
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.Emergency.EmergencyEventCategories.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      wrapperCol={{ span: 24 }}
      width="500px"
      key={new Date().getTime()}
      form={form}
      trigger={<MyButtons.Create title={`添加${props.title}`} />}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields();
        }
      }}
      onFinish={async (values) =>
        Apis.Emergency.EmergencyEventCategories.Store({
          ...values,
          is_enabled: values.is_enabled ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        TreeSelects.TreeSelectEventCategories(),
        {
          key: 'name',
          title: '名称',
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'description',
          title: '分类描述',
          valueType: 'textarea',
        },
        {
          title: '是否启用',
          key: 'is_enabled',
          valueType: 'switch',
        },
      ]}
    />
  );
}
