<script setup lang="ts">
import { watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Close, RefreshRight } from '@element-plus/icons-vue'
import { useTabsStore } from '@/stores/modules/tabs'

const route = useRoute()
const router = useRouter()
const tabsStore = useTabsStore()

// 路由变化时自动添加 tab
watch(
  () => route.path,
  () => {
    tabsStore.addTab(route)
  },
  { immediate: true }
)

function handleTabClick(path: string) {
  if (path === route.path) return
  tabsStore.setActive(path)
  router.push(path)
}

function handleClose(e: MouseEvent, path: string) {
  e.stopPropagation()
  const nextPath = tabsStore.closeTab(path, route.path)
  if (nextPath !== route.path) {
    router.push(nextPath)
  }
}

/**
 * 刷新当前页 — 通过自定义事件通知 BasicLayout 重新挂载 <router-view>
 * 不使用 router.replace({ path: '/redirect' + ... })，因为我们没有注册 /redirect/* 路由
 * 会触发通配符跳到 /404
 */
function handleRefresh() {
  window.dispatchEvent(new CustomEvent('app:refresh-view'))
}

function handleCloseOthers() {
  tabsStore.closeOthers(route.path)
}

function handleCloseRight() {
  tabsStore.closeRight(route.path)
}

function handleCloseAll() {
  tabsStore.closeAll()
  router.push('/dashboard')
}
</script>

<template>
  <div class="tabs-nav">
    <div class="tabs-scroll">
      <div
        v-for="tab in tabsStore.tabs"
        :key="tab.path"
        :class="['tab-item', { active: tab.path === tabsStore.activeTab }]"
        @click="handleTabClick(tab.path)"
      >
        <span v-if="tab.path === tabsStore.activeTab" class="tab-dot" />
        <span class="tab-title">{{ tab.title }}</span>
        <el-icon
          v-if="tab.path !== '/dashboard'"
          class="tab-close"
          @click="handleClose($event, tab.path)"
        >
          <Close />
        </el-icon>
      </div>
    </div>

    <div class="tabs-actions">
      <el-tooltip content="刷新当前页" placement="bottom">
        <el-button text size="small" :icon="RefreshRight" @click="handleRefresh" />
      </el-tooltip>
      <el-dropdown trigger="click">
        <el-button text size="small">
          <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <polyline points="6 9 12 15 18 9" />
          </svg>
        </el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="handleCloseOthers">关闭其他</el-dropdown-item>
            <el-dropdown-item @click="handleCloseRight">关闭右侧</el-dropdown-item>
            <el-dropdown-item divided @click="handleCloseAll">关闭所有</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.tabs-nav {
  display: flex;
  align-items: center;
  height: 40px;
  background: #fff;
  border-bottom: 1px solid $color-border-light;
  padding: 0 4px 0 0;
  flex-shrink: 0;
}

.tabs-scroll {
  flex: 1;
  display: flex;
  align-items: center;
  overflow-x: auto;
  padding: 0 8px;
  gap: 4px;
  scrollbar-width: none;

  &::-webkit-scrollbar {
    display: none;
  }
}

.tab-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 28px;
  padding: 0 10px;
  border-radius: 6px;
  font-size: 13px;
  color: $color-text-secondary;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.2s;
  border: 1px solid transparent;
  flex-shrink: 0;

  &:hover {
    color: $brand-primary;
    background: rgba(59, 130, 246, 0.06);
  }

  &.active {
    color: $brand-primary;
    background: rgba(30, 64, 175, 0.08);
    border-color: rgba(30, 64, 175, 0.15);
    font-weight: 500;
  }
}

.tab-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: $brand-primary;
  flex-shrink: 0;
}

.tab-title {
  line-height: 1;
}

.tab-close {
  font-size: 12px;
  color: $color-text-placeholder;
  border-radius: 3px;
  padding: 1px;
  transition: all 0.15s;

  &:hover {
    color: $color-danger;
    background: rgba(239, 68, 68, 0.1);
  }
}

.tabs-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  padding-right: 8px;
  border-left: 1px solid $color-border-light;
  padding-left: 8px;
  flex-shrink: 0;
  color: $color-text-secondary;
}
</style>
