import { listDictModel } from '/@/api/common';
import { sysTenantPackageQueryPage } from '/@/api/sys/sysTenantPackageApi';
import { validatorUsername } from '/@/api/sys/sysUserApi';
import { DescItem } from '/@/components/Description';
import { BasicColumn, FormSchema } from '/@/components/Table';
import { validateUserName } from '/@/utils/validate';

export const columns: BasicColumn[] = [
  {
    title: '名称',
    dataIndex: 'name',
  },
  {
    title: '类型',
    dataIndex: 'type',
  },
  {
    title: '联系人',
    dataIndex: 'contractUser',
  },
  {
    title: '联系人电话',
    dataIndex: 'contactMobile',
  },
  {
    title: '管理账号',
    dataIndex: 'username',
  },
  {
    title: '套餐名称',
    dataIndex: 'packageName',
  },
  {
    title: '状态',
    dataIndex: 'disable',
  },
];

// 表单列定义
export const searchFormSchema: FormSchema[] = [
  {
    label: '名称',
    field: 'name',
    component: 'Input',
    componentProps: {
      placeholder: '请输入名称',
    },
  },
  // {
  //   label: '类型',
  //   field: 'type',
  //   component: 'ApiSelect',
  //   componentProps: {
  //     api: listDictModel,
  //     params: {
  //       dictCode: 'sys_create_type',
  //     },
  //   },
  // },
  {
    label: '状态',
    field: 'disable',
    component: 'ApiSelect',
    componentProps: {
      api: listDictModel,
      params: {
        dictCode: 'sys_disable_type',
      },
    },
  },
];
// 表单新增编辑
export const dataFormSchema: FormSchema[] = [
  {
    label: '名称',
    field: 'name',
    required: true,
    component: 'Input',
    componentProps: {
      placeholder: '请输入名称',
    },
  },
  // 类型是否可以修改待定
  // {
  //   label: '类型',
  //   field: 'type',
  //   component: 'ApiRadioGroup',
  //   componentProps: {
  //     api: listDictModel,
  //     params: {
  //       dictCode: 'sys_create_type',
  //     },
  //   },
  // },
  {
    label: '联系人',
    field: 'contractUser',
    required: true,
    component: 'Input',
    componentProps: {
      placeholder: '请输入联系人',
    },
  },
  {
    label: '联系人电话',
    field: 'contactMobile',
    required: true,
    component: 'Input',
    componentProps: {
      placeholder: '请输入联系人电话',
    },
  },
  {
    label: '管理账号',
    field: 'username',
    component: 'Input',
    componentProps: {
      placeholder: '请输入管理账号',
      disabled: false,
    },
    dynamicRules: () => {
      return [
        {
          required: true,
          validator: async (_, value) => {
            if (!value) {
              return Promise.reject('管理账号不能为空');
            }
            if (validateUserName(value)) {
              return Promise.reject('管理账号为4-16为字母或数字组成');
            }
            await validatorUsername({ username: value }).then(res => {
              if (res != 1) {
                return Promise.reject('管理账号不可用，请重新输入！');
              }
            });
            return Promise.resolve();
          },
        },
      ];
    },
  },
  {
    label: '套餐',
    field: 'packageId',
    required: true,
    component: 'ApiSelect',
    componentProps: () => {
      return {
        placeholder: '请选择套餐',
        api: sysTenantPackageQueryPage,
        params: {
          pageNum: 1,
          pageSize: 999,
          disable: '0',
        },
        mode: 'single',
        labelField: 'packageName',
        valueField: 'id',
        resultField: 'data',
      };
    },
  },
  {
    label: '状态',
    field: 'disable',
    component: 'ApiRadioGroup',
    componentProps: {
      api: listDictModel,
      params: {
        dictCode: 'sys_disable_type',
      },
    },
    defaultValue: '0',
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入备注',
    },
  },
];
// 表单详情查看
export const viewSchema: DescItem[] = [
  {
    label: '名称',
    field: 'name',
  },
  {
    label: '类型',
    field: 'type',
  },
  {
    label: '联系人',
    field: 'contractUser',
  },
  {
    label: '联系人电话',
    field: 'contactMobile',
  },
  {
    label: '管理账号',
    field: 'username',
  },
  {
    label: '套餐',
    field: 'packageName',
  },
  {
    label: '状态',
    field: 'disable',
  },
  {
    label: '备注',
    field: 'remark',
  },
];
