<template>
  <div class="tags-view-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"
        :key="tag.path"
        @contextmenu.prevent.native="openMenu(tag, $event)"
      >
        {{ generateTitle(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)">{{ $t("tagsView.close") }}</li>
      <li @click="closeOthersTags">{{ $t("tagsView.closeOthers") }}</li>
      <li @click="closeAllTags">{{ $t("tagsView.closeAll") }}</li>
    </ul>-->
    <div class="tags-view-wrapper">
      <div style="height:100%">
        <div style="height:100%" v-if="newIndex == 10">
          <div class="tagsBox" v-if="routerList.length > 0 && newNum != 999">
            <div
              class="tagsOne"
              v-if="routerList[newIndex].meta"
              v-for="(item, i) in routerList[newIndex].children"
              :key="i"
            >
              <router-link :to="item.path" v-if="item.num ? item.num == newNum : false">
                <p
                  class="newTags"
                  :class="activeClass == i ? 'active' : ''"
                  @click="onClick(i)"
                >{{ generateTitle(item.meta.title) }}</p>
              </router-link>
            </div>
          </div>
          <div class="tagsBox" v-if="newNum == 999">
            <div
              class="tagsOne"
              v-if="permission_routers[newIndex].meta"
              v-for="(item, i) in permission_routers[newIndex].children"
              :key="i"
            >
              <router-link :to="item.path" v-if="item.num ? item.num == newNum : false">
                <p
                  class="newTags"
                  :class="activeClass == i ? 'active' : ''"
                  @click="onClick(i)"
                >{{ generateTitle(item.meta.title) }}</p>
              </router-link>
            </div>
          </div>
        </div>
        <div class="tagsBox" v-if="newIndex != 10">
          <div
            class="tagsOne"
            v-if="permission_routers[newIndex].meta"
            v-for="(item, i) in permission_routers[newIndex].children"
            :key="i"
          >
            <router-link :to="item.path" v-if="!item.hidden && !item.is_menu">
              <p
                class="newTags"
                :class="activeClass == i ? 'active' : ''"
                @click="onClick(i)"
              >{{ generateTitle(item.meta.title) }}</p>
            </router-link>
          </div>
          <div v-else class="tagsOne">
            <p class="newTags" :class="activeClass == i ? 'active' : ''" @click="onClick(i)">
              {{
              generateTitle(permission_routers[newIndex].children[0].meta.title)
              }}
            </p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import ScrollPane from "@/components/ScrollPane";
import { generateTitle } from "@/utils/i18n";
//
import { mapGetters } from "vuex";

export default {
  props: {
    index: Number,
    num: Number
  },
  components: { ScrollPane },
  data() {
    return {
      visible: false,
      top: 0,
      left: 0,
      selectedTag: {},
      newIndex: 0,
      activeClass: 0,
      newNum: 1
    };
  },
  computed: {
    // visitedViews() {
    //   return this.$store.state.tagsView.visitedViews;
    // },
    ...mapGetters(["permission_routers"])
  },
  watch: {
    // $route() {
    //   this.addViewTags();
    //   this.moveToCurrentTag();
    // },
    // visible(value) {
    //   if (value) {
    //     document.body.addEventListener("click", this.closeMenu);
    //   } else {
    //     document.body.removeEventListener("click", this.closeMenu);
    //   }
    // },
    index(newVal) {
      if (this.newIndex != newVal) {
        this.activeClass = 0;
      }
      this.newIndex = this.index;
    },
    num(newVal) {
      this.newNum = newVal;
      if (newVal == 999) {
        this.routerList = [];
      } else {
        this.routerList = this.permission_routers;
      }
      if (newVal == 1) {
        this.activeClass = 2;
      }
      if (newVal == 2) {
        this.activeClass = 6;
      }
    }
  },
  created() {
    this.newIndex = 3
    this.routerList = this.permission_routers;
  },
  mounted() {
    // this.addViewTags();
  },
  methods: {
    generateTitle, // generateTitle by vue-i18n
    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.path === 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);
          } else {
            this.$router.push("/");
          }
        }
      });
    },
    closeOthersTags() {
      this.$router.push(this.selectedTag);
      this.$store.dispatch("delOthersViews", this.selectedTag).then(() => {
        this.moveToCurrentTag();
      });
    },
    closeAllTags() {
      this.$store.dispatch("delAllViews");
      this.$router.push("/");
    },
    openMenu(tag, e) {
      this.visible = true;
      this.selectedTag = tag;
      const offsetLeft = this.$el.getBoundingClientRect().left; // container margin left
      this.left = e.clientX - offsetLeft + 15; // 15: margin right
      this.top = e.clientY;
    },
    closeMenu() {
      this.visible = false;
    },
    onClick(index) {
      console.log(index);
      this.activeClass = index;
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.tags-view-container {
  height: 60px;
  .tags-view-wrapper {
    background: #fff;
    height: 50px;
    border-bottom: 1px solid #d8dce5;
    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.12), 0 0 3px 0 rgba(0, 0, 0, 0.04);
    position: fixed;
    z-index: 1000;
    top: 50px;
    overflow-x: scroll;
    white-space: normal;
    // right: 0;
    // left: 150px;
    width: 100%;

    /* 针对缺省样式 (必须的) */
    &::-webkit-scrollbar {
      width: 6px;
      height: 2px;
    }
    /* 滚动条的滑轨背景颜色 */
    &::-webkit-scrollbar-track {
      background-color: #fff;
      border-radius: 3px;
      -moz-border-radius: 3px;
      -webkit-border-radius: 3px;
    }
    /* 滑块颜色 */
    &::-webkit-scrollbar-thumb {
      background: #fff;
      border-radius: 3px;
      -moz-border-radius: 3px;
      -webkit-border-radius: 3px;
    }
    /*内层轨道的颜色*/
    &::-webkit-scrollbar-track-piece {
      background-color: #fff;
      border-radius: 3px;
      -moz-border-radius: 3px;
      -webkit-border-radius: 3px;
    }
    /* 滑轨两头的监听按钮颜色 */
    &::-webkit-scrollbar-button {
      background-color: #fff;
      width: 0;
      height: 0;
    }
    /* 横向滚动条和纵向滚动条相交处尖角的颜色 */
    &::-webkit-scrollbar-corner {
      background-color: #fff;
    }
    .tags-view-item {
      display: inline-block;
      position: relative;
      border: 1px solid #d8dce5;
      color: #495060;
      background: #fff;
      padding: 0 4px 0 8px;
      font-size: 12px;
      margin: 12px 0 12px 5px;
      border-radius: 4px;
      height: 28px;
      line-height: 28px;
      &:first-of-type {
        margin-left: 15px;
      }
      &.active {
        background-color: #409efe;
        color: #fff;
        border-color: #409efe;
        &::before {
          content: "";
          background: #fff;
          display: inline-block;
          width: 8px;
          height: 8px;
          border-radius: 50%;
          position: relative;
          margin-right: 2px;
        }
      }
    }
  }
  .contextmenu {
    margin: 0;
    background: #fff;
    z-index: 100;
    position: absolute;
    list-style-type: none;
    padding: 5px 0;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 400;
    color: #333;
    box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.3);
    li {
      margin: 0;
      padding: 7px 16px;
      cursor: pointer;
      &:hover {
        background: #eee;
      }
    }
  }
  .tagsBox {
    display: flex;
    align-items: center;
    height: 100%;
    .tagsOne {
      .newTags {
        border: 1px solid rgba(235, 238, 240, 1);
        border-radius: 2px;
        padding: 8px 12px;
        margin-left: 10px;
        font-size: 14px;
      }
    }
    .active {
      border: 1px solid rgba(64, 158, 255, 1);
      background: rgba(64, 158, 255, 1);
      border-radius: 2px;
      color: #fff;
    }
  }
}
.scroll-wrapper {
  right: 0;
  position: fixed !important;
}
</style>

<style rel="stylesheet/scss" 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 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
      transform-origin: 100% 50%;
      &:before {
        transform: scale(0.6);
        display: inline-block;
        vertical-align: -3px;
      }
      &:hover {
        background-color: #b4bccc;
        color: #fff;
      }
    }
  }
}
</style>
