<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.path"
      :to="{ 'path': tag.path }">
      <span>{{ tag.title || "个人中心" }}</span>
      <!-- v-show="!isActive(tag)" -->
      <el-icon v-if="tag.title!='工作台'" class="el-icon-close" :class="isActive(tag) ? 'activexx' : ''"
        @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="var(--el-color-primary)">
            <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 "@/components/Hamburger/index";
import { reactive, ref, onMounted } 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();
/**
 * 关闭 tag 的点击事件
 */
const store = useStore();
const router = useRouter();

// 关闭当前页面
const onCloseClick = (index, current) => {
  if (store.getters.tagsViewList.length == 1) return;//必须保留最后一个路由
  if (store.getters.tagsViewList[index].fullPath == '/dutyBook/home/workbench') {
    return
  } else {
    store.commit("app/removeTagsView", {
      type: "index",
      index: index
    });
    if (current === true) {
      if (store.getters.tagsViewList.length === 0) {
        router.push("/dutyBook/home/workbench");
      } 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 (menu.path == '/dutyBook/home/workbench') {
        return
      } else {
        if (isActive(menu)) {
          if (store.getters.tagsViewList.length === 1) {
            router.push("/");
          } 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);

      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", menu);
      store.commit("app/clearTag", null);
      router.push('/dutyBook/home/workbench');
      // router.push("/editPassword");
      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: "fullScreen", label: "当前标签全屏", icon: "el-icon-FullScreen" },
  { 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 = true
  const { clientX, clientY } = el;
  contextmenuRef.value.onShowContextmenu(menu, {
    x: clientX,
    y: clientY
  });
};

</script>

<style lang="scss" scoped>
.icons {
  position: absolute;
  top: 17px;
  right: 20px;
}

.tags-view-container::-webkit-scrollbar {
  height: 0px;
  position: absolute;
}

.tags-view-container {
  // width: calc(100vw - 300px);
  height: 47px;
  overflow-x: scroll;
  overflow-y: hidden;
  white-space: nowrap;
  background-color: #fff;
  // border-bottom: 1px solid var(--el-color-primary);

  .hamburger-container {
    line-height: 47px;
    width: 47px;
    text-align: center;
    background-color: var(--el-table-cell-bmx-backgroundcolor) !important;
    // background-color: #ccebe1;
  }

  .tags-view-item {
    text-decoration: none;
    display: inline-block;
    position: relative;
    cursor: pointer;
    height: 47px;
    line-height: 47px;
    color: #000;
    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: rgb(32, 197, 76);
      color: var(--el-color-primary);
      // border-bottom: 2px solid rgb(32, 197, 76);
      // height: 28px;
      font-weight: bold;
      height: 28px;
      line-height: 28px;
      padding: 0 21px;
      // background: var(--el-table-cell-bmx-backgroundcolor);
      border-radius: 61px 61px 61px 61px;
      opacity: 1;

      // background: linear-gradient(180deg, #89f7fe 100%, #66a6ff 100%);
      // -webkit-background-clip: text;
      // -webkit-text-fill-color: transparent;
      &::before {
        content: "";
        width: 60%;
        height: 2px;
        position: absolute;
        background-color: var(--el-color-primary);
        // border-left: 6px solid transparent;
        // border-right: 6px solid transparent;
        // border-top: 6px solid transparent;
        // border-bottom: 1px solid var(--el-color-primary);
        bottom: -8px;
        left: 21%;
        // margin-left: -6px;
      }

      i {
        right: 3px !important;
        top: 7px !important;
      }
    }

    // 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;
}
</style>
