function isNull(data) {
  if (!data) return true
  if (JSON.stringify(data) === "{}") return true
  if (JSON.stringify(data) === "[]") return true
  return false
}

/* 1 . 手机校验 */
const validatePhone = (text, type) => {
  return (rule, value, callback) => {
    if (!value) {
      callback()
    } else {
      if (type == "01") {
        const reg = /^1[3-9]\d{9}$/
        if (reg.test(Number(value))) {
          callback()
        } else {
          return callback(new Error("请输入正确的" + text))
        }
      } else {
        //手机号或者座机号校验
        const phoneRegex = /^1[3-9]\d{9}$/
        const landlineRegex = /^(0\d{2,3})[- ]?\d{7,8}$/
        if (phoneRegex.test(Number(value)) || landlineRegex.test(value)) {
          callback()
        } else {
          return callback(new Error("请输入正确的" + text))
        }
      }
    }
  }
}
export const phoneRule = (rule, prop) => {
  if (isNull(rule)) return []
  const {
    require,
    validator,
    text = "手机号",
    type = "02"
  } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: "请输入" + text,
      trigger: "blur"
    })
  }
  if (validator) {
    result.push(
      // {
      //   min: 11,
      //   message: "手机号格式不正确",
      //   trigger: "blur"
      // },
      {
        trigger: "blur",
        validator: validatePhone(text, type)
      }
    )
  }
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}
//校验输入的是否是数字或字母
export const NoTexts = (rule, prop) => {
  if (isNull(rule)) return []
  const {
    require,
    validator
  } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: "请输入数字或字母",
      trigger: "blur"
    })
  }
  if (validator) {
    result.push({
      trigger: "blur",
      validator: validateNotext()
    })
  }
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}
const validateNotext = rule => {
  return (rule, value, callback) => {
    const regex = /^[a-zA-Z0-9]*$/
    if (!value) {
      callback()
    } else {
      if (!regex.test(value)) {
        callback(new Error("请输入数字或字母"))
      } else {
        callback()
      }
    }
  }
}
//校验输入的是否是数字或字母
export const NoText = (rule, prop) => {
  if (isNull(rule)) return []
  const {
    require,
    validator
  } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: "只能输入数字、大小写字母和符号",
      trigger: "blur"
    })
  }
  if (validator) {
    result.push({
      trigger: "blur",
      validator: validateZh()
    })
  }
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}
const validateZh = rule => {
  return (rule, value, callback) => {
    const regex = /^[a-zA-Z0-9!@#$%^&*(),.?":{}|<>]*$/
    if (!value) {
      callback()
    } else {
      if (!regex.test(value)) {
        callback(new Error("请输入数字、大小写字母和符号"))
      } else {
        callback()
      }
    }
  }
}
//校验是否为正数
const validateNumbers = rule => {
  return (rule, value, callback) => {
    if (!value) {
      callback()
    }
    if (typeof value == "number") {
      if (value < 0 || value > 99999999) {
        callback(new Error("请输入0到99999999的数"))
      } else {
        callback()
      }
    } else {
      if (value && value.startsWith("-")) {
        callback(new Error("请输入正数"))
      } else {
        callback()
      }
    }
  }
}
//输入正数
export const znumberRule = (rule, prop) => {

  if (isNull(rule)) return []
  const {
    require,
    validator
  } = rule && { ...rule }
  const result = []
  console.log(require)
  console.log(validator)
  if (require) {
    result.push({
      required: true,
      message: "请输入数字",
      trigger: "blur"
    })
  }
  if (validator) {
    result.push(
      // {
      //   message: "请输入正数",
      //   trigger: "blur"
      // },
      {
        trigger: "blur",
        validator: validateNumbers()
      }
    )
  }
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}
//输入规定范围内的正整数
export const numberzzsRule = (rule, prop) => {
  if (isNull(rule)) return []
  const {
    require,
    validator,
    iszs,
    num,
    minNum = 0
  } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: `请输${minNum}-${num}之间的数字`,
      trigger: "blur"
    })
  }
  if (validator) {
    result.push(
      // {
      //   message: "请输入正数",
      //   trigger: "blur"
      // },
      {
        trigger: "blur",
        validator: validatezzs(iszs, num, minNum)
      }
    )
  }
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}
const validatezzs = (iszs, num, minNum = 0) => {
  return (rule, value, callback) => {
    if (!value) {
      callback()
    } else if (iszs) {
      if (value < minNum || value > num) {
        callback(new Error(`请输入${minNum}到${num}之间的正数`))
      } else {
        callback()
      }
    } else if (value < 0 || value > num || !/^[0-9]\d*$/.test(value)) {
      callback(new Error(`请输入0到${num}之间的正整数`))
    } else {
      callback()
    }
  }
}

