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 Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Company.CompanyApps.Update>
      {...MyModalFormProps.props}
      title={`编辑应用配置`}
      trigger={<MyButtons.Edit />}
      wrapperCol={{ span: 24 }}
      width="500px"
      form={form}
      key={new Date().getTime()}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      onFinish={async (values) =>
        Apis.Company.CompanyApps.Update({
          ...values,
          id: props?.item?.id,
          companies_id: props?.item?.companies_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 },
        },
      ]}
    />
  );
}
