import { BasicColumn, FormSchema } from '/@/components/Table';
import { sysRoleQueryPage } from '/@/api/sys/sysRoleApi';
import { sysPortalQueryPage } from '/@/api/sys/sysPortalApi';
import { sysOrgQueryTree } from '/@/api/sys/sysOrgApi';
import { sysPosQueryPage } from '/@/api/sys/sysPosApi';
import { validatorUsername } from '/@/api/sys/sysUserApi';
import { listDictModel } from '/@/api/common';
import { validateUserName } from '/@/utils/validate';
export const columns: BasicColumn[] = [
  {
    title: '账号',
    dataIndex: 'username',
    width: 160,
  },
  {
    title: '昵称',
    dataIndex: 'nickname',
    width: 160,
  },
  {
    title: '性别',
    dataIndex: 'gender',
    width: 120,
  },
  {
    title: '手机号',
    dataIndex: 'phone',
    width: 140,
  },
  {
    title: '邮箱',
    dataIndex: 'email',
    width: 140,
  },
  {
    title: '用户状态',
    dataIndex: 'status',
    width: 100,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'username',
    label: '账号',
    component: 'Input',
    componentProps: {
      placeholder: '请输入账号',
      maxLength: 16,
    },
  },
  {
    field: 'nickname',
    label: '昵称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入昵称',
    },
  },
  {
    field: 'phone',
    label: '手机号',
    component: 'Input',
    componentProps: {
      placeholder: '请输入手机号',
    },
  },
];

export const dataFormSchema: FormSchema[] = [
  {
    field: 'username',
    label: '账号',
    component: 'Input',
    required: true,
    componentProps: {
      placeholder: '请输入账号',
    },
    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();
          },
        },
      ];
    },
  },
  {
    field: 'password',
    label: '密码',
    component: 'InputPassword',
    required: true,
    componentProps: {
      placeholder: '请输入密码',
    },
  },
  {
    field: 'nickname',
    label: '昵称',
    component: 'Input',
    required: true,
    componentProps: {
      placeholder: '请输入昵称',
    },
  },
  {
    field: 'gender',
    label: '性别',
    component: 'ApiRadioGroup',
    required: true,
    componentProps: {
      api: listDictModel,
      params: {
        dictCode: 'sys_sex',
      },
    },
    defaultValue: '1',
  },
  {
    field: 'phone',
    label: '手机号',
    component: 'Input',
    componentProps: {
      placeholder: '请输入手机号',
    },
  },
  {
    field: 'orgId',
    required: true,
    label: '组织',
    component: 'ApiTreeSelect',
    componentProps: {
      api: sysOrgQueryTree,
      placeholder: '请选择组织',
      resultField: 'data',
      fieldNames: {
        label: 'name',
        key: 'id',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'roleIds',
    required: true,
    label: '角色',
    component: 'ApiSelect',
    itemProps: {
      validateTrigger: 'blur',
    },
    componentProps: () => {
      return {
        placeholder: '请选择角色',
        api: sysRoleQueryPage,
        params: {
          pageNum: 1,
          pageSize: 999,
          disable: 0,
        },
        mode: 'multiple',
        labelField: 'name',
        valueField: 'id',
        resultField: 'data',
      };
    },
  },
  {
    field: 'postIds',
    label: '岗位',
    component: 'ApiSelect',
    itemProps: {
      validateTrigger: 'blur',
    },
    componentProps: () => {
      return {
        placeholder: '请选择岗位',
        api: sysPosQueryPage,
        params: {
          pageNum: 1,
          pageSize: 999,
          disable: 0,
        },
        mode: 'multiple',
        labelField: 'posName',
        valueField: 'id',
        resultField: 'data',
      };
    },
  },
  {
    field: 'portalIds',
    required: true,
    label: '门户',
    component: 'ApiSelect',
    itemProps: {
      validateTrigger: 'blur',
    },
    componentProps: () => {
      return {
        placeholder: '请选择门户',
        api: sysPortalQueryPage,
        params: {
          pageNum: 1,
          pageSize: 999,
        },
        mode: 'multiple',
        labelField: 'name',
        valueField: 'id',
        resultField: 'data',
      };
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'ApiRadioGroup',
    required: true,
    componentProps: {
      api: listDictModel,
      params: {
        dictCode: 'sys_user_status',
      },
    },
    defaultValue: '1',
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入备注',
    },
  },
];
