export default {
  methods: {
    // 路由跳转到指定tab
    setActiveTabByRoute() {
      if (!this.$route.query.active) {
        return
      }
      const keys = Object.keys(this.labelList)
      let newActive = this.$route.query.active.toString()

      if (keys.indexOf(newActive) > -1) {
        newActive = newActive.toString()
        this.activeName = newActive
        setTimeout(() => {
          // 解决activeName改变，active_bar没有改变
          const offsetArr = this.getTabOffset()
          let translateX = 'translateX(__OFFSET__px)'
          translateX = translateX.replace('__OFFSET__', (offsetArr[keys.indexOf(newActive)]).toString())
          document.querySelector('.el-tabs__active-bar').style.transform = translateX
        }, 1000)
        this.$router.push(this.$route.path)
      }
    },
    getTabOffset() {
      const arr = [0]
      let total = 0
      Object.keys(this.labelList).forEach((key) => {
        const offsetWidth = document.querySelector('#tab-' + key).offsetWidth
        total += offsetWidth
        arr.push(total)
      })
      arr.pop()
      return arr
    }
  }
}
