/**
 * Created by PanJiaChen on 16/11/18.
 */
 function accMulIn(arg1, arg2) {
  let m = 0,
    s1 = arg1.toString(),
    s2 = arg2.toString();

  try {
    m += s1.split(".")[1].length;
  } catch (e) {}

  try {
    m += s2.split(".")[1].length;
  } catch (e) {}

  return (
    (Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) /
    Math.pow(10, m)
  );
}
/** https
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path);
}
//区-座-分
export function isqzf(s) {
  let type = true;
  let arr = s.split("-");
  if (arr.length === 3) {
    type = setqj(arr[0]) && setzj(arr[1]) && setfj(arr[2]);
  } else {
    type = false;
  }
  console.log("console区-座-分 " + type);
  return type;
}
// //区-座
export function isqz(s) {
  let type = true;
  let arr = s.split("-");
  if (arr.length === 2) {
    type = setqj(arr[0]) && setzj(arr[1]);
  } else {
    type = false;
  }
  console.log("console区-座 " + type);
  return type;
}
//座-分
export function iszf(s) {
  let type = true;
  let arr = s.split("-");
  if (arr.length === 2) {
    type = setzj(arr[0]) && setfj(arr[1]);
  } else {
    type = false;
  }
  console.log("console座-分" + type);
  return type;
}

//座
export function isz(s) {
  let type = true;
  let arr = s.split("-");
  if (arr.length === 1) {
    type = setzj(arr[0]);
  } else {
    type = false;
  }
  console.log("console座" + type);
  return type;
}
function setqj(s) {
  return /0\d{2,3}?$/.test(s) && (s.length == 3 || s.length == 4);
}
function setzj(s) {
  return /\d{7,8}$/.test(s) && (s.length == 7 || s.length == 8);
}
function setfj(s) {
  return /\d{1,6}$/.test(s) && s.length > 0 && s.length < 7;
}

/** 用户名
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ["admin", "editor"];
  return valid_map.indexOf(str.trim()) >= 0;
}

/** url
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg =
    /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
  return reg.test(url);
}

/** 小写字母
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/;
  return reg.test(str);
}

/** 大写字母
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/;
  return reg.test(str);
}

/** 字母
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/;
  return reg.test(str);
}

/** 邮箱
 * @param {string} email
 * @returns {Boolean}
 */
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);
}

/** 字符串
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === "string" || str instanceof String) {
    return true;
  }
  return false;
}

/** 数组
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === "undefined") {
    return Object.prototype.toString.call(arg) === "[object Array]";
  }
  return Array.isArray(arg);
}

/**
 * 邮箱
 * @param {*} s
 */
export function isEmail(s) {
  return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(
    s
  );
}

/**
 * 手机号码
 * @param {*} s
 */
export function isMobile(s) {
  return isM(s) || isHK(s);
}
// 手机号码
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);
}

/**
 * 电话号码
 * @param {*} s
 */
export function isPhone(s) {
  return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s);
}

/**
 * 微信号
 * @param {*} s
 */
export function isWeixin(s) {
  return /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/.test(s);
}
/**
 * qq号
 * @param {*} s
 */
export function isQQ(s) {
  return /^[1-9][0-9]{4,14}$/.test(s);
}

/**
 * 固定电话(支持分机)
 * @param {*} s
 */
export function isExtension(s) {
  let type = true;
  let arr = s.split("-");
  // console.log(arr);
  if (arr.length === 2 || arr.length === 3) {
    type = setF(arr);
  } else {
    type = false;
  }
  // console.log('console' + type);
  return type;
  ///0\d{2,3}-\d{7,8}(-\d{1,6})?/.test(s)
}
function setF(arr) {
  let type1 = true,
    type2 = true,
    type3 = true;
  type1 = /0\d{2,3}?$/.test(arr[0]);
  type2 =
    arr[1].length === 7 || arr[1].length === 8
      ? /\d{7,8}$/.test(arr[1])
      : false;
  if (arr.length === 3) {
    type3 =
      arr[2].length === 0 || arr[2].length > 6
        ? false
        : /\d{1,6}$/.test(arr[2]);
  }
  // console.log(type1, type2, type3);
  if (arr.length === 2) {
    return type1 && type2;
  } else {
    return type1 && type2 && type3;
  }
}

// (^(\d{11})$|^((\d{3}-)?\d{8}(-\d{1,4})?)$|^((\d{4}-)?\d{7}(-\d{1,4})?)$|^(\d{7,8})$)
/**
 * 纳税人识别号
 * @param {*} s
 */
export function isLicense(s) {
  return /[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}/.test(s);
}

/**
 * 身份证号码
 * @param {*} s
 */
export function isIDentityCard(s) {
  return /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(
    s
  );
}

/**
 * URL地址
 * @param {*} s
 */
export function isURL(s) {
  return /^http[s]?:\/\/.*/.test(s);
}

/**
 * 汉语
 * @param {*} s
 */
export function isChinese(s) {
  return /[\u4e00-\u9fa5]$/.test(s);
}

/**
 * 数字
 * @param {*} s
 */
export function isnumber(s) {
  return /^[0-9]*$/.test(s);
}
/**
 * 数字或者带小数点的数字
 * @param {*} s
 */
