function isM(s) {
  return /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(s);
}

function isHK(s) {
  return /^852[3|5|6|8|9]\d{7}$/.test(s);
}

export function isMobile(s) {
  return isM(s) || isHK(s);
}

export function isWeixin(s) {
  return /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/.test(s);
}

export function isQQ(s) {
  return /^[1-9][0-9]{4,14}$/.test(s);
}

export function validEmail(email) {
  const reg =
    /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return reg.test(email);
}


const validateEmail = (rule, value, callback) => {
  if (value === "" || !value) {
    callback();
  } else {
    if (!validEmail(value)) {
      callback(new Error("邮箱格式不正确！"));
    } else {
      callback();
    }
  }
};
const validatemobile = (rule, value, callback) => {
  if (value === "" || !value) {
    callback(new Error("电话不能为空！"));
  } else {
    if (!isMobile(value)) {
      callback(new Error("电话格式不正确！"));
    } else {
      callback();
    }
  }
};

const validateWX = (rule, value, callback) => {
  if (value === "" || !value) {
    callback();
  } else {
    if (!isWeixin(value)) {
      callback(new Error("微信号格式不正确！"));
    } else {
      callback();
    }
  }
};
const validateQQ = (rule, value, callback) => {
  if (value === "" || !value) {
    callback();
  } else {
    console.log(value);
    if (!isQQ(value)) {
      callback(new Error("QQ号格式不正确！"));
    } else {
      callback();
    }
  }
};

export const rules = {
  contactor: [
    {
      required: true,
      message: "联系人不能为空！",
      trigger: "change",
    },
  ],
  position: [
    {
      required: true,
      message: "职位不能为空",
      trigger: "blur",
    },
  ],
  comdepart: [
    {
      required: true,
      message: "部门不能为空",
      trigger: "blur",
    },
  ],
  commobile: [
    {
      required: true,
      validator: validatemobile,
      trigger: "blur",
    },
  ],
  wxaccount: [
    {
      required: false,
      validator: validateWX,
      trigger: "blur",
    },
  ],
  qqaccount: [
    {
      required: false,
      validator: validateQQ,
      trigger: "blur",
    },
  ],
  email: [
    {
      required: false,
      validator: validateEmail,
      trigger: "blur",
    },
  ],
}
