// companyId加密
let encodeStr = function(str, secondary) {
  const table = 'VyB2Kz79QWYjpiD5lRCIMwJEhqFSx0GN1cveZfU4gs6rk8dPbLtAomOnT3'
  const ss = [2, 7, 5, 1, 4, 8, 3, 0, 6]
  const xor = 177451812
  const add = 8728348608
  const mp2 = new Map()
  let content = secondary ? str : '         '
  let result = '' // 最终加密id
  if (!secondary) {
    let s = parseInt(str)
    s = (s ^ xor) + add
    for (let i = 0; i < table.length; i++) {
      const s1 = table.substring(i, i + 1)
      mp2.set(i, s1)
    }
    for (let i = 0; i < 9; i++) {
      const r = mp2.get(parseInt(s / power(58, i) % 58))
      content = changeStr(content, ss[i], r)
    }
  }
  // 二次加密
  const idArr = content.split('')
  for (var i = 0; i < idArr.length; i++) {
    const hex = idArr[i].charCodeAt().toString(16)
    result = result + hex
  }
  return result
}

function power(a, b) {
  let power = 1
  for (let c = 0; c < b; c++) {
    power *= a
  }
  return power
}

function changeStr(str, index, changeStr) {
  return str.substr(0, index) + changeStr + str.substr(index + changeStr.length)
}

//甲方详情子页面互跳
function changePath(that, pathName){
  try {
    that.$parent.showPartPage({pathName})
  } catch {
    that.$parent.$parent.showPartPage({pathName})
  }
}

//保留小数N位
function saveFixed(field, num=2){
  return parseFloat(field.toFixed(num))
}

//移除字符串中指定标签
let removeTag = function(str, external, implant, row) { //str字符串， external外部引用方式的标签组[一个标签]， implant嵌入引用的标签组[包含首尾标签]， row行内标签组
  let newHtml = str
  if(external){
    let externalArr = external.split(",")
    for (let item of externalArr){
      newHtml = newHtml.replace(new RegExp("<"+item+"([^>]+)>", "img"),"")
    }
  }
  if(implant){
    let implantArr = implant.split(",")
    for (let item of implantArr){
      newHtml = newHtml.replace(new RegExp("<"+item+"[^>]*>([\\S\\s]*?)<\/"+item+">", "img"),"")
    }
  }
  if(row){
    let rowArr = row.split(",")
    for (let item of rowArr){
      newHtml = newHtml.replace(new RegExp(""+item+"\\s*?=\\s*?(['`\"])[\\s\\S]*?\\1", "img"),"")
    }
  }
  return newHtml
}
//替换字符串中指定标签
let checkTag = function(str, oldTag, newTag) { //str字符串， oldTag当前标签数组， newTag替换后的新标签数组
  let newHtml = str
  if(oldTag && newTag){
    let oldArr = oldTag.split(",")
    let newArr = newTag.split(",")
    if(oldArr.length==newArr.length){
      for (let i=0; i<oldArr.length; i++){
        newHtml = newHtml.replace(new RegExp("<"+oldArr[i]+"\\b[^>]*>([\\S\\s]*?)<\\/"+oldArr[i]+">", "img"), "<"+newArr[i]+">$1</"+newArr[i]+">")
      }
    }
  }
  return newHtml
}

let changeTime = function(item){
  let value = Number(item)
  let now = new Date().getTime()
  if( now-value<1000*60*59 ) {
    if( (now-value)/(1000*60) ){
      return Math.ceil((now-value)/(1000*60))+'分钟前'
    }else{
      return '1分钟前'
    }
  }else if(now-value<1000*60*60*23) {
    return Math.ceil((now-value)/(1000*60*60))+'小时前'
  }else {
    let YMD = new Date(value)
    let Y = YMD.getFullYear() + '-';//年
    let M =(YMD.getMonth() + 1 < 10 ? '0' + (YMD.getMonth() + 1) : YMD.getMonth() + 1) + '-';//月
    let D =(YMD.getDate() < 10 ? '0' + YMD.getDate() : YMD.getDate());//日
    return Y+M+D
  }
}

export {
  encodeStr,
  changeTime,
  changePath,
  saveFixed,
  removeTag,
  checkTag
}
