<template>
  <div id="tabs-container" class="tabs-container">
    <el-tabs v-model="tabActive" class="tabs-content" type="card" @tab-click="handleTabClick" @tab-remove="handleTabRemove">
      <el-tab-pane v-for="item in visitedRoutes" :key="item.path" :closable="!isAffix(item)" :name="item.path">
        <template slot="label">
          <vab-icon v-if="getTabIcon(item)" :icon="['fas', getTabIcon(item)]" class="tab-icon" />
          <span>{{ item.meta.title }}</span>
        </template>
      </el-tab-pane>
    </el-tabs>

    <el-dropdown  v-if="false">
      <div @mouseenter="handleMouseEnter">
        <i class="el-icon-message-solid el-icon--right"></i>
      <span style="cursor: pointer;" >
        消息
      </span>
      <span style="color:red;">({{messageList.length + total }})</span>
      </div>
      <el-dropdown-menu slot="dropdown" class="tabs-more">
        <div style="display: flex;justify-content: center;margin-top:20px;">
       <h3 class="message">
           消息通知
     </h3>
      <span style="color:red;">({{ messageList.length + total }})</span>
        </div>

          <div class="allmark" @click="allRead" v-if="messageList.length + total != 0 ">
        <p class="y">全部已读</p>
           <!-- <img class="gg" alt="" src="@/assets/g.png"  /> -->
      </div>
          <div class="order" v-for="(item, index) in list"
            :key="item.id">
      <div :class="[ markIndex2 == index? ' order-item' : 'order-item2'  ]" @mouseenter="marklist2(item,index)" @mouseleave="handleMouseLeave">
        <div class="tick">
            <div class="time">
       {{item.created_at}}
      </div>
        <div class="mark" v-if="markIndex2 == index" @click="markRead(item)">
        <p class="y">标记已读</p>
           <img class="gg" alt="" src="@/assets/g.png"  />
      </div>
        </div>
      <div class="od-item2">
       {{item.content}}
      </div>
      </div>
     </div>
     <div class="order" v-for="(item, index) in messageList"
            :key="index">
      <div :class="[ markIndex == index? ' order-item' : 'order-item2'  ]" @mouseenter="marklist(item,index)" @mouseleave="handleMouseLeave2">
        <div class="tick">
            <div class="time">
       {{item.date}}
      </div>
        <div class="mark" v-if="markIndex == index" @click="markRead2(item)">
        <p class="y">标记已读</p>
           <img class="gg" alt="" src="@/assets/g.png"  />
      </div>
        </div>
      <div class="od-item2">
       {{item.content}}
      </div>
      </div>
     </div>
        <!-- <el-dropdown-item command="closeOtherstabs">
          <vab-icon :icon="['fas', 'times-circle']" />
          关闭其他
        </el-dropdown-item>
        <el-dropdown-item command="closeLefttabs">
          <vab-icon :icon="['fas', 'arrow-alt-circle-left']"></vab-icon>
          关闭左侧
        </el-dropdown-item>
        <el-dropdown-item command="closeRighttabs">
          <vab-icon :icon="['fas', 'arrow-alt-circle-right']"></vab-icon>
          关闭右侧
        </el-dropdown-item>
        <el-dropdown-item command="closeAlltabs">
          <vab-icon :icon="['fas', 'ban']"></vab-icon>
          关闭全部
        </el-dropdown-item> -->
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>

