import { ProFormColumnsType } from '@ant-design/pro-components';
function formConfig(
  enumList?: { [key: string]: any },
  getWechatMchAll?: any[],
  pageList?: any[]
): ProFormColumnsType<{
  name: string;
  state: string;
}>[] {
  return [
    {
      title: '小程序appId',
      dataIndex: 'wechatAppId',
      formItemProps: {
        style: { marginBottom: 10 },
        rules: [
          {
            required: true,
            message: '此项为必填项',
          },
        ],
      },
    },
    {
      title: 'appSecret',
      dataIndex: 'appSecret',
      formItemProps: {
        style: { marginBottom: 10 },
        rules: [
          {
            required: true,
            message: '此项为必填项',
          },
        ],
      },
      fieldProps: {
        placeholder: '请输入小程序秘钥'
      },
    },
    {
      title: 'appToken',
      dataIndex: 'appToken',
      formItemProps: {
        style: { marginBottom: 10 },
        rules: [
          {
            required: true,
            message: '此项为必填项',
          },
        ],
      },
      fieldProps: {
        placeholder: '微信小程序消息服务器配置的token'
      },
    },
    {
      title: 'appAesKey',
      dataIndex: 'appAesKey',
      formItemProps: {
        style: { marginBottom: 10 },
        rules: [
          {
            required: true,
            message: '此项为必填项',
          },
        ],
      },
      fieldProps: {
        placeholder: '微信小程序消息服务器配置的EncodingAESKey'
      },
    },
    {
      title: '商户',
      dataIndex: 'mchId',
      valueType: 'select',
      fieldProps: { showSearch: true, placeholder: '请选择商户' },
      formItemProps: {
        style: { marginBottom: 10 },
        rules: [
          {
            required: true,
            message: '此项为必填项',
          },
        ],
      },
      valueEnum: () => {
        let arr = getWechatMchAll;
        return arr ? new Map([...arr]?.map(({ id, mchName }: any) => [id, mchName])) : {};
      },
    },
    {
      title: '小程序名称',
      dataIndex: 'appName',
      formItemProps: {
        style: { marginBottom: 10 },
        rules: [
          {
            required: true,
            message: '此项为必填项',
          },
        ],
      },
    },
    {
      title: '小程序类型',
      dataIndex: 'appCategory',
      valueType: 'radio',
      formItemProps: {
        initialValue: 2,
        style: { marginBottom: 10 },
        rules: [
          {
            required: true,
            message: '此项为必填项',
          },
        ],
      },
      valueEnum: () => {
        let arr = enumList?.APP_CATEGORY?.values;
        return arr
          ? new Map([...arr]?.map(({ value, description }: any) => [value, description]))
          : {};
      },
    },
    {
      title: '小程序页面模板',
      dataIndex: 'templateName',
      fieldProps: { showSearch: true, placeholder: '请选择小程序页面模板' },
      valueType: 'select',
      formItemProps: {
        style: { marginBottom: 10 },
        rules: [
          {
            required: true,
            message: '此项为必填项',
          },
        ],
      },
      valueEnum: () => {
        let arr = pageList || [];
        return arr ? new Map([...arr]?.map(({ templateName }: any) => [templateName, templateName])) : {};
      }
    },
    {
      valueType: 'dependency',
      name: ['templateName'],
      columns: ({ templateName }) => {
        return [
          {
            title: '小程序首页链接',
            dataIndex: 'homePage',
            hideInForm: !templateName,
            formItemProps: {
              style: { marginBottom: 10 },
              rules: [
                {
                  required: true,
                  message: '此项为必填项',
                },
              ],
            },
            valueEnum: () => {
              let arr = pageList?.find(item => item.templateName === templateName)?.appPageVOList || [];
              return arr ? new Map([...arr]?.map(({ pagePath, pageName }: any) => [pagePath, pageName])) : {};
            }
          },
        ]
      },
    },
    {
      title: '小程序版本号',
      tooltip: "线上版本号",
      dataIndex: 'appVersion',
      formItemProps: {
        style: { marginBottom: 10 },
      },
    },
    {
      valueType: 'dependency',
      name: ['appVersion'],
      columns: ({ appVersion }) => {
        return [
          {
            title: 'ios支付版本号',
            tooltip: "ios的特殊性,此版本号与IOS支付模块关联,小程序版本号<=此版本号IOS才会出现支付功能,(微信)小程序发布线上后必须设置",
            dataIndex: 'iosPayment',
            formItemProps: {
              style: { marginBottom: 10 },
              rules: [
                {
                  validator: (_, value) => {
                    if (!value || !appVersion) {
                      return Promise.resolve();
                    }

                    if (value > appVersion) {
                      return Promise.reject(new Error('支付版本号不能大于小程序版本号'));
                    }
                    return Promise.resolve();
                  },
                },
              ],
            },
          },
        ]
      }
    },
    {
      dataIndex: 'configParamList',
      valueType: 'formList',
      colProps: {
        offset: 6,
      },
      fieldProps: {
        creatorButtonProps: {
          creatorButtonText: '新增小程序配置',
        },
      },
      formItemProps: {
        style: { marginBottom: 10 },
      },
      columns: [
        {
          valueType: 'dependency',
          name: ['configParamList'],
          formItemProps: {
            style: { marginBottom: 10 },
          },
          columns: ({ configParamList }) => {
            return [
              {
                valueType: 'group',
                columns: [
                  {
                    formItemProps: {
                      style: { marginBottom: 10 },
                    },
                  },
                  {
                    formItemProps: {
                      style: { marginBottom: 10 },
                    },
                  },
                ],
              },
            ];
          },
        },
      ],
    },
    {
      title: '备注',
      dataIndex: 'remark',
    },
  ];
}

export default formConfig;
