<template>
  <div
    class="tags-view-container"
    ref="container"
  >
    <scroll-pane
      class="tags-view-wrapper"
      ref="scrollPane"
    >
      <router-link
        ref="tag"
        class="tags-view-item"
        :class="isActive(tag) ? 'active' : ''"
        v-for="tag in Array.from(visitedViews)"
        :to="tag.path"
        :key="tag.path"
        @contextmenu.prevent.native="openMenu(tag,$event)"
      >
        {{ tag.title }}
        <span
          class="el-icon-close"
          @click.prevent.stop="closeSelectedTag(tag)"
        ></span>
      </router-link>
    </scroll-pane>
    <ul
      class="contextmenu"
      v-show="visible"
      :style="{left:left+'px',top:top+'px'}"
    >
      <li @click="closeSelectedTag(selectedTag)">
        关闭
      </li>
      <li @click="closeOthersTags">
        关闭其他
      </li>
      <li @click="closeAllTags">
        关闭全部
      </li>
    </ul>
  </div>
</template>

<script>
import ScrollPane from '@/components/ScrollPane'
import { EventBus } from '@/utils/eventBus'

export default {
  components: { ScrollPane },
  data () {
    return {
      visible: false,
      top: 0,
      left: 0,
      selectedTag: {}
    }
  },
  computed: {
    visitedViews () {
      return this.$store.state.tagsView.visitedViews
    }
  },
  watch: {
    $route () {
      this.addViewTags()
      this.moveToCurrentTag()
    },
    visible (value) {
      if (value) {
        document.body.addEventListener('click', this.closeMenu)
      } else {
        document.body.removeEventListener('click', this.closeMenu)
      }
    }
  },
  mounted () {
    this.addViewTags()
  },
  methods: {
    generateRoute () {
      if (this.$route.name) {
        return this.$route
      }
      return false
    },
    isActive (route) {
      return route.path === this.$route.path
    },
    addViewTags () {
      const route = this.generateRoute()
      if (!route) {
        return false
      }
      this.$store.dispatch('addVisitedViews', route)
    },
    moveToCurrentTag () {
      const tags = this.$refs.tag
      this.$nextTick(() => {
        for (const tag of tags) {
          if (tag.to === this.$route.path) {
            this.$refs.scrollPane.moveToTarget(tag.$el)
            break
          }
        }
      })
    },
    closeSelectedTag (view) {
      this.$store.dispatch('delVisitedViews', view).then((views) => {
        if (this.isActive(view)) {
          const latestView = views.slice(-1)[0]
          if (latestView) {
            this.$router.push(latestView.path)
          } else {
            this.$router.push({ name: 'dashboard' })
          }
        }
      })
    },
    closeOthersTags () {
      this.$router.push(this.selectedTag.path)
      this.$store.dispatch('delOthersViews', this.selectedTag).then(() => {
        this.moveToCurrentTag()
      })
    },
    closeAllTags () {
      this.$store.dispatch('delAllViews')
      this.$router.push({ name: 'dashboard' })
    },
    openMenu (tag, e) {
      this.visible = true
      this.selectedTag = tag
      let offset = this.$refs.container.getBoundingClientRect().x
      // console.log(offset)
      this.left = e.clientX - offset
      this.top = e.clientY

      EventBus.$emit('activeTag', tag)
    },
    closeMenu () {
      this.visible = false
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.tags-view-container {
  .tags-view-wrapper {
    height: 40px;
    background: #F8F9FD;
    border: 1px solid #E4E6ED;
    box-shadow: 6px 0px 12px 0px #DCDFE6;
    &:before {
      position: absolute;
      bottom: 0; left: 0;
      z-index: 3;
      width: 100%;
      content: '';
      border-bottom: 1 solid #E4E6ED;
    }
    .tags-view-item {
      position: relative;
      z-index: 1;
      display: inline-block;
      height: 40px;
      line-height: 40px;
      padding: 0 20px;
      margin-left: -1px;
      font-size: 16px;
      color: #555555;
      border-top-left-radius: 5px;
      border-top-right-radius: 5px;
      transition: background .5s;
      &:before,&:after {
        position: absolute;
        top: 0;
        right: 0;
        width: 1px ;
        height: 40px;
        content: '';
        background: #F8F9FD;
      }
      &:after {
        left: 0;
      }

      &:last-child {
        &:after{
          display: none;
        }
      }
      &:hover {
        background: darken($gray,5%);
        color: $blue;
        z-index: 2;
        &:before,&:after {
          display: none;
        }
      }

      &.active {
        z-index: 4;
        color: $blue;
        background-color: #E4E6ED;
        &:before,&:after {
          display: none;
        }
        // &::before {
        //   position: relative;  display: inline-block;  width: 8px;  height: 8px;  margin-right: 2px;  content: '';  background: #fff;  border-radius: 50%;
        // }
      }
    }
    .tags-view-item:first-child {
      margin-left: 15px;
      &:before{
        display: none;
      }
    }

    ::v-deep .el-icon-close {
      width: 16px;
      height: 16px;
      text-align: center;
      vertical-align: 0;
      border-radius: 50%;
      transition: all .3s cubic-bezier(.645, .045, .355, 1);
      transform-origin: 100% 50%;
      &:before {
        display: inline-block;
        vertical-align: -1px;
        transform: scale(.6);
      }
      &:hover {
        color: #fff;
        background-color: #b4bccc;
        box-shadow: 0 0 5px #fff;
      }
    }
  }
  .contextmenu {
    position: absolute;
    z-index: 100;
    padding: 5px 0;
    margin: 0;
    font-size: 12px;
    font-weight: 400;
    color: #333;
    list-style-type: none;
    background: #fff;
    border-radius: 4px;
    box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, .3);
    li {
      padding: 7px 16px;  margin: 0;  cursor: pointer;
      &:hover {
        background: #eee;
      }
    }
  }
}

</style>