<script>
  import path from 'path'
  import { mapGetters } from 'vuex'
  import { unreadOtice,markNotice } from '@/api/table'
  import { recordRoute } from '@/config'

  

  export default {
    name: 'VabTabs',
    data() {
      return {
        list:[],
        total:'',
        affixtabs: [],
        tabActive: '',
        markIndex:-1,
        markIndex2:-1,
        mark:false,
         messages: [],
      ws: null,
      messageList:[]
      }
    },

    computed: {
      ...mapGetters({
        visitedRoutes: 'tabsBar/visitedRoutes',
        routes: 'routes/routes',
      }),
    },
    watch: {
      $route: {
        handler(route) {
          this.inittabs()
          this.addtabs()
          let tabActive = ''
          this.visitedRoutes.forEach((item, index) => {
            if (item.path === this.$route.path) {
              tabActive = item.path
            }
          })
          this.tabActive = tabActive
        },
        immediate: true,
      },
    },
    created(){
//  this.connect();
//  this.fetchData()
 
    },
    mounted() {
      // console.log(111)
     setTimeout(() => {
     this.$store.dispatch('user/logout')
     const fullPath = this.$route.fullPath
     this.$router.push(`/login?redirect=${fullPath}`)
    },
     86400000);
    },
    methods: {
        async fetchData() {
        this.listLoading = true
        const { data, total } = await unreadOtice(this.queryForm)
        this.list = data
        // console.log( this.list,'--')
        this.total = total
        this.timeOutID = setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
         connect() {
      this.ws = new WebSocket('wss://dev.wagoo.pet/wagoo/1.1/ws/admin'); // 确保使用wss（加密的WebSocket）或ws（非加密的WebSocket）
      this.ws.onopen = this.onOpen;
      this.ws.onmessage = this.onMessage;
      this.ws.onerror = this.onError;
      this.ws.onclose = this.onClose;
    },
    onOpen() {
      console.log('已经链接到服务');
      setTimeout(() => {
         this.ws.send('Hello Server!');
        }, 500)
    },
    onMessage(event) {
      // console.log(event,'--')
      this.messageList.push(JSON.parse(event.data))
       this.messageList.reverse()
      // console.log( this.messageList,'什么东东')
      // console.log( this.messageList,'???')
      // const message = JSON.parse(event.data);
      // this.messages.push(message); // 更新消息列表
    },
    onError(event) {
      console.error('WebSocket连接出错:', event);
    },
    onClose(event) {
      console.log('WebSocket连接已关闭', event);

      this.connect()
      // 可以在这里重新连接，例如使用setTimeout或setInterval
    },
    disconnect() {
      if (this.ws) {
        this.ws.close();
      }
    },
  beforeDestroy() {
    this.disconnect(); // 组件销毁时关闭WebSocket连接
  },
async  markRead(it){
  const queryForm = {
    id:it.id
  }
   const res = await markNotice(queryForm)
   if(res.code === 200){
    this.fetchData()
   }
    // console.log(it,'??')

  },

   async  allRead(){
     this.messageList = []
     const queryForm = {
    id:'0'
  }
      const res = await markNotice(queryForm)
       if(res.code === 200){
    this.fetchData()
   }
  },
  async  markRead2(it){
    console.log(it,'?')
  const queryForm = {
    id:it.id
  }
   const res = await markNotice(queryForm)
    if(res.code === 200){
    this.fetchData()
   }
    // console.log(it,'??')

  },
      marklist(item,index){
        this.markIndex = index
//  console.log( item);
      },
       marklist2(item,index){
        this.markIndex2 = index
//  console.log( item);
      },
      handleMouseLeave2(){
 this.markIndex = -1
      },
      handleMouseLeave(){
        // console.log(111)
  this.markIndex2 = -1
      },
     handleMouseEnter(event) {
      // this.fetchData()
      // console.log('鼠标移入事件被触发', event);
      // 在这里编写你的处理代码
    },
      async handleTabRemove(tabActive) {
        let view
        this.visitedRoutes.forEach((item, index) => {
          if (tabActive == item.path) {
            view = item
          }
        })
        const { visitedRoutes } = await this.$store.dispatch('tabsBar/delRoute', view)
        if (this.isActive(view)) {
          this.toLastTag(visitedRoutes, view)
        }
      },
      handleTabClick(tab) {
        const route = this.visitedRoutes.filter((item, index) => {
          if (tab.index == index) return item
        })[0]
        if (this.$route.path !== route.path) {
          this.$router.push({
            path: route.path,
            query: route.query,
            fullPath: route.fullPath,
          })
        } else {
          return false
        }
      },
      isActive(route) {
        return route.path === this.$route.path
      },
      isAffix(tag) {
        return tag.meta && tag.meta.affix
      },
      filterAffixtabs(routes, basePath = '/') {
        let tabs = []
        routes.forEach((route) => {
          if (route.meta && route.meta.affix) {
            const tagPath = path.resolve(basePath, route.path)
            tabs.push({
              fullPath: tagPath,
              path: tagPath,
              name: route.name,
              meta: { ...route.meta },
            })
          }
          if (route.children) {
            const temptabs = this.filterAffixtabs(route.children, route.path)
            if (temptabs.length >= 1) {
              tabs = [...tabs, ...temptabs]
            }
          }
        })
        return tabs
      },
      inittabs() {
        const affixtabs = (this.affixtabs = this.filterAffixtabs(this.routes))
        for (const tag of affixtabs) {
          if (tag.name) {
            this.$store.dispatch('tabsBar/addVisitedRoute', tag)
          }
        }
      },
      addtabs() {
        const { name } = this.$route
        if (name) {
          this.$store.dispatch('tabsBar/addVisitedRoute', this.$route)
        }
        return false
      },
      handleCommand(command) {
        switch (command) {
          case 'refreshRoute':
            this.refreshRoute()
            break
          case 'closeOtherstabs':
            this.closeOtherstabs()
            break
          case 'closeLefttabs':
            this.closeLefttabs()
            break
          case 'closeRighttabs':
            this.closeRighttabs()
            break
          case 'closeAlltabs':
            this.closeAlltabs()
            break
        }
      },
      async refreshRoute() {
        this.$baseEventBus.$emit('reloadrouter-view')
      },
      async closeSelectedTag(view) {
        const { visitedRoutes } = await this.$store.dispatch('tabsBar/delRoute', view)
        if (this.isActive(view)) {
          this.toLastTag(visitedRoutes, view)
        }
      },
      async closeOtherstabs() {
        const view = await this.toThisTag()
        await this.$store.dispatch('tabsBar/delOthersRoutes', view)
      },
      async closeLefttabs() {
        const view = await this.toThisTag()
        await this.$store.dispatch('tabsBar/delLeftRoutes', view)
      },
      async closeRighttabs() {
        const view = await this.toThisTag()
        await this.$store.dispatch('tabsBar/delRightRoutes', view)
      },
      async closeAlltabs() {
        const view = await this.toThisTag()
        const { visitedRoutes } = await this.$store.dispatch('tabsBar/delAllRoutes')
        if (this.affixtabs.some((tag) => tag.path === view.path)) {
          return
        }
        this.toLastTag(visitedRoutes, view)
      },
      toLastTag(visitedRoutes, view) {
        const latestView = visitedRoutes.slice(-1)[0]
        if (latestView) {
          this.$router.push(latestView)
        } else {
          this.$router.push('/')
        }
      },
      async toThisTag() {
        const view = this.visitedRoutes.filter((item, index) => {
          if (item.path === this.$route.fullPath) {
            return item
          }
        })[0]
        if (this.$route.path !== view.path) this.$router.push(view)
        return view
      },
      getTabIcon(item) {
        // 如果当前路由有图标，直接返回
        if (item.meta && item.meta.icon) {
          return item.meta.icon
        }

        // 查找父级路由的图标
        const parentIcon = this.findParentIcon(item.path)
        if (parentIcon) {
          return parentIcon
        }

        return null
      },

      findParentIcon(path) {
        // 从路径中提取父级路径
        const pathParts = path.split('/').filter((part) => part)
        if (pathParts.length <= 1) {
          return null
        }

        // 查找父级路由
        const findRouteByPath = (routes, targetPath) => {
          for (const route of routes) {
            if (route.path === targetPath) {
              return route
            }
            if (route.children) {
              const found = findRouteByPath(route.children, targetPath)
              if (found) return found
            }
          }
          return null
        }

        // 尝试查找父级路径
        for (let i = pathParts.length - 1; i > 0; i--) {
          const parentPath = '/' + pathParts.slice(0, i).join('/')
          const parentRoute = findRouteByPath(this.routes, parentPath)
          if (parentRoute && parentRoute.meta && parentRoute.meta.icon) {
            return parentRoute.meta.icon
          }
        }

        return null
      },
    },
  }
</script>

<style lang="scss" scoped>
  .tabs-container {
    position: relative;
    box-sizing: border-box;
    display: flex;
    align-content: center;
    align-items: center;
    justify-content: space-between;
    height: 54px;
    padding-right: $base-padding;
    padding-left: $base-padding;
    user-select: none;
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border-top: 1px solid rgba(255, 255, 255, 0.3);
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06), inset 0 1px 0 rgba(255, 255, 255, 0.6);

    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: linear-gradient(135deg, rgba(255, 255, 255, 0.4) 0%, rgba(255, 255, 255, 0.1) 50%, rgba(255, 255, 255, 0.2) 100%);
      pointer-events: none;
    }

    ::v-deep {
      .fold-unfold {
        margin-right: $base-padding;
      }
    }

    .tabs-content {
      position: relative;
      width: calc(100% - 90px);
      height: $base-tag-item-height;

      ::v-deep {
        .el-tabs__nav-next,
        .el-tabs__nav-prev {
          height: $base-tag-item-height;
          line-height: $base-tag-item-height;
          color: rgba(0, 0, 0, 0.6);
          background: rgba(255, 255, 255, 0.6);
          border: 1px solid rgba(255, 255, 255, 0.8);
          border-radius: 12px;
          transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
          backdrop-filter: blur(10px);
          -webkit-backdrop-filter: blur(10px);

          &:hover {
            color: rgba(0, 0, 0, 0.8);
            background: rgba(255, 255, 255, 0.8);
            border-color: rgba(255, 255, 255, 1);
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1), 0 2px 4px rgba(0, 0, 0, 0.05);
          }
        }

        .el-tabs__header {
          border-bottom: 0;

          .el-tabs__nav {
            border: 0;
          }

          .el-tabs__item {
            position: relative;
            box-sizing: border-box;
            height: $base-tag-item-height;
            margin-right: 8px;
            margin-top: 3px;
            padding: 0 20px;
            line-height: $base-tag-item-height;
            border: 1px solid $base-color-default;
            color: $base-color-default;
            border-radius: 5px;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
            overflow: hidden;

            .tab-icon {
              margin-right: 6px;
              font-size: 12px;
            }

            &::before {
              content: '';
              position: absolute;
              top: 0;
              left: -100%;
              width: 100%;
              height: 100%;
              background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
              transition: left 0.6s cubic-bezier(0.4, 0, 0.2, 1);
            }

            &:hover {
              
              color: rgba(255, 255, 255, 0.95);
              background: $base-color-default;
              border-color: $base-color-default;

              &::before {
                left: 100%;
              }
            }

            &.is-active {
              color: rgba(255, 255, 255, 0.95);
              background: $base-color-default;
              border-color: $base-color-default;
            }

            .el-icon-close {
              position: relative;
              margin-left: 8px;
              font-size: 12px;
              color: rgba(255, 255, 255, 0.7);
              transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
              border-radius: 5px;
              padding: 2px;

              &:hover {
                color: rgba(255, 255, 255, 0.9);
                background: rgba(255, 255, 255, 0.2);
                transform: scale(1.2);
              }
            }
          }
        }
      }
    }

    .more {
      position: relative;
      display: flex;
      align-content: center;
      align-items: center;
      padding: 8px 16px;
      color: rgba(0, 0, 0, 0.7);
      background: rgba(255, 255, 255, 0.6);
      border: 1px solid rgba(255, 255, 255, 0.8);
      border-radius: 16px;
      cursor: pointer;
      transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
      backdrop-filter: blur(10px);
      -webkit-backdrop-filter: blur(10px);

      &:hover {
        color: rgba(0, 0, 0, 0.9);
        background: rgba(255, 255, 255, 0.8);
        border-color: rgba(255, 255, 255, 1);
        transform: translateY(-1px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1), 0 2px 4px rgba(0, 0, 0, 0.05);
      }

      &:active {
        transform: translateY(0);
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1), 0 1px 2px rgba(0, 0, 0, 0.05);
      }
    }
  }
