/**
 * 判断是否为外部资源
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}
export function validateCarnumber(rule, value, callback) {
  const ptreg = /^[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘桂琼川贵云渝藏陕甘青宁新粤]{1}[ABCDEFGHJKLMNOPQRSTUVWXY]{1}[0-9A-Z]{5}$/u;
  const xnyreg = /^[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘桂琼川贵云渝藏陕甘青宁新粤]{1}[A-Z]{1}(([0-9]{5}[DABCEFGHJK]$)|([DABCEFGHJK][A-HJ-NP-Z0-9][0-9]{4}$))/; // 2021年新能源车牌不止有DF
  if (!value) {
    callback(new Error('请选择车牌号'))
  } else {
    if (value.length === 7) {
      if (!ptreg.test(value)) {
        callback(new Error('请输入正确的普通车牌号'))
      } else {
        callback()
      }
    } else if (value.length === 8) {
      if (!xnyreg.test(value)) {
        callback(new Error('请输入正确的新能源车牌号'))
      } else {
        callback()
      }
    } else {
      callback(new Error('车牌号错误'))
    }
  }
}

// 表单重置
export function resetForm(refName) {
  if (this.$refs[refName]) {
    this.$refs[refName].resetFields();
  }
}

//当type=1时获取出生日期,type=2时获取性别,type=3时获取年龄
export function IdCard(IdCard, type) {
  if (type === 1) {
      //获取出生日期
      let birthday = IdCard.substring(6, 10) + "-" + IdCard.substring(10, 12) + "-" + IdCard.substring(12, 14)
      return birthday
  }
  if (type === 2) {
      //获取性别
      if (parseInt(IdCard.substr(16, 1)) % 2 === 1) {
          return "男"
      } else {
          return "女"
      }
  }
  if (type === 3) {
      //获取年龄
      var ageDate = new Date()
      var month = ageDate.getMonth() + 1
      var day = ageDate.getDate()
      var age = ageDate.getFullYear() - IdCard.substring(6, 10) - 1
      if (IdCard.substring(10, 12) < month || IdCard.substring(10, 12) === month && IdCard.substring(12, 14) <= day) {
          age++
      }
      if (age <= 0) {
          age = 1
      }
      return age
  }
}

