<template>
  <div class="tags-list">
    <el-tabs v-model="cruuentTab" class="main-tabs" closable @tab-remove="closeTags" @tab-click="navTo">
      <el-tab-pane v-for="item in tagsList" :key="item.path" :label="item.title" :name="item.name">
        <template #label>
          <div class="custom-tabs-label">
            <el-icon @click="reload">
              <refresh />
            </el-icon>
            <span class="lable-text">{{ item.title }}</span>
          </div>
        </template>
      </el-tab-pane>
    </el-tabs>
    <el-dropdown class="action-btns" @command="handleTags">
      <el-icon class="el-icon--right">
        <arrow-down />
      </el-icon>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="other">关闭其他</el-dropdown-item>
          <el-dropdown-item command="all">关闭所有</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script setup>
import { inject, onMounted, ref } from "vue";
import { onBeforeRouteUpdate, useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
let tagsList = ref([]);
const cruuentTab = ref("");

const reload = inject("reload");
const $route = useRoute();
const $router = useRouter();
const $store = useStore();
onBeforeRouteUpdate((to) => {
  setTags(to);
});
onMounted(() => {
  setTags($route);
});

const closeTags = (name) => {
  let tags = tagsList.value;
  let index = -1;
  tags.map((item, idx) => {
    if (item.name == name) {
      index = idx;
    }
  });
  tagsList.value.splice(index, 1);
  $store.commit('outTagList', name)
  const item = tags[index] ? tags[index] : tags[index - 1];
  if (item) {
    $router.push(item.path);
  } else {
    $router.push("/");
  }
};
const setTags = (routeObj) => {
  // const isExist = tagsList.value.some((item) => {
  //   return item.name === routeObj.name;
  // });
  let isExist = -1;
  tagsList.value.map((item, index) => {
    if (item.name === routeObj.name) {
      isExist = index;
    }
  });
  if (isExist == -1) {
    const sideList = $store.state.sideList;
    let title = sideList.get(routeObj.name);
    tagsList.value.push({
      title: title || routeObj.meta.title,
      path: routeObj.fullPath,
      name: routeObj.name,
      params: routeObj.query,
      route: routeObj.path,
    });
    cruuentTab.value = routeObj.name;
    $store.commit('inputTagList', routeObj.name)
  } else {
    tagsList.value[isExist].params = routeObj.query;
    cruuentTab.value = routeObj.name;
  }
};
const navTo = (to) => {
  let tags = tagsList.value;
  tags.map((item, idx) => {
    if (item.name == to.paneName) {
      $router.push({ path: item.route, query: item.params });
      cruuentTab.value = item.name;
    }
  });
};
const handleTags = (command) => {
  command === "other" ? closeOther() : closeAll();
};
// 关闭全部标签
const closeAll = () => {
  tagsList.value.map(item => {
    $store.commit('outTagList', item.name)
  })
  tagsList.value = [];
  $router.push("/");
};
// 关闭其他标签
const closeOther = () => {
  const curItem = tagsList.value.filter((item) => {
    return item.path === $route.fullPath;
  });

  tagsList.value.map(item => {
    if (item.name != $route.name) {
      $store.commit('outTagList', item.name)
    }

  })
  tagsList.value = curItem;
};
</script>

<style lang="scss" scoped>
.tags-list {
  align-items: center;
  padding: 0 15px;
  width: 100%;
  box-sizing: border-box;
  display: flex;
  background-color: #ebebeb;

  &:after {
    clear: both;
  }
}

.action-btns {
  width: 3%;

  :deep(.el-icon--right) {
    width: 2em;
    height: 2em;
    background-color: #fff;
    margin-left: 0;
  }
}

.main-tabs {
  padding: 5px 15px;
  box-sizing: border-box;
  width: 97%;

  // float: left;
  :deep(.el-tabs__header) {
    margin-bottom: 0;
    border-bottom: none;

    .el-tabs__nav-prev,
    .el-tabs__nav-next {
      font-size: 16px;
      line-height: 36px;
    }

    .el-tabs__nav-wrap:after {
      background-color: transparent;
    }

    .el-tabs__item {
      background-color: #fff;
      margin-left: 0;
      margin-right: 10px;
      padding: 0 10px;
      height: 32px;
      line-height: 32px;
      // display: flex;
      // align-items: center;
    }

    .el-tabs__active-bar {
      background-color: transparent;
    }

    .custom-tabs-label {
      display: inline-flex;
      align-items: center;

      .lable-text {
        margin-left: 5px;
      }
    }
  }
}
</style>