</style>

<style lang="scss">
  .tabs-more {
    border-radius:15px;
      width: 550px;
      height: 550px;
      position: relative;
       overflow: auto;
          .allmark{
          font-size: 12px;
          padding: 0 10px;
          position: absolute;
          right: 10px;
          top: 20px;
          // width: 60px;
          height: 30px;
        border: 1px solid #ccc;
        font-weight: 600;
        border-radius: 30px;
        display: flex;
        justify-content: center;
        align-items: center;
        .gg{
          width: 15px;
          height: 15px;
        }
        }
      .message{
        display: flex;
        justify-content: center;
        margin: 0;
        // display: inline-block;
      }
      .order{
        padding:0 20px 0 20px;
       
        .od-item{
          color:#9B939A;
          margin-top: 30px;

        }
        .order-item{
          border-radius: 5px;
          margin-top: 10px;
          background: #fbf9f9;
          height: 50px;
          padding: 5px;
          position: relative;
          .tick{
            display: flex;
            align-items: center;
            justify-content: space-between;
            .mark{
              position: absolute;
              right: 10px;
              top: 10px;
          font-size: 12px;
          padding: 0 10px;
          // width: 60px;
          height: 30px;
        border: 1px solid #ccc;
        font-weight: 600;
        border-radius: 30px;
        display: flex;
        justify-content: center;
        align-items: center;
        .gg{
          width: 15px;
          height: 15px;
          margin-left: 4px;
        }
        }
          .time{
          display: flex;
          justify-content: center;
          font-size: 12px;
           color:#887a86;
        }

          }
         
        .od-item2{
          color:#353535;
          margin-top: 10px;
        }
        

        }
         .order-item2{
          margin-top: 10px;
          height: 50px;
          padding: 5px;
          position: relative;
          .tick{
            display: flex;
            align-items: center;
            justify-content: space-between;
            .mark{
          font-size: 12px;
          padding: 0 10px;
          position: absolute;
          right: 10px;
          top: 10px;
          // width: 60px;
          height: 30px;
        border: 1px solid #ccc;
        font-weight: 600;
        border-radius: 30px;
        display: flex;
        justify-content: center;
        align-items: center;
        .gg{
          width: 15px;
          height: 15px;
        }
        }
          .time{
          display: flex;
          justify-content: center;
          font-size: 12px;
           color:#887a86;
        }

          }
         
        .od-item2{
          color:#353535;
          margin-top: 10px;
        }
        

        }
       
      }
   
    .el-dropdown-menu__item {
      padding: 8px 20px;
      font-size: 14px;
      line-height: 1.5;

      &:not(:last-child) {
        margin-bottom: 4px;
      }

      .vab-icon {
        margin-right: 8px;
        width: 16px;
        text-align: center;
      }

      &:hover {
        background: rgba(77, 138, 240, 0.1);
        color: #4d8af0;
        border-radius: 6px;
      }
    }
  }
</style>
