// 手机号校验
export const checkPhoneNumber = async (rule, value) => {
  const { itemLabelName, validateErrorTip, required } = rule;
  if (required && !value) {
    return Promise.reject(validateErrorTip || `请输入${itemLabelName}`);
  }
  if (value) {
    const reg = /^1[345789]\d{9}$/;
    if (!reg.test(value)) {
      return Promise.reject(validateErrorTip || `请输入正确的${itemLabelName}格式`);
    } else {
      return Promise.resolve();
    }
  }
};

// 验证邮箱
export const validatorEnterpriseEmail = async (rule, value) => {
  const { itemLabelName, validateErrorTip, required } = rule;
  if (required && !value) {
    return Promise.reject(validateErrorTip || `请输入${itemLabelName}`);
  }
  if (value) {
    const reg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
    if (!reg.test(value)) {
      return Promise.reject(validateErrorTip || `请输入正确的${itemLabelName}格式`);
    } else {
      return Promise.resolve();
    }
  }
};

// 验证身份证
export const checkIdentityCode = async (rule, value) => {
  const { itemLabelName, validateErrorTip, required } = rule;
  if (required && !value) {
    return Promise.reject(validateErrorTip || `请输入${itemLabelName}`);
  }
  if (value) {
    let reg = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
    if (!reg.test(value)) {
      return Promise.reject(validateErrorTip || `请输入正确的${itemLabelName}格式`);
    } else {
      return Promise.resolve();
    }
  }
};

// 验证网址
export const checkWebsite = async (rule, value) => {
  const { itemLabelName, validateErrorTip, required } = rule;
  if (required && !value) {
    return Promise.reject(validateErrorTip || `请输入${itemLabelName}`);
  }
  if (value) {
    let reg = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/i;
    if (!reg.test(value)) {
      return Promise.reject(validateErrorTip || `请输入正确的${itemLabelName}格式`);
    } else {
      return Promise.resolve();
    }
  }
};

// 验证银行卡
export const checkBankcard = async (rule, value) => {
  const { itemLabelName, validateErrorTip, required } = rule;
  if (required && !value) {
    return Promise.reject(validateErrorTip || `请输入${itemLabelName}`);
  }
  if (value) {
    // 先检查是否为纯数字且长度在 13-19 位
    if (!/^[0-9]{13,19}$/.test(value)) {
      return Promise.reject(validateErrorTip || `请输入正确的${itemLabelName}格式`);
    }

    // Luhn 算法校验
    let sum = 0;
    let shouldDouble = false;

    // 从右到左遍历每一位
    for (let i = value.length - 1; i >= 0; i--) {
      let digit = parseInt(value.charAt(i), 10);

      if (shouldDouble) {
        digit *= 2;
        if (digit > 9) {
          digit -= 9;
        }
      }

      sum += digit;
      shouldDouble = !shouldDouble;
    }
    if (sum % 10 === 0) {
      return Promise.resolve();
    }
    return Promise.reject(validateErrorTip || `请输入正确的${itemLabelName}格式`);
  }
};

// 验证金额  保留两位小数
export const checkSumFixedTwo = async (rule, value) => {
  const { itemLabelName, validateErrorTip, required } = rule;
  if (required && !value) {
    return Promise.reject(validateErrorTip || `请输入${itemLabelName}`);
  }
  if (value) {
    // 支持正负号，最多2位小数
    const reg = /^[+-]?\d+(\.\d{1,2})?$/;
    if (!reg.test(value)) {
      return Promise.reject(validateErrorTip || `请输入正确的${itemLabelName}格式`);
    } else {
      return Promise.resolve();
    }
  }
};

// 验证纯数字  允许整数或小数
export const checkPureDigital = async (rule, value) => {
  const { itemLabelName, validateErrorTip, required } = rule;
  if (required && !value) {
    return Promise.reject(validateErrorTip || `请输入${itemLabelName}`);
  }
  if (value) {
    const reg = /^-?\d*\.?\d+$/;
    if (!reg.test(value)) {
      return Promise.reject(validateErrorTip || `请输入正确的${itemLabelName}格式`);
    } else {
      return Promise.resolve();
    }
  }
};

// 验证纯英文
export const checkPureEnglish = async (rule, value) => {
  const { itemLabelName, validateErrorTip, required } = rule;
  if (required && !value) {
    return Promise.reject(validateErrorTip || `请输入${itemLabelName}`);
  }
  if (value) {
    const reg = /^[a-zA-Z]+$/;
    if (!reg.test(value)) {
      return Promise.reject(validateErrorTip || `请输入正确的${itemLabelName}格式`);
    } else {
      return Promise.resolve();
    }
  }
};

// 验证纯中文
export const checkPureChinese = async (rule, value) => {
  const { itemLabelName, validateErrorTip, required } = rule;
  if (required && !value) {
    return Promise.reject(validateErrorTip || `请输入${itemLabelName}`);
  }
  if (value) {
    const reg = /^[\u4e00-\u9fa5]+$/;
    if (!reg.test(value)) {
      return Promise.reject(validateErrorTip || `请输入正确的${itemLabelName}格式`);
    } else {
      return Promise.resolve();
    }
  }
};

// 验证是否是数字英文下划线
export const isNumberEnglish = str => {
  const regex = /^[a-zA-Z0-9_]+$/;
  return regex.test(str);
};
