<template>
  <div class="home-tabs">
    <!-- 标签页头部 -->
    <div class="tabs-header">
      <div 
        v-for="tab in tabs" 
        :key="tab.id"
        class="tab-item"
        :class="{ 'active': activeTabId === tab.id }"
        @click="switchTab(tab.id)"
      >
        <span class="tab-title">{{ tab.title }}</span>
        <button 
          class="tab-close"
          @click.stop="closeTab(tab.id)"
          :disabled="tab.id === 'mainindex'"
        >
          ×
        </button>
      </div>
      <div class="tab-add" @click="addTab">
        +
      </div>
    </div>
    
    <!-- 标签页内容 -->
    <div class="tabs-content">
      <router-view v-slot="{ Component }">
        <transition name="fade" mode="out-in">
          <component :is="Component" />
        </transition>
      </router-view>
    </div>
  </div>
</template>

<script>
export default {
  name: 'HomeTabs',
  data() {
    return {
      // 标签页列表
      tabs: [
        {
          id: 'mainindex',
          title: '首页',
          path: '/mainindex'
        }
      ],
      // 当前激活的标签页ID
      activeTabId: 'mainindex'
    }
  },
  mounted() {
    // 初始化标签页
    this.initTabs()
    
    // 监听路由变化
    this.$router.beforeEach((to, from, next) => {
      this.handleRouteChange(to)
      next()
    })
  },
  methods: {
    // 初始化标签页
    initTabs() {
      // 从本地存储加载标签页状态
      const savedTabs = localStorage.getItem('homeTabs')
      const savedActiveTabId = localStorage.getItem('activeTabId')
      
      if (savedTabs) {
        this.tabs = JSON.parse(savedTabs)
      }
      
      if (savedActiveTabId) {
        this.activeTabId = savedActiveTabId
      }
    },
    
    // 保存标签页状态
    saveTabsState() {
      localStorage.setItem('homeTabs', JSON.stringify(this.tabs))
      localStorage.setItem('activeTabId', this.activeTabId)
    },
    
    // 处理路由变化
    handleRouteChange(to) {
      const path = to.path.replace('/', '')
      const tabId = path || 'mainindex'
      
      // 检查标签页是否已存在
      const existingTab = this.tabs.find(tab => tab.id === tabId)
      
      if (!existingTab) {
        // 添加新标签页
        this.addTabFromRoute(to)
      }
      
      // 激活当前标签页
      this.activeTabId = tabId
      this.saveTabsState()
    },
    
    // 从路由添加标签页
    addTabFromRoute(route) {
      const path = route.path.replace('/', '')
      const tabId = path || 'mainindex'
      
      // 根据路由路径生成标签页标题
      let title = '新页面'
      switch (path) {
        case 'mainindex':
          title = '首页'
          break
        case 'mynotice':
          title = '我的通知'
          break
        case 'announcement':
          title = '公告管理'
          break
        case 'role':
          title = '角色管理'
          break
        case 'user':
          title = '用户管理'
          break
      }
      
      // 添加新标签页
      this.tabs.push({
        id: tabId,
        title: title,
        path: route.path
      })
      
      this.saveTabsState()
    },
    
    // 切换标签页
    switchTab(tabId) {
      this.activeTabId = tabId
      
      // 查找标签页对应的路由路径
      const tab = this.tabs.find(t => t.id === tabId)
      if (tab) {
        this.$router.push(tab.path)
      }
      
      this.saveTabsState()
    },
    
    // 关闭标签页
    closeTab(tabId) {
      // 不允许关闭首页标签页
      if (tabId === 'mainindex') {
        return
      }
      
      const index = this.tabs.findIndex(tab => tab.id === tabId)
      if (index !== -1) {
        // 从标签页列表中移除
        this.tabs.splice(index, 1)
        
        // 如果关闭的是当前激活的标签页，切换到前一个标签页
        if (this.activeTabId === tabId) {
          const newActiveTab = this.tabs[this.tabs.length - 1]
          this.switchTab(newActiveTab.id)
        }
        
        this.saveTabsState()
      }
    },
    
    // 添加标签页
    addTab() {
      // 这里可以打开一个对话框，让用户选择要添加的页面
      // 暂时默认添加我的通知页面
      this.$router.push('/mynotice')
    }
  }
}
</script>

<style scoped>
.home-tabs {
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* 标签页头部样式 */
.tabs-header {
  display: flex;
  align-items: center;
  background-color: #f8fafc;
  border-bottom: 1px solid #e2e8f0;
  padding: 0 16px;
  height: 48px;
  overflow-x: auto;
  white-space: nowrap;
}

.tabs-header::-webkit-scrollbar {
  height: 4px;
}

.tabs-header::-webkit-scrollbar-track {
  background: #f1f5f9;
}

.tabs-header::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 2px;
}

.tabs-header::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

/* 标签页项样式 */
.tab-item {
  display: flex;
  align-items: center;
  padding: 0 16px;
  height: 36px;
  margin-right: 8px;
  background-color: #ffffff;
  border: 1px solid #e2e8f0;
  border-radius: 6px 6px 0 0;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  top: 1px;
}

.tab-item:hover {
  background-color: #f0f9ff;
  border-color: #bae6fd;
}

.tab-item.active {
  background-color: #ffffff;
  border-bottom-color: #ffffff;
  box-shadow: 0 2px 0 #3b82f6;
}

.tab-title {
  font-size: 14px;
  color: #64748b;
  margin-right: 8px;
}

.tab-item.active .tab-title {
  color: #3b82f6;
  font-weight: 500;
}

/* 标签页关闭按钮 */
.tab-close {
  background: none;
  border: none;
  font-size: 16px;
  color: #94a3b8;
  cursor: pointer;
  padding: 0;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.3s ease;
}

.tab-close:hover {
  background-color: #ef4444;
  color: #ffffff;
}

.tab-close:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.tab-close:disabled:hover {
  background-color: transparent;
  color: #94a3b8;
}

/* 添加标签页按钮 */
.tab-add {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  margin-left: 8px;
  background-color: #ffffff;
  border: 1px dashed #cbd5e1;
  border-radius: 6px;
  cursor: pointer;
  font-size: 18px;
  color: #94a3b8;
  transition: all 0.3s ease;
}

.tab-add:hover {
  border-color: #3b82f6;
  color: #3b82f6;
  background-color: #f0f9ff;
}

/* 标签页内容样式 */
.tabs-content {
  flex: 1;
  overflow: auto;
  padding: 20px;
  background-color: #ffffff;
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .tabs-header {
    padding: 0 8px;
  }
  
  .tab-item {
    padding: 0 12px;
    margin-right: 4px;
  }
  
  .tab-title {
    font-size: 13px;
  }
  
  .tabs-content {
    padding: 16px;
  }
}
</style>