import { i18n } from "@/utils/i18ns";

/**
 * 验证电话号码
 * @param rule
 * @param value
 * @param callback
 */
export const validateMobile = (rule, value, callback) => {
  let reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
  if (value) {
    if (!reg.test(value)) {
      const msg = i18n.t('common.validation.mobile');
      callback(new Error(msg))
    } else {
      callback()
    }
  }
  callback();
}

/**
 * 验证座机号码
 * @param rule
 * @param value
 * @param callback
 */
export const validateOfficePhone = (rule, value, callback) => {
  let reg = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,6})?$/
  if (value) {
    if (!reg.test(value)) {
      const msg = i18n.t('common.validation.mobile');
      callback(new Error(msg))
    } else {
      callback()
    }
  }
  callback();
}

/**
 * 验证邮政编码
 * @param rule
 * @param value
 * @param callback
 */
export const validatePostCode = (rule, value, callback) => {
  let reg = /^[1-9][0-9]{5}$/
  if (value) {
    if (!reg.test(value)) {
      console.log(i18n);
      const msg = i18n.t('common.validation.postCode');
      callback(new Error(msg))
    } else {
      callback()
    }
  }
  callback()
}

/**
 * 验证金额
 * @param rule
 * @param value
 * @param callback
 */
export const validateAmount = (rule, value, callback) => {
  let reg = /^[0-9]\d{0,14}(?:\.\d{0,2})?$/
  if (value) {
    if (!reg.test(value)) {
      const msg = i18n.t('common.validation.amount');
      callback(new Error(msg))
    } else {
      callback()
    }
  }
  callback()
}

/**
 * 验证传真
 * @param rule
 * @param value
 * @param callback
 */
export const validateFax = (rule, value, callback) => {
  let reg = /^(?:\d{3,4}-)?\d{7,8}(?:-\d{1,6})?$/
  if (value) {
    if (!reg.test(value)) {
      const msg = i18n.t('common.validation.fax');
      callback(new Error(msg))
    } else {
      callback()
    }
  }
  callback();
}
/**
 * 验证邮箱
 * @param rule
 * @param value
 * @param callback
 */
export const validateEmail = (rule, value, callback) => {
  let reg = /\w@\w*\.\w/
  if (value) {
    if (!reg.test(value)) {
      const msg = i18n.t('common.validation.email');
      callback(new Error(msg))
    } else {
      callback()
    }
  }
  callback();
}

/**
 * 验证url
 * @param rule
 * @param value
 * @param callback
 */
export const validateUrl = (rule, value, callback) => {
  let reg = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/
  if (value) {
    if (!reg.test(value)) {
      const msg = i18n.t('common.validation.url');
      callback(new Error(msg))
    } else {
      callback()
    }
  }
  callback();
}