export function isnumber2(s) {
  return /(^[0-9]{1,2}$)|(^[0-9]{1,2}[\.]{1}[0-9]{1,2}$)/.test(s);
}
/**
 * 数字或者带小数点的数字 1,2,3位小数
 * @param {*} s
 */
export function isnumber3(s) {
  return /^(([^0][0-9]+|0)\.([0-9]{1,3})$)|^([^0][0-9]+|0)$/.test(s);
}
/**
 * 数字字母
 * @param {*} s
 */
export function isAlphanumeric(s) {
  return /^[0-9A-Za-z]*$/.test(s);
}
/**
 * 数字大写字母
 * @param {*} s
 */
 export function isNumeric(s) {
  return /^[0-9A-Z]*$/.test(s);
}

/**
 * 表情包
 * @param {*} s
 */
export function isEmoticon(s) {
  const reg =
    /[^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n]/g;
  // console.log(reg.test(s));
  return reg.test(s);
}

/**
 * 判断是否为微信浏览器
 * @param {*} s
 */
export function JudgeEnvironment() {
  const ua = navigator.userAgent.toLowerCase();
  let environment = "";
  const isWeixin = ua.indexOf("micromessenger") != -1;
  if (isWeixin) {
    environment = "Weixin";
  }
  const isWelink = ua.indexOf("huawei-anyoffice") != -1;
  if (isWelink) {
    environment = "Welink";
  }
  const isDingDing = ua.indexOf("dingtalk") != -1;
  if (isDingDing) {
    environment = "isDingDing";
  }
  if (environment === "") {
    environment = "otherBrowser";
  }
  return environment;
}

export function timestampToTime(timestamp) {
  const date = new Date(timestamp); // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
  const Y = date.getFullYear() + "-";
  const M =
    (date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1) + "-";
  const D = date.getDate() + " ";
  let h = date.getHours();
  h = h < 10 ? `0${h}:` : h + ":";
  let m = date.getMinutes();
  m = m < 10 ? `0${m}:` : m + ":";
  let s = date.getSeconds();
  s = s < 10 ? `0${s}` : s;
  return Y + M + D + h + m + s;
}
export function isCreditCode(s) {
  const reg = /[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/g;
  return reg.test(s);
}
//加法精算
export function add_sum(arg1, arg2) {
  let r1, r2, m;
  try {
    r1 = arg1.toString().split(".")[1].length;
  } catch (e) {
    r1 = 0;
  }

  try {
    r2 = arg2.toString().split(".")[1].length;
  } catch (e) {
    r2 = 0;
  }

  m = Math.pow(10, Math.max(r1, r2));

  return (accMulIn(arg1, m) + accMulIn(arg2, m)) / m;
}

//乘法精算
export function accMul(arg1, arg2) {
  return accMulIn(arg1, arg2)
}
 //除法精度问题
 export function accDiv(arg1, arg2) {
  let t1 = 0,
    t2 = 0,
    c1,
    c2;
  try {
    t1 = arg1.toString().split(".")[1].length;
  } catch (e) {}
  try {
    t2 = arg2.toString().split(".")[1].length;
  } catch (e) {}
  // with (Math) {
  c1 = Number(arg1.toString().replace(".", ""));
  c2 = Number(arg2.toString().replace(".", ""));
  return (c1 / c2) * Math.pow(10, t2 - t1);
  // }
};
export function isSpecialSymbol(s) {
  // console.log(s);
  const str = (s ?? "").replace(
    /\\|\/|\"|\'|\<|\>|\{|\}|\[|\]|\:|\^|\$|\!|\~|\`|\|/g,
    ""
  );
  // console.log(str);
  const pattern = new RegExp("[`~!@#$^&()=|{}':;',<>/?]");
  let specialStr = "";
  for (let i = 0; i < str.length; i++) {
    specialStr += str.substr(i, 1).replace(pattern, "");
  }
  return s.length > specialStr.length;
}

export function isAddr(s) {
  let isok = false;
  const regList = [
    "多地",
    "另给",
    "线下",
    "地址",
    "单独给",
    "内部沟通",
    "微信",
    "钉钉",
    "短信",
    "文件",
    "另外",
    "后面",
    "以后",
    "晚点",
    "后补",
    "再给",
  ];
  for (let i = 0; i < regList.length; i++) {
    const is = s.includes(regList[i]);
    if (is) {
      isok = is;
    }
  }
  const sstr = /^[0-9A-Za-z]*$/;

  if (sstr.test(s)) {
    isok = true;
  }

  if (!isok) {
    let reslist = [];
    let str = "";

    for (let i = 0; i < s.length; i++) {
      let isa = sstr.test(s[i]);
      // console.log(isa);
      if (isa) {
        str += s[i];
      } else {
        reslist.push(str);
        str = "";
      }
      if (i === s.length - 1) {
        reslist.push(str);
        str = "";
      }
    }
    for (let b = 0; b < reslist.length; b++) {
      if (reslist[b].length > 5) {
        isok = true;
      }
    }
  }
  return !isok;
}
export function hasSpace(s) {
  // const str1 = s.replace(/\s/g, "");
  const str2 = s.replace(/[\r\n]/g, "");
  return !(str2 === s);
}


