import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { OrganizationsTypeEnum } 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.Company.Organizations.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      wrapperCol={{ span: 24 }}
      width="500px"
      trigger={<MyButtons.Edit title={`添加下级`} size="small" />}
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values: any) =>
        Apis.Company.Organizations.Store({
          ...values,
          companies_id: values?.companies_id || props?.item?.companies_id,
          parent_id: props.item?.id,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        MyFormItems.EnumRadio({
          key: 'type',
          title: '组织类型',
          tooltip: '可根据自身情况自定义设置',
          colProps: { span: 24 },
          valueEnum: OrganizationsTypeEnum,
          required: true,
        }),
        {
          key: 'name',
          title: '组织名称',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
      ]}
    />
  );
}
