// 类似金钱,首位不为0,最多2位小数...
export function checkNumPot2(rule, value, callback) {
  const reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
  if (!value) {
    return callback(new Error("请填写数字"));
  } else if (value == 0) {
    return callback(new Error("请输入正数,最多2位小数"));
  } else if (!reg.test(value)) {
    return callback(new Error("请输入正数,最多2位小数"));
  } else {
    callback();
  }
}
//最多4位小数
export function checkNumPot4(rule, value, callback) {
  const reg = /^\d+(?:\.\d{1,4})?$/;
  if (!value) {
    return callback(new Error("请填写数字"));
  } else if (value == 0) {
    return callback(new Error("请输入正数,最多4位小数"));
  } else if (!reg.test(value)) {
    return callback(new Error("请输入正数,最多4位小数"));
  } else {
    callback();
  }
}

// 整数
export function checkInterNum(rule, value, callback) {
  const reg = /^[0-9]*[1-9][0-9]*$/;
  if (!value) {
    return callback(new Error("请填写正整数"));
  } else if (!reg.test(value)) {
    return callback(new Error("请输入正整数"));
  } else {
    callback();
  }
}

// 汉字
export function checkChinese(rule, value, callback) {
  const reg = /^[\u4e00-\u9fa5]+$/;
  if (!value) {
    return callback(new Error("请填写汉字"));
  } else if (!reg.test(value)) {
    return callback(new Error("请输入汉字"));
  } else {
    callback();
  }
}

// 自然数
export function checkNatureNum(rule, value, callback) {
  const reg = /^\d+$/;
  if (!value) {
    return callback(new Error("请填写自然数"));
  } else if (!reg.test(value)) {
    return callback(new Error("请输入自然数"));
  } else {
    callback();
  }
}
//耗材编码
export function proCodeRule(rule, value, callback) {
  const reg = new RegExp("^[A-Z]");
  if (value != "") {
    if (!reg.test(value)) {
      return callback(new Error("请输入正确的医保编码,，且必须为大写字母开头"));
    } else {
      callback();
    }
  } else {
    callback();
  }
}

// 联系方式
const checkPhoneNum = (rule, value, callback) => {
  const reg = new RegExp(
    "^1\\d{10}$|^(0\\d{2,3}-?|\\(0\\d{2,3}\\))?[1-9]\\d{4,7}(-\\d{1,8})?$"
  );
  if (!value) {
    return callback(new Error("请输入正确的联系方式"));
  } else if (!reg.test(value)) {
    return callback(new Error("请输入正确的联系方式(手机号或电话号码)"));
  } else {
    callback();
  }
};

// 邮箱
const checkEmail = (rule, value, callback) => {
  const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
  if (!value) {
    callback(new Error("邮箱不能为空"));
  } else if (mailReg.test(value)) {
    callback();
  } else {
    callback(new Error("请输入正确的邮箱格式"));
  }
};

// 证件号码
const checkCard = (rule, value, callback) => {
  const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  if (!value) {
    callback(new Error("证件号码不能为空"));
  } else if (reg.test(value)) {
    callback();
  } else {
    callback(new Error("请输入正确的证件号码"));
  }
};

export default {
  phone: [
    {
      required: true,
      pattern: /^1[34578]\d{9}$/,
      message: "目前只支持中国大陆的手机号码",
      trigger: "blur"
    }
  ],
  numPotNo: [
    {
      pattern: /^\d+(?:\.\d{1,4})?$/,
      message: "请输入正数,最多4位小数",
      trigger: "blur"
    }
  ],
  InterEng: [
    {
      pattern: /^[a-zA-Z\s]+$/,
      message: "只包含英文字母和空格",
      trigger: "blur"
    }
  ],
  proCodeRule: [{ required: false, validator: proCodeRule, trigger: "blur" }],
  numPot2: [{ required: true, validator: checkNumPot2, trigger: "blur" }],
  numPot4: [{ required: true, validator: checkNumPot4, trigger: "blur" }],
  InterNum: [{ required: true, validator: checkInterNum, trigger: "blur" }],
  InterChina: [{ required: true, validator: checkChinese, trigger: "blur" }],
  natureNum: [{ required: true, validator: checkNatureNum, trigger: "blur" }],
  phoneNum: [{ required: true, validator: checkPhoneNum, trigger: "blur" }],
  email: [{ required: true, validator: checkEmail, trigger: "blur" }],
  card: [{ required: true, validator: checkCard, trigger: "blur" }],
  required: [{ required: true, message: "请输入", trigger: "blur" }],
  selectRequired: [{ required: true, message: "请选择", trigger: "change" }],
  uploadRequired: [
    { required: true, message: "请上传文件", trigger: "change" }
  ],
  dateRequired: [{ required: true, message: "请选择日期", trigger: "blur" }],
  loginId: [{ required: true, message: "请填写账号", trigger: "blur" }],
  password: [{ required: true, message: "请填写密码", trigger: "blur" }],
  noRequire: [{ required: false }]
};
