import { appointment, appointPos, roleList } from '/@/api/sys/user';
import { isAccountExist, IsPhoneExist } from '/@/api/system/system';
import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { copyTransFun } from '/@/utils/fnUtils';
import { emailRule, ChineseRegexp, EmailRegexp, phoneRegexp } from '/@/utils/rules';

let olderPhoneNumber;

export const columns: BasicColumn[] = [
  {
    title: '用户名',
    dataIndex: 'NAME',
    width: 120,
  },
  {
    title: '登录姓名',
    dataIndex: 'LOGIN_NAME',
    width: 120,
  },
  {
    title: '用户ID',
    dataIndex: 'EMPLOYEE_ID',
    width: 200,
  },
  {
    title: '性别',
    dataIndex: 'SEX',
    width: 80,
    slots: { customRender: 'SEX' },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'username',
    label: '用户名',
    component: 'Input',
    colProps: { span: 8 },
    componentProps: {
      maxLength: 255,
      placeholder: '请输入用户名',
    },
  },
  {
    field: 'realName',
    label: '姓名',
    component: 'Input',
    colProps: { span: 8 },
    componentProps: {
      maxLength: 255,
      placeholder: '请输入姓名',
    },
  },
];

export const accountFormSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    component: 'Input',
    show: false,
    componentProps: {
      maxLength: 36,
    },
  },
  {
    field: 'loginName',
    label: '平台登录名',
    component: 'Input',
    colProps: { span: 12 },
    dynamicDisabled: false,
    componentProps: {
      maxLength: 36,
      placeholder: '请输入用户名',
    },
    dynamicRules: ({ values }) => {
      return [
        {
          required: true,
          validator(_, value) {
            return new Promise((resolve, reject) => {
              if (value == '') {
                reject('请输入用户名');
              } else if (ChineseRegexp.test(value)) {
                reject('用户名不能含有中文');
              } else if (EmailRegexp.test(value)) {
                reject('用户名不能为电子邮箱格式');
              } else {
                if (values.username != undefined && values.id == undefined) {
                  isAccountExist(value).then(({ data }) => {
                    if (data != null) {
                      reject('用户名已存在');
                    } else {
                      resolve();
                    }
                  });
                } else {
                  resolve();
                }
              }
            });
          },
        },
      ];
    },
  },
  {
    field: 'userName',
    label: '姓名',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
    componentProps: {
      maxLength: 10,
    },
  },
  {
    label: '手机号',
    field: 'mobile',
    component: 'Input',
    colProps: { span: 12 },
    dynamicRules: ({ values }) => {
      return [
        {
          required: true,
          validator(_, value) {
            return new Promise((resolve, reject) => {
              if (value == '') {
                reject('请输入手机号');
              } else if (!phoneRegexp.test(value)) {
                reject('请输入正确的手机号');
              } else {
                if (values.phoneNumber != undefined) {
                  // 此处可以用防抖函数优化性能
                  IsPhoneExist(value).then(({ data }) => {
                    if (data != null) {
                      reject('手机号已存在');
                    } else {
                      resolve();
                    }
                  });
                } else {
                  resolve();
                }
              }
            });
          },
        },
      ];
    },
    componentProps({ formActionType }) {
      const { clearValidate } = formActionType;
      return {
        onChange(value) {
          if (value == olderPhoneNumber) {
            clearValidate('phoneNumber');
          }
        },
      };
    },
  },
  {
    field: 'pwd',
    label: '密码',
    component: 'InputPassword',
    required: true,
    colProps: { span: 12 },
    rules: [
      { required: true, message: '密码不能为空' },
      { pattern: /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*\W)[\S]{8,30}$/, message: '密码中必须包含大小写 字母、数字、特殊字符，至少8个字符，最多30个字符' }

    ]
  },
  {
    field: 'sex',
    label: '性别',
    component: 'RadioButtonGroup',
    required: true,
    componentProps: {
      options: [
        { label: "男", value: '1' },
        { label: "女", value: '0' },
      ],
    },
  },
  {
    field: 'sszw',
    component: 'ApiTreeSelect',
    label: '所属职位',
    componentProps: {
      placeholder: '请选择职位',
      api: async () => {
        var data = await appointment();
        copyTransFun(data as any as any[]);
        return data;
      },
      onChange(value) {
        sszw.value = value;
      },
    },
  },
  {
    field: 'ssgw',
    label: '所属岗位',
    colProps: { span: 24 },
    component: 'ApiTreeSelect',
    componentProps: ({ formModel }) => {
      const selectValue = formModel.sszw;
      return {
        params: selectValue,
        api: async (params) => {
          return params ? await appointPos(params) : [];
        },
        onChange(value) {
          ssgw.value = value;
        },
      }
    }
  },
  // {
  //   field: 'userjs',
  //   component: 'ApiTreeSelect',
  //   label: '角色列表',
  //   componentProps: {
  //     placeholder: '请选择角色',
  //     api: async () => {
  //       var data = await roleList();
  //       return data;
  //     },
  //     onChange(value) {
  //       userjs.value = value;
  //     },
  //   },
  // },

  {
    label: '角色',
    field: 'userjs',
    component: 'Select',
    colProps: { span: 24 },
    slot: 'roleSlot',
    rules: [
      {
        required: true,
        message: '请选择角色',
        type: 'array',
      },
    ],
  },
];
