<template>
  <div id="tags-view-container" class="tags-view-container">
    <!--<scroll-pane ref="scrollPane" class="tags-view-wrapper" @scroll="handleScroll">
        <el-tag class="tag-router"
                v-for="tag in visitedViews"
                :key="tag.name"
                :effect="tag.fullPath == selectedTag ? 'dark' : 'plain'"
                :closable="!tag.noClose"
                @close="removeTag(tag)"
                @click="tagClick(tag)">
            {{tag.title}}
        </el-tag>
    </scroll-pane>-->
      <el-tag class="tag-router"
              v-for="tag in visitedViews"
              :key="tag.name"
              :effect="tag.fullPath == selectedTag ? 'dark' : 'plain'"
              :closable="!tag.noClose"
              @close="removeTag(tag)"
              @click="tagClick(tag)">
          {{tag.title}}
      </el-tag>

  </div>
</template>

<script>
import ScrollPane from './ScrollPane'

export default {
    components: {ScrollPane},
    data() {
        return {
            selectedTag: '',
            editableTabsValue: ''
        }
    },
    computed: {
        visitedViews() {
            return this.$store.getters.visitedViews
        }
    },
    watch: {
        $route() {
            this.addTags();
        }
    },
    mounted() {
        this.initTags()
        this.addTags()
    },
    methods: {
        initTags() {
            console.log('this.$route', this.$route)
            this.selectedTag = this.$route.fullPath;

        },
        addTags() {
            this.selectedTag = this.$route.fullPath;
            this.$store.dispatch('tagsView/addView', this.$route);
            this.editableTabsValue = this.$route.fullPath;
        },
        tagClick(tag) {
            this.selectedTag = tag.fullPath;
            this.$router.push(tag.fullPath);
        },
        removeTag(tag) {
            if(tag.name === 'home') return;
            let vIndex = -1;
            let view = null;
            this.visitedViews.forEach((v, index) => {
                if(v.fullPath === tag.fullPath) {
                    vIndex = index;
                    view = v;
                }
            });
            if(vIndex != -1) {
                let newIndex = vIndex;
                if(newIndex == this.visitedViews.length - 1) {
                    newIndex = vIndex - 1;
                }
                this.$store.dispatch('tagsView/delView', view);
                this.$router.push(this.visitedViews[newIndex].fullPath);
            }
        },
        handleScroll() {
            this.closeMenu();
        }
    }
}
</script>

<style lang="scss" scoped>
.tags-view-container {
  min-height: 42px;
  width: 100%;
  background: #fff;
  border-bottom: 1px solid #d8dce5;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, .12), 0 0 3px 0 rgba(0, 0, 0, .04);
  overflow-y: auto;
  display: flex;
  flex-wrap: nowrap;
  .tags-view-wrapper {
    .tags-view-item {
      display: inline-block;
      position: relative;
      cursor: pointer;
      height: 26px;
      line-height: 26px;
      border: 1px solid #d8dce5;
      color: #495060;
      background: #fff;
      padding: 0 8px;
      font-size: 12px;
      margin-left: 5px;
      margin-top: 4px;
      &:first-of-type {
        margin-left: 15px;
      }
      &:last-of-type {
        margin-right: 15px;
      }
      &.active {
        background-color: $theme-color;
        color: #fff;
        border-color: $theme-color;
        &::before {
          content: '';
          background: #fff;
          display: inline-block;
          width: 8px;
          height: 8px;
          border-radius: 50%;
          position: relative;
          margin-right: 2px;
        }
      }
    }


  }

  .tag-router {
    cursor: pointer;
    margin-right: 5px;
  }

}
</style>

<style lang="scss">
//reset element css of el-icon-close
.tags-view-wrapper {
  .tags-view-item {
    .el-icon-close {
      width: 16px;
      height: 16px;
      vertical-align: 2px;
      border-radius: 50%;
      text-align: center;
      transition: all .3s cubic-bezier(.645, .045, .355, 1);
      transform-origin: 100% 50%;
      &:before {
        transform: scale(.6);
        display: inline-block;
        vertical-align: -3px;
      }
      &:hover {
        background-color: #b4bccc;
        color: #fff;
      }
    }
  }
}
</style>
