// 必填
export const required = [
  { required: true, message: "请填值", trigger: "blur" },
];

export function requiredValueMin(num) {
  return [
    { required: true, message: "请填值", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        if (value < num) {
          callback(new Error(`值必须大于等于${num}`));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ];
}

// 手机号码校验必填
export const mobileRequired = [
  { required: true, message: "请输入", trigger: "blur" },
  {
    validator: (rule, value, callback) => {
      var reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
      if (!reg.test(value)) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    },
    trigger: "blur",
  },
];

export const mobileRequired2 = [
  { required: true, message: "请输入", trigger: "blur" },
  {
    validator: (rule, value, callback) => {
      if (value.length !== 11) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    },
    trigger: "blur",
  },
];

export const mobileRequired3 = [
  {
    validator: (rule, value, callback) => {
      if (value && value.length !== 11) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    },
    trigger: "blur",
  },
];

// 手机号码校验非必填
export const mobile = [
  {
    validator: (rule, value, callback) => {
      var reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
      if (value && !reg.test(value)) {
        callback(new Error("请输入正确的手机号"));
      } else {
        callback();
      }
    },
    trigger: "blur",
  },
];

// 邮箱校验非必填
export const email = [
  {
    validator: (rule, value, callback) => {
      var result = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value);
      if (value && !result) {
        callback(new Error("请输入正确的邮箱"));
      } else {
        callback();
      }
    },
    trigger: "blur",
  },
];

// 身份证号码校验非必填
export const idcard = [
  {
    validator: (rule, value, callback) => {
      var result =
        /^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}[\dXx]$/g.test(
          value
        );
      if (value && !result) {
        callback(new Error("请输入正确的身份证号"));
      } else {
        callback();
      }
    },
    trigger: "blur",
  },
];

// 网址校验非必填
export const httpUrl = [
  {
    validator: (rule, value, callback) => {
      var result =
        /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\*\+,;=.]+$/.test(
          value
        );
      if (value && !result) {
        callback(new Error("请输入正确的网址"));
      } else {
        callback();
      }
    },
    trigger: "blur",
  },
];

export function diyRequired(cb) {
  return {
    validator: (rule, value, callback) => {
      if (value === undefined || value === "" || value === null) {
        return callback(cb(rule, value));
      }
      callback();
    },
    trigger: "blur",
  };
}
