
<template>
  <div class="tags-view-container">
    <Hamburger class="hamburger-container"></Hamburger>
    <router-link
      @contextmenu.prevent="onContextmenu(tag, $event)"
      class="tags-view-item"
      :class="isActive(tag) ? 'active' : ''"
      v-for="(tag, index) in $store.getters.tagsViewList"
      :key="tag.fullPath"
      :to="{ path: tag.fullPath }"
    >
      <span>{{ tag.title || "个人中心" }}</span>
      <el-icon
        class="el-icon-close"
        @click.prevent.stop="onCloseClick(index, isActive(tag))"
      >
        <close />
      </el-icon>
    </router-link>
    <!-- <span v-if="store.getters.tagsViewList.length > 11">...</span> -->
    <div class="icons">
      <el-dropdown
        @visible-change="checkActive"
        @command="dropRoute"
        trigger="click"
      >
        <span class="el-dropdown-link">
          <el-icon :class="active ? '_icon icon' : 'icon'" color="#24b6dd"
            ><ArrowUpBold
          /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="cxjz">重新加载</el-dropdown-item>
            <el-dropdown-item command="close">关闭其他标签</el-dropdown-item>
            <el-dropdown-item command="closeAll">关闭全部标签</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    <Contextmenu
      ref="contextmenuRef"
      :items="contextmenuItems"
      @contextmenuItemClick="onContextmenuItem"
    />
  </div>
</template>

<script setup>
import Contextmenu from "@/components/contextmenu/index.vue";
import Hamburger from "./Hamburger/index";
import { reactive, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";

const route = useRoute();
const active = ref(false);

/**
 * 是否被选中
 */
const isActive = (tag) => {
  return tag.path === route.path;
};
const contextmenuRef = ref(null);
/**
 * 关闭 tag 的点击事件
 */
const store = useStore();
const router = useRouter();
const onCloseClick = (index, current) => {
  store.commit("app/removeTagsView", {
    type: "index",
    index: index
  });
  if (current === true) {
    if (store.getters.tagsViewList.length === 0) {
      window.location.href = '/'
      // router.push("/");
    } else if (index > 0) {
      router.push(store.getters.tagsViewList[index - 1].fullPath);
    } else {
      router.push(store.getters.tagsViewList[0].fullPath);
    }
  }
};
function checkActive(e) {
  active.value = e;
}
const onContextmenuItem = async (item) => {
  const { name, menu } = item;
  switch (name) {
    case "refresh":
      router.go(0);
      break;
    case "close":
      const index = store.getters.tagsViewList.indexOf(menu);
      if (isActive(menu)) {
        if (store.getters.tagsViewList.length === 1) {
          // router.push("/");
          window.location.href = '/'
        } else if (index > 0) {
          router.push(store.getters.tagsViewList[index - 1].fullPath);
        } else if (index == 0) {
          router.push(store.getters.tagsViewList[1].fullPath || "/");
        }
      }
      store.commit("app/removeTagsView", {
        type: "data",
        data: menu
      });
      break;
    case "closeOther":
      store.commit("app/clearTag", menu);
      router.push(menu.fullPath);
      break;
    case "closeAll":
      store.commit("app/clearTag", null);
      // router.push("/");
      window.location.href = '/'
      break;
    // case "fullScreen":

    //   break;
  }
};
/*生成title */
const getTitle = (route) => {
  let title = "";
  if (!route.meta) {
    const pathArr = route.path.split("/");
    title = pathArr[pathArr.length - 1];
  } else {
    title = route.meta.title;
  }
  return title;
};
function dropRoute(e) {
  switch (e) {
    case "cxjz":
      router.go(0);
      break;
    case "closeAll":
      store.commit("app/clearTag", null);
      // router.push("/");
      window.location.href = '/'
      break;
    case "close":
      const { fullPath, meta, name, params, path, query } = route;
      let menu = {
        fullPath,
        meta,
        name,
        params,
        path,
        query,
        title: getTitle(route)
      };
      store.commit("app/clearTag", menu);
      router.push(menu.fullPath);
      break;
  }
}
const contextmenuItems = reactive([
  { name: "refresh", label: "重新加载", icon: "fa fa-refresh" },
  { name: "close", label: "关闭标签", icon: "fa fa-times" },
  { name: "closeOther", label: "关闭其他标签", icon: "fa fa-minus" },
  { name: "closeAll", label: "关闭全部标签", icon: "fa fa-stop" }
]);
const onContextmenu = (menu, el) => {
  // 禁用刷新
  contextmenuItems[0].disabled = route.path !== menu.path;
  // 禁用关闭其他和关闭全部
  contextmenuItems[3].disabled = contextmenuItems[2].disabled = store.getters.tagsViewList.length == 1 ? true : false;
  const { clientX, clientY } = el;
  contextmenuRef.value.onShowContextmenu(menu, { x: clientX,  y: clientY });
};
</script>

<style lang="scss" scoped>
.icons {
  position: absolute;
  top: 12px;
  right: 6px;
}
.tags-view-container::-webkit-scrollbar {
  height: 0px;
  position: absolute;
}
.tags-view-container {
  width: calc(100vw - 307px);
  height: 36px;
  overflow: hidden;
  overflow-x: scroll;
  overflow-y: hidden;
  white-space: nowrap;
  background: #fff;
  .hamburger-container {
    line-height: 36px;
  }
  .tags-view-item {
    text-decoration: none;
    display: inline-block;
    position: relative;
    cursor: pointer;
    height: 36px;
    line-height: 36px;
    color: #24b6dd;
    font-size: 14px;
    margin-left: 5px;
    margin-right: 28px;
    // display: inline-flex;
    align-items: center;

    span {
      vertical-align: middle;
    }

    i {
      vertical-align: middle;
      position: absolute;
      right: -18px;
      top: 11px;
    }

    &:first-of-type {
      margin-left: 8px;
    }

    &:last-of-type {
      margin-right: 15px;
    }

    &.active {
      color: #61f9ff;
      border-bottom: 2px solid #61f9ff;
      height: 34px;
      font-weight: bold;
      &::before {
        content: "";
        width: 0px;
        height: 0px;
        font-size: 0px;
        position: absolute;
        border-left: 6px solid transparent;
        border-right: 6px solid transparent;
        border-top: 6px solid transparent;
        border-bottom: 6px solid #61f9ff;
        bottom: -1px;
        left: 50%;
        margin-left: -6px;
      }
    }

    // close 按钮
    .el-icon-close {
      width: 16px;
      height: 16px;
      line-height: 10px;
      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;
      }
    }
  }
}
.icon._icon {
  transform: rotateZ(180deg);
  transition: all 0.2s linear;
}
.icon {
  transform: rotateZ(0deg);
  transition: all 0.2s linear;
}
::v-deep .el-popper.is-light,
::v-deep .el-popper.is-light .el-popper__arrow::before {
  background: #0d2944;
  border: 1px solid #03438b;
}
::v-deep .el-dropdown-menu {
  background-color: transparent;
  .el-dropdown-menu__item {
    color: #fff;
    &:hover {
      color: #61f9ff;
      background: transparent;
    }
  }
}
</style>