import validate from "./validate"

let rulesArray = []

export function addRules(rules) {
  let flag = true
  rulesArray.forEach((v) => {
    if (v.name === rules.name && v.Msg === rules.Msg) {
      flag = false
    }
  })
  if (flag) {
    rulesArray.push(rules)
  }
}
export function clearOneRule(val) {
  rulesArray.forEach((v, i) => {
    if (v.name === val.name || v.Msg === val.Msg) return rulesArray.splice(i, 1)
  })
}
export function clearRulesArray() {
  rulesArray = []
}
// 校验函数
export function validateArray(val) {
  let flag = true
  for (let i = 0; i < rulesArray.length; i++) {
    if (!validate[rulesArray[i].rule](val[rulesArray[i].name])) {
      uni.showToast({
        title:
          rulesArray[i].rule == "REG_NotNUll"
            ? rulesArray[i].Msg.includes("请上传")
              ? rulesArray[i].Msg
              : "请输入" + rulesArray[i].Msg
            : "请检查" + rulesArray[i].Msg + "格式",
        icon: "none",
      })
      flag = false
      break
    } else {
      flag = true
    }
  }
  return flag
}
