<template>
  <el-dropdown trigger="click" :teleported="false">
    <div class="more-button">
      <i :class="'iconfont icon-xiala'" />
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item @click="refresh">
          <el-icon>
            <Refresh />
          </el-icon>
          刷新
        </el-dropdown-item>
        <el-dropdown-item @click="closeCurrentTab">
          <el-icon>
            <Remove />
          </el-icon>
          关闭当前
        </el-dropdown-item>
        <el-dropdown-item @click="tabStore.closeTabsOnSide(route.fullPath, 'left')">
          <el-icon>
            <DArrowLeft />
          </el-icon>
          关闭左侧
        </el-dropdown-item>
        <el-dropdown-item @click="tabStore.closeTabsOnSide(route.fullPath, 'right')">
          <el-icon>
            <DArrowRight />
          </el-icon>
          关闭右侧
        </el-dropdown-item>
        <el-dropdown-item divided @click="closeOtherTab">
          <el-icon>
            <CircleClose />
          </el-icon>
          关闭其他
        </el-dropdown-item>
        <el-dropdown-item @click="closeAllTab">
          <el-icon>
            <FolderDelete />
          </el-icon>
          关闭所有
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup lang="ts">
import { inject, nextTick } from 'vue'
import { HOME_URL } from '@/constants'
import { useTabsStore } from '@/stores/modules/tabs'
import { useKeepAliveStore } from '@/stores/modules/keepAlive'
import { useRoute, useRouter } from 'vue-router'
import { CircleClose, DArrowLeft, DArrowRight, FolderDelete, Remove, Refresh } from '@element-plus/icons-vue'
type refreshFunction = (val: boolean) => void
const route = useRoute()
const router = useRouter()
const tabStore = useTabsStore()
const keepAliveStore = useKeepAliveStore()

// refresh current page
const refreshCurrentPage = inject('refresh') as refreshFunction
const refresh = () => {
  setTimeout(() => {
    keepAliveStore.removeKeepAliveName(route.name as string)
    refreshCurrentPage(false)
    nextTick(() => {
      keepAliveStore.addKeepAliveName(route.name as string)
      refreshCurrentPage(true)
    })
  }, 0)
}

// Close Current
const closeCurrentTab = () => {
  if (route.meta.isAffix) return
  tabStore.removeTabs(route.fullPath)
  keepAliveStore.removeKeepAliveName(route.name as string)
}

// Close Other
const closeOtherTab = () => {
  tabStore.closeMultipleTab(route.fullPath)
}

// Close All
const closeAllTab = () => {
  tabStore.closeMultipleTab()
  router.push(HOME_URL)
}
</script>

<style scoped lang="scss">
@use './index';
</style>
