import AsyncValidator from 'async-validator'

AsyncValidator.validators.phoneAndTel= (rule, value, callback, source, options) => {
  var ruleType = rule.type
  var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field))
  let err = []

  if (validate) {
    if (!value && !rule.required) {
      return callback()
    } else if (rule.required && !value) {
      err.push('手机号不能为空')
      return callback(err)
    }
  }

  if (value && !value.includes(',')) {
    const telReg = /^([1]\d{10}|([\(（]?0[0-9]{2,3}[）\)]?[-]?)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?)$/
    const reg = /^(0|86|17951)?(13[0-9]|19[0-9]|15[012356789]|166|17[0-9]|18[0-9]|14[57])[0-9]{8}$/
    if (!reg.test(value)&&!telReg.test(value)) {
      err.push('请输入有效手机号或电话号码')
      return callback(err)
    }
  }

  callback()
}
AsyncValidator.validators.phone = (rule, value, callback, source, options) => {
  var ruleType = rule.type
  var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field))
  let err = []

  if (validate) {
    if (!value && !rule.required) {
      return callback()
    } else if (rule.required && !value) {
      err.push('手机号不能为空')
      return callback(err)
    }
  }

  if (value && !value.includes(',')) {
    const reg = /^(0|86|17951)?(13[0-9]|19[0-9]|15[012356789]|166|17[0-9]|18[0-9]|14[57])[0-9]{8}$/
    if (!reg.test(value)) {
      err.push('请输入有效的手机号')
      return callback(err)
    }
  }

  callback()
}

AsyncValidator.messages.types.email = '%s 格式不正确'
console.dir(AsyncValidator)
AsyncValidator.validators.idCard = (rule, value, callback, source, options) => {
  var ruleType = rule.type
  var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field))
  let err = []
  if (validate) {
    if (!value && !rule.required) {
      return callback()
    } else if (rule.required && !value) {
      err.push('身份证不能为空！')
    }
  }
  if (value) {
    const reg = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/
    if (!reg.test(value)) {
      err.push('身份证格式不正确！')
    }
  }
  callback(err)
}

AsyncValidator.validators.tel = (rule, value, callback, source, options) => {
  var ruleType = rule.type
  var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field))
  let err = []
  if (validate) {
    if (!value && !rule.required) {
      return callback()
    } else if (rule.required && !value) {
      err.push('电话号码不能为空！')
    }
  }
  if (value) {
    const reg = /^([1]\d{10}|([\(（]?0[0-9]{2,3}[）\)]?[-]?)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?)$/
    if (!reg.test(value)) {
      err.push('电话号码格式不正确！')
    }
  }
  callback(err)
}

AsyncValidator.validators.plateNumber = (rule, value, callback, source, options) => {

  var ruleType = rule.type
  var validate = rule.required || (!rule.required && source.hasOwnProperty(rule.field))
  let err = []

  if (validate) {
    if (!value && !rule.required) {
      return callback()
    } else if (rule.required && !value) {
      err.push('车牌号不能为空')
      return callback(err)
    }
  }

  if (value && !value.includes(',')) {
    const reg = /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/
    if (!reg.test(value)) {
      err.push('请输入有效车牌号')
      return callback(err)
    }
  }
  callback()
}
