// 验证手机号码/座机
export function checkPhoneNumber(rule, value, callback) {
  var mobile = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
  let flag = false
  if (!value) {
    return callback(new Error('请填写联系电话！'))
  } else {
    if (value.indexOf('1') === 0) { // 手机号码
      flag = mobile.test(value)
    } else { // 座机电话
      var landline = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/
      flag = landline.test(value)
    }
    if (!flag) {
      return callback(new Error('请填写正确的联系电话！'))
    } else {
      callback()
    }
  }
}

export function checkIdNum(rule, value, callback) {
  const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  if (!value) {
    return callback(new Error('证件号码不能为空'))
  } else if (!reg.test(value)) {
    return callback(new Error('证件号码不正确'))
  } else {
    callback()
  }
}

export const uniSCID = {
  validator: (rule, value, callback) => {
    if (!value) {
      callback()
      return
    }
    const patrn = /^[0-9A-Z]+$/
    // 18位校验及大写校验
    if (value.length !== 18 || patrn.test(value) === false) {
      callback('统一社会信用代码格式不正确')
      return false
    } else {
      let total = 0
      const weightedfactors = [
        1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28
      ] // 加权因子
      const str = '0123456789ABCDEFGHJKLMNPQRTUWXY'
      // 不用I、O、S、V、Z
      for (let i = 0; i < value.length - 1; i++) {
        total = total + str.indexOf(value.substring(i, i + 1)) * weightedfactors[i]
        // 权重与加权因子相乘之和
      }
      let logiccheckcode = 31 - (total % 31)
      if (logiccheckcode === 31) {
        logiccheckcode = 0
      }
      logiccheckcode = str[logiccheckcode]

      const checkcode = value.substring(17, 18)
      if (logiccheckcode !== checkcode) {
        callback('统一社会信用代码格式不正确')
        return false
      }
      callback()
      return true
    }
  },
  trigger: 'blur'
}
