import { isAccountExist, IsPhoneExist } from '/@/api/system/system';
import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { emailRule, ChineseRegexp, EmailRegexp, phoneRegexp } from '/@/utils/rules';

let olderPhoneNumber;

export const columns: BasicColumn[] = [
  {
    title: '登录名',
    dataIndex: 'username',
    width: 120,
  },
  {
    title: '姓名',
    dataIndex: 'realName',
    width: 120,
  },
  {
    title: '手机号码',
    dataIndex: 'phoneNumber',
    width: 120,
  },
  {
    title: '邮箱',
    dataIndex: 'email',
    width: 120,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
  },
  {
    title: '状态',
    dataIndex: 'userStatusEnum',
    width: 120,
    slots: { customRender: 'status' },
  },
  {
    title: '锁定状态',
    dataIndex: 'locked',
    width: 120,
    slots: { customRender: 'statusT' },
  },
];

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: 'username',
    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 == ''||value == undefined) {
                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: 'password',
    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: 'realName',
    label: '姓名',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
    componentProps: {
      maxLength: 10,
    },
  },
  {
    label: '角色',
    field: 'roleIds',
    component: 'Select',
    colProps: { span: 12 },
    slot: 'roleSlot',
    rules: [
      {
        required: true,
        message: '请选择角色',
        type: 'array',
      },
    ],
  },
  {
    label: '手机号',
    field: 'phoneNumber',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
   
    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) {
                  // 此处可以用防抖函数优化性能
                  let data ={
                    phoneNumber:value,
                    userId: values.id?values.id:''
                  }
                  IsPhoneExist(data).then(( res ) => {
                  if (res.data != null) {
                    reject('手机号已存在');
                  } else {
                    resolve();
                  }
                 

                  
                  });
                } else {
                  resolve();
                }
              }
            });
          },
        },
      ];
    },
    componentProps({ formActionType }) {
      const { clearValidate } = formActionType;
      return {
        onChange(value) {
          if (value == olderPhoneNumber) {
            clearValidate('phoneNumber');
          }
        },
      };
    },
  },
  {
    label: '邮箱',
    field: 'email',
    component: 'Input',
    colProps: { span: 12 },
    rules: emailRule,
  },
  {
    field: 'accountExpireTime',
    label: '有效期',
    component: 'DatePicker',
    colProps: { span: 12 },
    componentProps: {
      showTime: true,
      format: 'YYYY-MM-DD HH:mm:ss',
    },
  },
  {
    field: 'enabled',
    label: '状态',
    component: 'RadioButtonGroup',
    colProps: { span: 12 },
    defaultValue: true,
    componentProps: {
      options: [
        { label: '启用', value: true },
        { label: '禁用', value: false },
      ],
    },
  },
  {
    field: 'remark',
    label: '备注',
    component: 'InputTextArea',
    colProps: { span: 24 },
    componentProps: {
      maxLength: 255,
      placeholder: '请输入备注',
    },
  },
  {
    field: 'organizationIds',
    label: '所属组织',
    component: 'Input',
    slot: 'organizationId',
    required: true,
  },
];
