// 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))
}

export {
  encodeStr,
  changePath,
  saveFixed
}
