import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
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"
      key={new Date().getTime()}
      trigger={<MyButtons.Create title={`添加应用`} />}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values) =>
        Apis.Company.CompanyApps.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,
              }),
            ]),
        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 !== 'MpOfficial'
              ? [
                  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 },
        // },
      ]}
    />
  );
}
