import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
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.Company.CompanyPropertyBrands.Store>
      {...MyModalFormProps.props}
      title={`添加品牌`}
      wrapperCol={{ span: 24 }}
      width="360px"
      trigger={<MyButtons.Create title={`添加品牌`} />}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values) =>
        Apis.Company.CompanyPropertyBrands.Store({
          ...values,
          companies_id: values?.companies_id || props?.item?.id,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        ...(props?.item?.id
          ? []
          : [
              Selects?.Companies({
                key: 'companies_id',
                title: '公司',
                colProps: { span: 24 },
                required: true,
              }),
            ]),
        {
          key: 'name',
          title: '品牌名称',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.UploadImages({
          key: 'logo',
          title: '品牌logo【 尺寸比例 1:1 】',
          tooltip: '只能上传1张图片',
          max: 1,
          colProps: { span: 24 },
          formItemProps: { required: false },
        }),
      ]}
    />
  );
}
