<template>
  <div class="avue-tags" v-if="setting.tag" @click="contextmenuFlag = false">
    <!-- tag盒子 -->
    <div v-if="contextmenuFlag" class="avue-tags__contentmenu" :style="{ left: contentmenuX + 'px', top: contentmenuY + 'px' }">
      <div class="item" @click="closeOthersTags">{{ $t('tagsView.closeOthers') }}</div>
      <div class="item" @click="closeAllTags">{{ $t('tagsView.closeAll') }}</div>
    </div>
    <div class="avue-tags__box">
      <el-tabs v-model="active" type="card" @contextmenu.native="handleContextmenu" :closable="tagLen !== 1" @tab-click="openTag" @edit="menuTag">
        <el-tab-pane v-for="item in tagList" :key="item.value" :label="item.label" :name="item.value">
          <span slot="label">
            {{ item.label }}
          </span>
        </el-tab-pane>
      </el-tabs>

      <el-dropdown class="avue-tags__menu">
        <el-button type="primary" size="mini">
          {{ $t('tagsView.menu') }}
          <i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <!-- <el-dropdown-item @click.native="$parent.isSearch = true">{{ $t('tagsView.search') }}</el-dropdown-item> -->
          <el-dropdown-item @click.native="closeOthersTags">{{ $t('tagsView.closeOthers') }}</el-dropdown-item>
          <!-- <el-dropdown-item @click.native="closeAllTags">{{ $t('tagsView.closeAll') }}</el-dropdown-item> -->
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
  name: 'tags',
  data() {
    return {
      refresh: false,
      active: '',
      contentmenuX: '',
      contentmenuY: '',
      contextmenuFlag: false
    }
  },
  watch: {
    tag: {
      handler(val) {
        // 默认删除首页这个标签
        this.tagList.forEach((item, index) => {
          if (item.name === '首页' || item.label === '首页') {
            this.tagList.splice(index, 1)
          }
        })
        // this.active = val.fullPath
        this.setActive()
      },
      immediate: true
    },
    contextmenuFlag() {
      window.addEventListener('mousedown', this.watchContextmenu)
    }
  },
  mounted() {
    this.setActive()
  },
  created() {
    // this.tagList.splice(0, 1)
  },
  computed: {
    ...mapGetters(['tagWel', 'tagList', 'tag', 'setting']),
    tagLen() {
      return this.tagList.length || 0
    }
  },
  methods: {
    handleRefresh() {
      this.refresh = true
      this.$store.commit('SET_IS_REFRESH', false)
      setTimeout(() => {
        this.$store.commit('SET_IS_REFRESH', true)
      }, 100)
      setTimeout(() => {
        this.refresh = false
      }, 500)
    },
    generateTitle(item) {
      return this.$router.$avueRouter.generateTitle({
        ...item,
        ...{
          label: item.name
        }
      })
    },
    watchContextmenu(event) {
      if (!this.$el.contains(event.target) || event.button !== 0) {
        this.contextmenuFlag = false
      }
      window.removeEventListener('mousedown', this.watchContextmenu)
    },
    handleContextmenu(event) {
      let target = event.target
      let flag = false
      if (target.className.indexOf('el-tabs__item') > -1) flag = true
      else if (target.parentNode.className.indexOf('el-tabs__item') > -1) {
        target = target.parentNode
        flag = true
      }
      if (flag) {
        event.preventDefault()
        event.stopPropagation()
        this.contentmenuX = event.clientX
        this.contentmenuY = event.clientY
        this.tagName = target.getAttribute('aria-controls').slice(5)
        this.contextmenuFlag = true
      }
    },
    // 激活当前选项
    setActive() {
      this.active = this.tag.value
    },
    menuTag(value, action) {
      if (action === 'remove') {
        let { tag, key } = this.findTag(value)
        this.$store.commit('DEL_TAG', tag)
        if (tag.value === this.tag.value) {
          tag = this.tagList[key === 0 ? key : key - 1] // 如果关闭本标签让前推一个
          this.openTag(tag)
        }

        // if (tag.fullPath === this.tag.fullPath) {
        //   tag = this.tagList[key - 1]
        //   this.$router.push({
        //     path: tag.path,
        //     query: tag.query
        //   })
        // }
      }
    },
    openTag(item) {
      let tag
      if (item.name) {
        tag = this.findTag(item.name).tag
      } else {
        tag = item
      }
      this.$router
        .push({
          path: this.$router.$avueRouter.getPath({
            name: tag.label,
            src: tag.value
          }),
          query: tag.query
        })
        .catch(() => {})
    },
    findTag(value) {
      let tag, key
      this.tagList.map((item, index) => {
        if (item.value === value) {
          tag = item
          key = index
        }
      })
      return { tag: tag, key: key }
    },
    closeOthersTags() {
      this.contextmenuFlag = false
      this.$store.commit('DEL_TAG_OTHER')
    },
    closeAllTags() {
      this.contextmenuFlag = false
      this.$store.commit('DEL_ALL_TAG')
      // console.log(this.tagWel)
      this.$router.push(this.tagWel.path)
    }
  }
}
</script>
