<template>
  <div>
    <div class="tags" v-if="showTags>0">
      <div v-if="isShow">
        <!-- {{copyList}} -->
        <el-tag size="mini" class="tags-mini" v-for="(item,index) in tagsList" :class="{'active': isActive(item.path)}" :key="index">
          <router-link :to="item.path" class="tagsTitle">{{item.title}}</router-link>
          <span class="tagsIcon" @click="closeTags(index)">
            <i class="el-icon-close"></i>
          </span>
        </el-tag>
      </div>
      <div class="tags-close-box">
        <el-dropdown @command="handleTags">
          <el-button size="mini" type="primary" plain>
            标签选项<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu size="small" slot="dropdown">
            <el-dropdown-item command="other">关闭其他</el-dropdown-item>
            <el-dropdown-item command="all">关闭所有</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <div class="padding-top padding-left padding-right">
      <transition name="el-fade-in-linear" mode="out-in">
        <keep-alive>
          <router-view />
        </keep-alive>
      </transition>
    </div>
  </div>
</template>

<script>
import app from "../../style/app.css";
export default {
  data() {
    return {
      tagsList: [],
      copyList: [],
      isShow: true,
      showTags: -1,
      list: [], //主数组
    };
  },
  methods: {
    isActive(path) {
      return path === this.$route.fullPath;
    },
    // 关闭单个标签
    closeTags(index) {
      const delItem = this.tagsList.splice(index, 1)[0];
      const item = this.tagsList[index]
        ? this.tagsList[index]
        : this.tagsList[index - 1];
      if (item) {
        delItem.path === this.$route.fullPath && this.$router.push(item.path);
      } else {
        this.$router.push("/home");
      }
    },
    // 关闭全部标签
    closeAll() {
      this.$router.push("/home");
    },
    // 关闭其他标签
    closeOther() {
      const curItem = this.tagsList.filter((item) => {
        return item.path === this.$route.fullPath;
      });
      this.tagsList = curItem;
    },
    // 设置标签
    setTags(route) {
      if (route.meta.title == "服务单-收银") {
        this.tagsList.forEach((item, index) => {
          if (item.title == "服务单-收银") {
            this.tagsList.splice(index, 1, {
              title: route.meta.title,
              path: route.path,
            });
          }
        });
      }
      this.isShow = false;
      const isExist = this.tagsList.some((item) => {
        return item.title === route.meta.title;
      });
      //isExist false:不存在 需要添加 true:存在 需要切换
      if (!isExist) {
        if (this.tagsList.length > 8) {
          this.tagsList.shift();
        } else {
          this.tagsList.push({
            title: route.meta.title,
            path: route.path,
          });
        }
      }
      setTimeout(() => {
        this.showTags = this.tagsList.length;
      }, 100);
      console.log(this.tagsList);
      this.copyList = JSON.parse(JSON.stringify(this.tagsList));
      this.isShow = true;

      if (route.meta.title != "促销购买") {
        this.tagsList.forEach((item, index) => {
          if (item.title == "促销购买") {
            console.log(index);
            this.tagsList.splice(index, 1);
          }
        });
      }

      if (!sessionStorage.getItem("list") && route.path != "/login") {
        this.$message.error({
          message: "没有操作权限!",
        });
        setTimeout(() => {
          this.$router.push("/");
          sessionStorage.clear();
          localStorage.clear();
        }, 2000);
      }
    },
    handleTags(command) {
      command === "other" ? this.closeOther() : this.closeAll();
    },
  },
  computed: {},
  watch: {
    $route(newValue, oldValue) {
      setTimeout(() => {
        this.setTags(newValue);
        return;
      }, 100);
    },
  },

  created() {
    this.setTags(this.$route);
    // 监听关闭当前页面的标签页
  },
};
</script>

<style scoped>
.tags {
  overflow: hidden;
  padding-left: 20px;
  display: flex;
  justify-content: space-between;
  border: 1px solid #eef8ff;
  background: #cbe9ff;
  position: sticky;
  top: 0;
  padding-top: 5px;
  padding-bottom: 5px;
  z-index: 99;
  border-bottom-left-radius: 3px;
  border-bottom-right-radius: 3px;
  margin: 0px 5px;
}
.tags-mini {
  font-size: 12px;
  overflow: hidden;
  cursor: pointer;
  height: 25px;
  line-height: 25px;
  padding-left: 13px;
  padding-right: 10px;
  border-radius: 4px;
  margin-right: 5px;
}
.tags .el-dropdown {
  margin-right: 10px;
}

.tags-mini:not(.active):hover {
  background: linear-gradient(#237fe0, #5facfd) !important;

  color: white;
}

.tags-mini.active {
  color: black;
  background: linear-gradient(#237fe0, #5facfd) !important;
}

.tagsTitle {
  float: left;
  max-width: 100px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  margin-right: 4px;
  color: white;
  text-decoration: none;
  font-family: "宋体";
}
.tags-mini.active .tagsTitle {
  color: #fff;
}
.el-button--primary:focus,
.el-button--primary:hover {
  background: linear-gradient(#1873d4, #51a5ff);
}

.el-dropdown-menu__item:hover {
  background: linear-gradient(#1873d4, #51a5ff) !important;

  color: #ffffff !important;
}
.tags-mini {
  background: #71b6ff;

  color: white;
}
</style>
