const is_empty_string = function (value) {
  return value == null || value == "" || value.toString().trim() == "";
};
const is_number = function (value) {
  return /^\d+\.?\d*$/.test(value.toString().trim());
};
const is_AllNumber = function (value) {
  return /^(\-|\+)?\d+(\.\d+)?$/.test(value.toString().trim());
};
const is_positive_integer = function (value) {
  return /^[1-9]\d*$/.test(value.toString().trim());
};
//小数正则：小数点后可以有一位、两位小数，或没有小数部分
const is_decimal = function (value) {
  return /^\d+(\.\d{1,2})?$/.test(value.toString().trim());
};
const is_tel = function (value){
  value=value||'';
  return /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value.toString().trim());
};
//固定电话验证:
const is_phone = function (value) {
  return /(^((0\d{2,3})-)?(\d{7,8})(-(\d{3,8}))?$)|(^400\d{7}$)/.test(value.toString().trim());
};
//身份证验证 （含港澳台）
const is_IDcard = function (value) {
  return /(^\d{17}(\d|X|x)$)|(^[HMhm]{1}([0-9]{10}|[0-9]{8})$)/.test(value.toString().trim());
};

function plateNumber(value) {
  value=value||'';
  if(value.length>8) return false;
  return /([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1})/.test(value.toString().trim());
}
export default {
  required: function (msg, trigger = "change") {
    return {
      required: true,
      validator: this.trim,
      message: msg + "不能为空",
      trigger: trigger
    };
  },
  length: function (min, max) {
    return {
      min: min,
      max: max,
      message: "长度在" + min + "到" + max + "个字符",
      trigger: "blur"
    };
  },
  date: function (msg = "日期") {
    return {
      type: "date",
      required: true,
      message: "请选择" + msg,
      trigger: "change"
    };
  },
  number: function (msg = "") {
    return {
      type: "number",
      required: true,
      message: msg + "必须为数字",
      trigger: "change"
    };
  },
  integer: function (msg = "") {
    return {
      type: "integer",
      required: true,
      message: msg + "必须为整数",
      trigger: "change"
    };
  },
//   不能为空的正整数
  integerZ: function (msg = "", min = 0, max = 9999999, required = true) {
    return {
      type: "integer",
      required: required,
      validator: function (rule, value, callback) {
        if (value != null) value = value.toString().trim();
        if (!required && !value) {
          return callback();
        }
        if (value === '' || value === null || value === undefined) {
          return callback(new Error(msg + "不能为空"));
        }
        if (!/^[0-9]+$/.test(value)) {
          return callback(new Error(msg + "必须为正整数"));
        } else if (value <= min || value > max) {
          return callback(new Error(msg + "必须为大于" + min + "小于等于" + max + "的整数"));
        } else {
          return callback();
        }
      },
      trigger: "change"
    };
  },
//   大于0的整数
integerZ0: function (msg = "", min = -1, max = 9999999, required = true) {
    return {
      type: "integer",
      required: required,
      validator: function (rule, value, callback) {
        if (value != null) value = value.toString().trim();
        if (!required && !value) {
          return callback();
        }
        if (value === '' || value === null || value === undefined) {
          return callback(new Error(msg + "不能为空"));
        }
        if (!/^[0-9]+$/.test(value)) {
          return callback(new Error(msg + "必须为正整数"));
        } else if (value <= min || value > max) {
          return callback(new Error(msg + "必须为大于" + min + "小于等于" + max + "的整数"));
        } else {
          return callback();
        }
      },
      trigger: "change"
    };
  },
  //   大于0小于100的整数
integerZ100: function (msg = "", min = -1, max = 9999999, required = true) {
    return {
      type: "integer",
      required: required,
      validator: function (rule, value, callback) {
        if (value != null) value = value.toString().trim();
        if (!required && !value) {
          return callback();
        }
        if (value === '' || value === null || value === undefined) {
          return callback(new Error(msg + "不能为空"));
        }
        if (!/^[0-9]+$/.test(value)) {
          return callback(new Error(msg + "必须为正整数"));
        } else if (value <= min || value >= max) {
          return callback(new Error(msg + "必须为大于" + min + "小于" + max + "的整数"));
        } else {
          return callback();
        }
      },
      trigger: "change"
    };
  },
  //   大于0的整数
  zoneCode: function (msg = "", min = -1, required = true) {
    return {
        type: "integer",
        required: required,
        validator: function (rule, value, callback) {
          if (value != null) value = value.toString().trim();
          if (!required && !value) {
            return callback();
          }
          if (value === '' || value === null || value === undefined) {
            return callback(new Error(msg + "不能为空"));
          }
          if (value.length !== 12) {
            return callback(new Error(msg + "请填写正确的格式"));
          }
          if (!/^[0-9]+$/.test(value)) {
            return callback(new Error(msg + "请填写正确的格式"));
          } else if (value <= min) {
            return callback(new Error(msg + "请填写正确的格式"));
          } else {
            return callback();
          }
        },
        trigger: "change"
      };
  },
  /** 不可空的数字范围 >= && <= */
  range: function (msg = "", min = 0, max = 999999999) {
    return {
      type: "number",
      required: true,
      min,
      max,
      message: msg +
        "必须为大于等于" +
        min +
        (max ? "且小于等于" + max : "") +
        "的数字",
      trigger: "change"
    };
  },
  range2: function (msg = "", min = 1, max = 999999999) {
    return {
      type: "number",
      required: true,
      min,
      max,
      message: msg + "必须为大于0" + (max ? "且小于等于" + max : "") + "的数字",
      trigger: "change"
    };
  },
  range3: function (msg = "", min = 0, max = 999999999) {
    return {
      type: "number",
      required: true,
      min,
      max,
      message: msg + "必须为大于0小于" + max + "的数字",
      trigger: "change"
    };
  },
  range4: function (msg = "", required = true, min = 0.0000001) {
    return {
      type: "number",
      required: required,
      min,
      message: msg + "必须为大于0的数字",
      trigger: "change"
    };
  },

  rangeNum: function (msg = "", required = true, min = 0, max = 999999999) {
    return {
      type: "number",
      required: required,
      min,
      max,
      validator: function (rule, value, callback) {
        if (value === '' || value === null || value === undefined) {
          if (required) {
            return callback(new Error(msg + "不能为空"));
          } else {
            return callback();
          }
        }
        value = value.toString().trim();
        if (value <= min) {
          return callback(new Error(msg + "不能小于或等于" + min));
        } else if (value > max) {
          return callback(new Error(msg + "不能大于" + max));
        } else {
          return callback();
        }
      },
      // message: msg + "必须为大于0" + (max ? "且小于等于" + max : "") + "的数字",
      trigger: ["change", "blur"]
    };
  },
  /** 可空的数字范围 >= && <=  */
  nullNumber: function (msg = "") {
    return {
      type: "number",
      required: false,
      message: msg + "必须为数字",
      trigger: "change"
    };
  },
  nullRange: function (msg = "", min = 1) {
    return {
      type: "number",
      min,
      message: msg + "只可以为大于等于" + min + "的数字",
      trigger: "change"
    };
  },
  /** 默认不可空的数字范围 >min && <=max */
  excludeRange: function (
    title = "",
    required = true,
    min = 0,
    max = 999999999
  ) {
    return {
      validator: this.myExcludeIncludeRangeValidator,
      required,
      min,
      max,
      title, //不要用message，否则抛出的Error描述文字会被message字段覆盖
      trigger: "blur"
    };
  },
  email: function (msg = "邮箱") {
    return {
      type: "email",
      required: true,
      message: "请输入正确的" + msg,
      trigger: ["blur", "change"]
    };
  },
  nullemail: function (msg = "邮箱") {
    return {
      type: "email",

      message: "请输入正确的" + msg,
      trigger: ["blur", "change"]
    };
  },
  IDcard: function (msg = '身份证号', required = true) {
    return {
      type: "string",
      required: required,
      validator: this.validIDcard,
      msg: msg,
      // message: "请输入正确的" + msg,
      trigger: ["blur", "change"]
    }
  },
  BankCode: function (msg = '银行账号', required = true) {
    return {
      type: "string",
      required: required,
      validator: this.validBankCode,
      msg: msg,
      trigger: ["blur", "change"]
    }
  },
  desc: function (min = 20, msg = "") {
    return {
      type: "string",
      min: min,
      message: msg + "不能少于20字",
      trigger: "blur"
    };
  },
  chineseName: function (msg = "") {
    return {
      validator: this.validChinese,
      message: msg + "必须为中文",
      trigger: ["blur", "change"]
    };
  },
  noChinese: function (msg = "") {
    return {
      validator: this.validNOChinese,
      message: "需为不包含中文或空格的6-15位" + msg,
      trigger: ["blur", "change"]
    };
  },
  letter: function (msg = "") {
    return {
      required: true,
      validator: this.vaileLetter,
      message: msg + "仅支持数字和字母",
      trigger: ["blur", "change"]
    };
  },
  tel: function (msg = "手机号") {
    return {
      required: true,
      validator: this.validTel,
      message: msg + "格式不正确",
      trigger: ["blur", "change"]
    };
  },
  nullTel: function (msg = "手机号") {
    //可空手机号
    return {
      validator: this.validNullTel,
      message: msg + "格式不正确",
      trigger: ["blur", "change"]
    };
  },
  phone: function (msg = '固定电话', required = true) {
    return {
      required: required,
      validator: this.validPhone,
      message: msg + "格式不正确，如:区号-号码",
      trigger: ["blur", "change"]
    };
  },
  telPhone: function (msg = "联系方式", required = true) {
    return {
      required: required,
      validator: this.validTelPhone,
      message: msg + "格式不正确",
      trigger: ["blur", "change"]
    };
  },
//   大于0且不超过两位小数的数字
  decimals: (msg = '', max = 999999999, required = true, can0 = false, canNegative = true) => {
    return {
      required: required,
      validator: (rule, value, callback) => {
        if (value === '' || value === null || value === undefined) {
          if (required) {
            return callback(new Error(msg + "不能为空"))
          } else {
            return callback();
          }
        } else if (!is_AllNumber(value)) {
          callback(new Error("请输入数字"));
        } else {
          if (!canNegative && value < 0) {
            callback(new Error("不能为负数"));
          }
          if (can0 && value === 0) {
            return callback()
          }
          if (value <= 0 || value >= max) callback(new Error("请输入大于0小于" + max + "的数字"));
          else {
            if (value % 1 > 0) {
              if (value.toString().split(".")[1].length > 2)
                callback(new Error("请输入不超过两位小数的数字"));
              else callback();
            } else callback();
          }
        }
      },
      trigger: ["blur", "change"]
    };
  },
// 大于等于0且不超过两位的小数
decimals2: (msg = '', max = 999999999, required = true, can0 = false, canNegative = true) => {
    return {
      required: required,
      validator: (rule, value, callback) => {
        if (value === '' || value === null || value === undefined) {
          if (required) {
            return callback(new Error(msg + "不能为空"))
          } else {
            return callback();
          }
        } else if (!is_AllNumber(value)) {
          callback(new Error("请输入数字"));
        } else {
          if (!canNegative && value < 0) {
            callback(new Error("不能为负数"));
          }
          if (can0 && value === 0) {
            return callback()
          }
          if (value < 0 || value >= max) callback(new Error("请输入大于等于0小于" + max + "的数字"));
          else {
            if (value % 1 > 0) {
              if (value.toString().split(".")[1].length > 2)
                callback(new Error("请输入不超过两位小数的数字"));
              else callback();
            } else callback();
          }
        }
      },
      trigger: ["blur", "change"]
    };
  },
//   大于0且小于十万的数字
  decimals3: (msg = '', max = 99999, required = true, can0 = false, canNegative = true) => {
    return {
      required: required,
      validator: (rule, value, callback) => {
        if (value === '' || value === null || value === undefined) {
          if (required) {
            return callback(new Error(msg + "不能为空"))
          } else {
            return callback();
          }
        } else if (!is_AllNumber(value)) {
          callback(new Error("请输入数字"));
        } else {
          if (!canNegative && value < 0) {
            callback(new Error("不能为负数"));
          }
          if (can0 && value === 0) {
            return callback()
          }
          if (value <= 0 || value >= max) callback(new Error("请输入大于0小于" + max + "的数字"));
          else {
            if (value % 1 > 0) {
              if (value.toString().split(".")[1].length > 2)
                callback(new Error("请输入不超过两位小数的数字"));
              else callback();
            } else callback();
          }
        }
      },
      trigger: ["blur", "change"]
    };
  },
  nullDecimals: function (msg = "", num = 2) {
    //可空的数值，比如金额。lzf
    return {
      validator: (rule, value, callback) => {
        if (value) {
          var reg = new RegExp("^\\d+(\\.\\d{1," + num + "})?$", "ig");
          if (!(reg.test(value.toString()) && !isNaN(Number(value)))) {
            callback(new Error("请输入数值,保留" + num + "位小数"));
          } else {
            callback();
          }
        } else {
          callback();
        }
      },
      trigger: ["blur", "change"]
    };
  },
  validTel: function (rule, value, callback) {
    if (value === '' || value === undefined) {
      return callback(new Error("手机号不能为空"));
    }
    if (!is_tel(value)) {
      return callback(new Error("手机号格式不正确"));
    } else {
      return callback();
    }
  },
  validNullTel: function (rule, value, callback) {
    if (value.toString().trim() && !is_tel(value)) {
      return callback(new Error("手机号格式不正确"));
    } else {
      return callback();
    }
  },
  validPhone: function (rule, value, callback) {
    if (value && value.toString().trim()) {
      if (is_phone(value)) {
        return callback();
      } else {
        return callback(new Error(rule.msg + "格式不正确"));
      }
    } else {
      if (rule.required) {
        return callback(new Error(rule.msg + "不能为空"));
      } else {
        return callback();
      }
    }
  },
  validTelPhone: function (rule, value, callback) {
    if (value && value.toString().trim()) {
      if (is_phone(value) || is_tel(value)) {
        return callback();
      } else {
        return callback(new Error(rule.msg + "格式不正确"));
      }
    } else {
      if (rule.required) {
        return callback(new Error(rule.msg + "不能为空"));
      } else {
        return callback();
      }
    }
  },
  validChinese: function (rule, value, callback) {
    if (/^[\u2E80-\u9FFF]+$/.test(value)) {
      return callback();
    } else {
      return callback(new Error("必须为中文"));
    }
  },
  validIDcard: function (rule, value, callback) {
    if (value && value.toString().trim()) {
      if (is_IDcard(value)) {
        return callback();
      } else {
        return callback(new Error(rule.msg + "格式不正确"));
      }
    } else {
      if (rule.required) {
        return callback(new Error(rule.msg + "不能为空"));
      } else {
        return callback();
      }
    }
  },
  validBankCode: function (rule, value, callback) {
    if (value && value.toString().trim()) {
      if (/^([1-9]{1})(\d{14}|\d{19})$/.test(value)) {
        return callback();
      } else {
        return callback(new Error(rule.msg + "格式不正确"));
      }
    } else {
      if (rule.required) {
        return callback(new Error(rule.msg + "不能为空"));
      } else {
        return callback();
      }
    }
  },
  validNOChinese: function (rule, value, callback) {
    if (/^[^\u2E80-\u9FFF\s]{6,15}$/.test(value)) {
      return callback();
    } else {
      return callback(new Error("不可包含中文或空格"));
    }
  },
  vaileLetter: function (rule, value, callback) {
    if (/^[a-z0-9A-Z]+$/.test(value)) {
      return callback();
    } else {
      return callback(new Error("仅支持数字和字母"));
    }
  },
  trim: function (rule, value, callback) {
    if (value == null || $.trim(value) == "") {
      return callback(new Error("不能为空"));
    } else {
      return callback();
    }
  },
  myExcludeIncludeRangeValidator: function (rule, value, callback) {
    if (!rule.required && value == null) {
      callback();
    } else if (isNaN(value)) {
      callback(new Error(rule.title + "必须为数字"));
    } else if (value <= rule.min || value > rule.max) {
      callback(
        new Error(
          rule.title +
          "必须为大于" +
          rule.min +
          (rule.max ? "且小于等于" + rule.max : "") +
          "的数字"
        )
      );
    } else callback();
  },
  plateNumber: function (msg) {
    return {
      required:true,
      validator: function (rule, value, callback) {
        if (!value) return callback(new Error(`请输入${msg||'车牌号'}`));
        if (!plateNumber(value)) return callback(new Error(`${msg||'车牌号'}格式错误`));
        return callback();
      },
      trigger: 'blur'
    };
  },
};
