// isAccountExist
import { roleSelectListApi, checkUserApi } from '/@/api/system/system';
import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { h } from 'vue';
// import { Switch } from 'ant-design-vue';
import { Time } from '/@/components/Time';
import { isBoolean } from '/@/utils/is';

export const columns: BasicColumn[] = [
  {
    title: '用户名',
    dataIndex: 'userName',
    width: 120,
  },
  {
    title: '昵称',
    dataIndex: 'nickName',
    width: 120,
  },
  {
    title: '手机',
    dataIndex: 'phone',
    width: 140,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
    customRender: ({ record }) => {
      return h(Time, {
        value: record.createTime,
        mode: 'datetime',
      });
    },
  },
  {
    title: '角色',
    dataIndex: 'roleName',
    width: 200,
  },
  {
    title: '备注',
    dataIndex: 'remark',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'userName',
    label: '用户名',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'nickName',
    label: '昵称',
    component: 'Input',
    colProps: { span: 8 },
  },
];

export const accountFormSchema: FormSchema[] = [
  {
    field: 'userName',
    label: '用户名',
    component: 'Input',
    // helpMessage: ['本字段演示异步验证', '不能输入带有admin的用户名'],
    // rules: [
    //   {
    //     required: true,
    //     message: '请输入用户名',
    //   },
    //   {
    //     validator(_, value) {
    //       return new Promise((resolve, reject) => {
    //         isAccountExist(value)
    //           .then(() => resolve())
    //           .catch((err) => {
    //             reject(err.message || '验证失败');
    //           });
    //       });
    //     },
    //   },
    // ],
  },
  // {
  //   field: 'pwd',
  //   label: '密码',
  //   component: 'InputPassword',
  //   required: true,
  //   ifShow: false,
  // },
  {
    label: '角色',
    field: 'roleId',
    component: 'ApiSelect',
    componentProps: {
      api: roleSelectListApi,
      labelField: 'roleName',
      valueField: 'roleId',
    },
    required: false,
  },
  // {
  //   field: 'dept',
  //   label: '所属部门',
  //   component: 'TreeSelect',
  //   componentProps: {
  //     fieldNames: {
  //       label: 'deptName',
  //       key: 'id',
  //       value: 'id',
  //     },
  //     getPopupContainer: () => document.body,
  //   },
  //   required: true,
  // },

  {
    field: 'nickName',
    label: '昵称',
    component: 'Input',
    required: true,
  },
  {
    field: 'phone',
    label: '手机号',
    component: 'Input',
    required: false,
    helpMessage: ['验证提示', '手机号需在指房宝APP注册后才可新增'],
    itemProps: {
      validateTrigger: 'blur',
    },
    rules: [
      {
        required: false,
        message: '请输入手机号',
      },
      {
        validator(_, value) {
          return new Promise(async (resolve, reject) => {
            try {
              const res = await checkUserApi({
                phone: value || '',
              });

              isBoolean(res) && res ? resolve() : reject('用户不存在');
            } catch (error) {
              reject(error.message || '用户不存在');
            }
          });
        },
      },
    ],
  },

  // {
  //   label: '邮箱',
  //   field: 'email',
  //   component: 'Input',
  //   required: true,
  // },

  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
  },
];
