<script setup>
import { ref, watch, onMounted, computed, inject } from "vue";
import { useRoute, useRouter } from "vue-router";

import { HomeFilled, RemoveFilled, Delete, Refresh } from "@element-plus/icons-vue";

import { useTabsStore } from "@/store/tabs";
import { storeToRefs } from "pinia";

const router = useRouter();
const route = useRoute();

const tabsStore = useTabsStore();
const { visitedViews } = storeToRefs(tabsStore);

const reload = inject("reload");

const activeTab = computed({
  get: () => {
    return tabsStore.activeTab;
  },
  set: (val) => {
    tabsStore.setActiveTab(val);
  },
});

onMounted(() => {
  addTab();
});

watch(route, () => {
  addTab();
});

const handleClick = (tab) => {
  router.push(tab.props.name);
};

const handleRemove = (item) => {
  if(visitedViews.value.length === 1){
    router.push('/');
  }else{
    // 如果關閉的是當前激活的tab，則激活前一個tab
    if (item.path === activeTab.value) {
      //取得當前的tab的index
      const index = visitedViews.value.findIndex((tab) => tab.path === item.path);

      const prevTab = visitedViews.value[index - 1];
      activeTab.value = prevTab.path;
      router.push(prevTab.path);
    }
  }
  tabsStore.removeTab(item);
};

const handleCloseOther = () => {
  tabsStore.closeOtherTabs();
};

const handleCloseAll = () => {
  tabsStore.closeAllTabs();
};

const addTab = () => {
  if (route.path === "/login" || route.path === "/news/list/form" || route.path === "/product/list/form" || route.path === "/news/esg/form") return;
  if (route.path) {
    tabsStore.addTab({
      path: route.path,
      title: route.meta.title,
    });
  }
};
</script>

<template>
  <div class="main-tabs-view">
    <div>
      <el-tabs v-model="activeTab" type="card" @tab-click="handleClick">
        <el-tab-pane name="/home">
          <template #label>
            <div class="tab-content">
              <el-icon><HomeFilled /></el-icon>
            </div>
          </template>
        </el-tab-pane>
        <el-tab-pane :name="item.path" v-for="item in visitedViews">
          <template #label>
            <div class="tab-content">
              <el-icon @click.stop="reload"><Refresh /></el-icon>
              {{ item.title }}
              <el-icon @click.stop="handleRemove(item)"><RemoveFilled /></el-icon>
            </div>
          </template>
        </el-tab-pane>
      </el-tabs>
    </div>
    <div>
      <el-dropdown>
        <span class="el-dropdown-link">
          <el-icon><Delete /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="handleCloseOther">關閉其他</el-dropdown-item>
            <el-dropdown-item @click="handleCloseAll">關閉全部</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<style lang="less" scoped>
.main-tabs-view {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  div:nth-child(1) {
    flex: 1 0 0%;
    overflow: hidden;
    overflow-x: auto;   
  }
  div:nth-child(2) {
    // text-align: right;
    flex:0 0 auto;
  }
}

:deep(.el-tabs) {
  background-color: #f0f2f5;
}

:deep(.el-tabs .el-tabs__nav) {
  // margin-top: 5px;
  background-color: #fff;
}
:deep(.el-tabs .el-tabs__header) {
  margin: 0 !important;
}

// :deep(.el-tabs .el-tabs__header .el-tabs__item) {
//   border: none;
//   margin: 0 0 5px !important;
//   height: 50px;
//   color: #606266;
// }

:deep(.el-tabs .el-tabs__header .el-tabs__item.is-active) {
  border-bottom: 2px solid #409eff;
  color: #409eff;
}

// .el-tabs__content {
//   display: none;
// }
</style>