//输入规定范围内的9位整数4位小数{}
export const numbernineRule = (rule, prop) => {
  if (isNull(rule)) return []
  const {
    require,
    validator,
    iszs,
    firstNum = 9,
    message,
    lastNum = 4
  } = rule && { ...rule }
  // iszs:是否
  const result = []
  if (require) {
    result.push({
      required: true,
      message: message || `最多输入${firstNum}位整数，${lastNum}位小数`,
      trigger: "blur"
    })
  }
  if (validator) {
    result.push({
      trigger: "blur",
      validator: validatenine(iszs, firstNum, lastNum, message)
    })
  }
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}
const validatenine = (iszs, firstNum, lastNum, message) => {
  return (rule, value, callback) => {
    if (!value) {
      callback()
    } else if (iszs) {
      let _reg = new RegExp(`^\\d{1,${firstNum}}(\\.\\d{1,${lastNum}})?$`)
      if (value < 0 || !_reg.test(value)) {
        callback(new Error(message || `最多输入${firstNum}位整数，${lastNum}位小数`))
      } else {
        callback()
      }
    } else {
      let _reg = new RegExp(`^\\d{1,${firstNum}}(\\.\\d{1,${lastNum}})?$`)
      if (value > 0 && !_reg.test(value)) {
        callback(new Error(message || `最多输入${firstNum}位整数，${lastNum}位小数`))
      } else {
        callback()
      }
    }
  }
}
//输入0-100的正数
export const percentageRule = (rule, prop) => {
  if (isNull(rule)) return []
  const {
    require,
    validator
  } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: "请输0-100之间的数字",
      trigger: "blur"
    })
  }
  if (validator) {
    result.push(
      // {
      //   message: "请输入正数",
      //   trigger: "blur"
      // },
      {
        trigger: "blur",
        validator: validateBFZ()
      }
    )
  }
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}

//校验是否为百分制
const validateBFZ = rule => {
  return (rule, value, callback) => {
    if (!value) {
      callback()
    }
    if (typeof value == "number") {
      if (value && (value < 0 || value > 100)) {
        callback(new Error("请输0-100之间的数字"))
      } else {
        callback()
      }
    } else {
      if (value && (Number(value) < 0 || Number(value) > 100)) {
        callback(new Error("请输0-100之间的数字"))
      } else {
        callback()
      }
    }
  }
}
/* 手机校验end */

/* 2. 身份证校验 bengin */
const validateIdentity = text => {
  return (rule, value, callback) => {
    if (!value) {
      return callback()
    } else if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(value)) {
      callback(new Error("输入的" + text + "长度或格式错误"))
    }

    //身份证城市
    var aCity = {
      11: "北京",
      12: "天津",
      13: "河北",
      14: "山西",
      15: "内蒙古",
      21: "辽宁",
      22: "吉林",
      23: "黑龙江",
      31: "上海",
      32: "江苏",
      33: "浙江",
      34: "安徽",
      35: "福建",
      36: "江西",
      37: "山东",
      41: "河南",
      42: "湖北",
      43: "湖南",
      44: "广东",
      45: "广西",
      46: "海南",
      50: "重庆",
      51: "四川",
      52: "贵州",
      53: "云南",
      54: "西藏",
      61: "陕西",
      62: "甘肃",
      63: "青海",
      64: "宁夏",
      65: "新疆",
      71: "台湾",
      81: "香港",
      82: "澳门",
      91: "国外"
    }
    if (!aCity[parseInt(value?.substr(0, 2))]) {
      callback(new Error(text + "地区非法"))
    }
    // 出生日期验证
    var sBirthday = (value.substr(6, 4) + "-" + Number(value.substr(10, 2)) + "-" + Number(value.substr(12, 2))).replace(/-/g, "/"),
      d = new Date(sBirthday)
    if (sBirthday !== d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate()) {
      callback(new Error(text + "上的出生日期非法"))
    }

    // 身份证号码校验
    var sum = 0,
      weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
      codes = "10X98765432"
    for (var i = 0; i < value.length - 1; i++) {
      sum += value[i] * weights[i]
    }
    var last = codes[sum % 11] //计算出来的最后一位身份证号码
    if (value[value.length - 1] !== last) {
      callback(new Error("输入的身份证号非法"))
    }
    callback()
  }
}
export const identityCardRule = (rule, prop) => {
  if (isNull(rule)) return []
  const {
    require,
    validator,
    text = "身份证号"
  } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: "请输入" + text,
      trigger: "blur"
    })
  }
  if (validator) {
    result.push(
      {
        message: text + "不正确",
        trigger: "blur"
      },
      {
        trigger: "blur",
        validator: validateIdentity(text)
      }
    )
  }

  // return {
  //   idEntityCard: result
  // };
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}
/* 身份证校验 end */

/* 3车牌号校验 */
const validateCarnumber = () => {
  return (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 const carNumberRule = (rule, prop = "carnumber") => {
  if (isNull(rule)) return []
  const {
    require,
    validator
  } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: "请输入车牌号",
      trigger: "blur"
    })
  }
  if (validator) {
    result.push({
      trigger: "blur",
      validator: validateCarnumber()
    })
  }
  // return {
  //   carnumber: result
  // };
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}
/* 3车牌号校验end */

