import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { CompanyAppsAppTypeEnum, CompanyAppsModuleEnum } 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.CompanyApps.Store>
      {...MyModalFormProps.props}
      title={`添加应用配置`}
      wrapperCol={{ span: 24 }}
      width="500px"
      trigger={<MyButtons.Create title={`添加应用`} />}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values) =>
        Apis.Company.CompanyApps.Store({
          ...values,
          companies_id: props?.item?.id,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        MyFormItems.EnumRadio({
          key: 'app_type',
          title: '应用类型',
          colProps: { span: 24 },
          valueEnum: CompanyAppsAppTypeEnum,
          required: true,
        }),

        {
          name: ['app_type'],
          valueType: 'dependency',
          columns: ({ app_type }: any) => {
            return app_type === 'MiniProgram'
              ? [
                  MyFormItems.EnumRadio({
                    key: 'module',
                    title: '模块',
                    colProps: { span: 24 },
                    valueEnum: CompanyAppsModuleEnum,
                  }),
                ]
              : [];
          },
        },

        {
          key: 'app_id',
          title: '应用ID',
          tooltip: '微信AppID/企业微信CorpID',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'app_secret',
          title: '应用密钥',
          valueType: 'textarea',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        // {
        //   key: 'is_default',
        //   title: '是否设为默认账号',
        //   valueType: 'switch',
        //   colProps: { span: 24 },
        // },
      ]}
    />
  );
}