/* 4民族校验 */
export const nationSelectRule = (rule, prop = "nation") => {
  if (isNull(rule)) return []
  const { require } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: "请选择民族",
      trigger: "change"
    })
  }
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}

/* 5 组织机构 */
export const frameWorkRule = (rule, prop = "frameWork") => {
  if (isNull(rule)) return []
  const { require } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: "请选择组织机构",
      trigger: "change"
    })
  }
  // return {
  //   frameWork: result
  // };
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}

/* 6 选择地址 */
export const addressSelectRule = (rule, prop = "addredd") => {
  if (isNull(rule)) return []
  const { require } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: "请选择地址",
      trigger: "change"
    })
  }
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}

/* 7.Email */
const validateEmail = () => {
  return (rule, value, callback) => {
    const reg = /^([a-zA-Z0-9]+[-_\.]?)+@[a-zA-Z0-9]+\.[a-z]+$/
    if (!value) {
      callback()
    } else {
      if (!reg.test(value)) {
        return callback(new Error("请输入正确的邮箱地址"))
      } else {
        callback()
      }
    }
  }
}
export const emailRule = (rule, prop) => {
  if (isNull(rule)) return []
  const {
    require,
    validator
  } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: "请输入邮箱",
      trigger: "blur"
    })
  }
  if (validator) {
    result.push({
      trigger: "change",
      validator: validateEmail()
    })
  }
  // return {
  //   email: result
  // };
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}

/* 5 组织机构 */
export const departmentRule = (rule, prop = "department") => {
  if (isNull(rule)) return []
  const { require } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: "请选择部门",
      trigger: "change"
    })
  }
  // return {
  //   frameWork: result
  // };
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}

//校验是否为正整数
const validateIntRule = (num, message) => {
  return (rule, value, callback) => {
    // 正则表达式，匹配大于0的正整数
    const regex = /^[1-9]\d*$/
    if (!value || value == "") {
      callback()
    } else if (!regex.test(value)) {
      callback(new Error(message || "请输入大于0的正整数"))
    } else {
      callback()
    }
  }
}

const validate_intRule = (num, message) => {
  return (rule, value, callback) => {
    // 正则表达式，匹配大于0的正整数
    const regex = /^[0-9]\d*$/
    if (!value || value == "") {
      callback()
    } else if (!regex.test(value)) {
      callback(new Error(message || "请输入等于0的正整数"))
    } else {
      callback()
    }
  }
}

export const intRuleNumber = (rule, prop) => {
  if (isNull(rule)) return []
  const {
    require,
    validator,
    num = 1,
    message
  } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: "请输入数字",
      trigger: "blur"
    })
  }
  if (validator) {
    result.push({
      trigger: "change",
      validator: validate_intRule(num, message)
    })
  }
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}

export const intRule = (rule, prop) => {
  if (isNull(rule)) return []
  const {
    require,
    validator,
    num = 1,
    message
  } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: message || "请输入数字",
      trigger: "blur"
    })
  }
  if (validator) {
    result.push({
      trigger: "change",
      validator: validateIntRule(num, message)
    })
  }
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}
export const intfsRule = (rule, prop) => {
  if (isNull(rule)) return []
  const {
    require,
    validator
  } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: "请输入数字",
      trigger: "blur"
    })
  }
  if (validator) {
    result.push(
      // {
      //   message: "请输入正整数",
      //   trigger: "change"
      // },
      {
        trigger: "blur",
        validator: validatefsIntRule()
      }
    )
  }
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}

const validatefsIntRule = num => {
  return (rule, value, callback) => {
    const regex = /^(0|[1-9]\d*)$/ // 正则表达式，匹配0或大于0的正整数
    if (!value || value == "") {
      callback()
    } else if (!regex.test(value) || Number(value) > 99999999) {
      callback(new Error("请输入0到99999999之间的正数"))
    } else {
      callback()
    }
  }
}
//校验是否为负整数
const validateFintRule = rule => {
  return (rule, value, callback) => {
    if (!value) {
      callback()
    }
    if (value % 1 !== 0 || value > 0) {
      callback(new Error("请输入负整数"))
    } else {
      callback()
    }
  }
}
export const FintRule = (rule, prop) => {
  if (isNull(rule)) return []
  const {
    require,
    validator
  } = rule && { ...rule }
  const result = []
  if (require) {
    result.push({
      required: true,
      message: "请输入数字",
      trigger: "blur"
    })
  }
  if (validator) {
    result.push(
      // {
      //   message: "请输入负整数",
      //   trigger: "change"
      // },
      {
        trigger: "blur",
        validator: validateFintRule()
      }
    )
  }
  const resultObj = {}
  resultObj[prop] = result
  return { ...resultObj }
}
