<template>
  <div class="page-header">
    <div class="header-left">
      <!-- <img :src="logoImg" /> -->
       <h1>恒星管理</h1>
      <div class="header-nav">
        <!--        <a :class="{cur:navIdx==-1}" href="/seller">工作台</a>-->
        <a :class="{ cur: navIdx == idx }" v-for="(item, idx) in nav" :href="item.url" :key="idx" @click="onNav(idx)">{{ item.label }}</a>
      </div>
    </div>
    <div class="header-right">
      <img class="user-ava" :src="user.avatar ? user.avatar : '/assets/seller/images/tx.png'" />
      <span class="user-name">{{ user.nick_name }}</span>
      <div @click="logoutClick">
        <a><img :src="logoutIcon" /></a>
      </div>
   
    </div>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
var popup
export default {
  name: 'PageHeader',
  data() {
    return {
      unreadCount: 0,
      showAppCenter: false, // 是否显示应用中心

    }
  },
  computed: {
    logoImg() {
      return process.env.BASE_URL + 'static/images/logo.png'
    },
    logoutIcon() {
      return process.env.BASE_URL + 'static/images/icon_layout.png'
    },
    tipsImg() {
      return process.env.BASE_URL + 'static/images/icon_appcenter_tips.png'
    },

    ...mapState({
      nav: (state) => state.navMenu.list,
      user: (state) => state.user.info,
      navIdx: (state) => state.navMenu.navIdx,
    }),
    ...mapGetters({
      localUrl: 'navMenu/localUrl',
    }),
    shopType(){
      return this.user.shop_type
    },

  },

  watch: {
    $route(to, from) {
      let { action } = to.meta
      let idx = this.getNavIdx(this.nav, process.env.BASE_URL + to.href, action)
      this.onNav(idx)
    },
    nav(n) {
      let { action } = this.$route.meta
      let idx = this.getNavIdx(n, this.localUrl, action)
      console.log('查询出来的导航：', idx)
      this.onNav(idx)
    }
  },
  mounted() {

    window.addEventListener('message',  (e)=>{
      if(e.data.type== 'im') {
        this.unreadCount = e.data.num
        console.log(this.unreadCount)
        console.log(e.data, '这里能收到嘛？')
      }
    })
  
    this.$api.pub.getMenuList().then((res) => {
      this.$store.commit('navMenu/init', res.data)
      // this.$store.commit('navMenu/menu', res.data)
      
    
    })
    this.$api.user.loginUserInfo().then((res) => {
      this.$store.commit('user/init', res.data)
    })
  },
  methods: {
    logoutClick() {
      document.setCookie("token",'',0)
      window.location.href="/#/login";
      
      // popup.postMessage({ type: 'layOut' }, '/')
    },
    // goChat() {
    //   popup = window.open('/bs/#/chat')
    // },
    onNav(idx) {
      this.$store.commit('navMenu/setNavIdx', idx)
    },
    getNavIdx(arr, fullPath, action) {
      let idx = -1
      for (let i = 0; i < arr.length; i++) {
        let item = arr[i]
       
        if (action && item.action && action == item.action) {
         
          idx = i
          break
        }
          // console.log(fullPath,item.url,'-------')
        if (fullPath.toLowerCase() === item.url.toLowerCase()) {
          idx = i
          break
        } else if (Array.isArray(item.children) && item.children.length > 0) {
          let cIdx = this.getNavIdx(item.children, fullPath)
          if (cIdx !== -1) {
            idx = i
            break
          }
        }
      }
      return idx
    },
    dropdownItemClick(item) {
      const {
          isDownload,
          url
      } = item
      if (!url) return;
      if (isDownload) {
          window.open(url, '_self') // 兼容下载路径为http协议 下载失败问题
      } else {
          window.open(url)
      }
    },
  },
}
</script>

<style lang="less" scoped>
.ry-box {
  display: flex;
  align-items: center;
  position: relative;
  margin-right: 50px;
  .icon {
    width: 22px;
    height: 20px;
    margin-right: 8px;
  }
  .txt {
    font-size: 16px;
    color: #333333;
    line-height: 18px;
  }
  .dw {
    position: absolute;
    top: -14px;
    right: -18px;
    width: 43px;
    height: 18px;
  }
}
.page-header,
.header-left,
.header-nav,
.header-right {
  display: flex;
  align-items: center;
  height: 80px;
}
.header-right {
  cursor: pointer;
}
.page-header {
  z-index: 999;
  width: calc(100% - 81px);
  top: 0;
  position: fixed;
  justify-content: space-between;
  background: #fff;
  box-shadow: 0px 2px 5px 0px rgb(0 0 0 / 10%);
  padding: 0 40px;
  font-weight: bold;
  font: "PingFang SC","微软雅黑","Microsoft Yahei",miui,system-ui,-apple-system,BlinkMacSystemFont,Helvetica Neue,Helvetica,sans-serif
}

.header-left {
}

.header-nav {
  margin-left: 42px;
  height: 100%;
}

.header-nav a {
  margin: 0 8px;
  font-size: 16px;
  font-weight: bold;
  color: #2c2b31;
  text-decoration: none;
  height: 100%;
  line-height: 80px;
}

.header-nav a.cur {
  border-bottom: 3px solid #50a0fc;
  color: #5075fc !important;
}

.user-ava {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  margin-right: 12px;
}

.user-name {
  font-size: 16px;
  font-weight: bold;
  margin-right: 12px;
}


.open-chat {
  position: fixed;
  top: 50vh;
  right: 0;
  width: 60px;
  height: 60px;
  background: linear-gradient(270deg, #7AB0FC 0%, #579DFF 100%);
  box-shadow: 0px 0px 20px 0px rgba(92, 159, 254, 0.2000);
  border-radius: 4px;
  backdrop-filter: blur(10px);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: #fff;
  font-size: 12px;
  z-index: 9999;
  .icon{
    width: 22px;
    height: 20px;
  }
}

.open-chat span {
  margin-top: 3px;
}

.triangle {
  display: inline-block;
  width: 0;
  height: 0;
  border-width: 6px;
  border-style: solid;
  border-color: #333 transparent transparent;
  vertical-align: bottom;
  margin-left: 3px;
  margin-right:12px
}

.el-dropdown {
  color: #2C2B31;
  display: block;
}

.font16{
  font-size: 16px;
  font-weight: bold;
}

.el-dropdown-menu__item{
  display:block;
}

.el-dropdown:hover {
  color: #5075FC;
}

.el-dropdown:hover .triangle {
  border-color: #5075fc transparent transparent;
}

.el-dropdown-menu.app-box{
  margin: 0;
}

.app-box {
  min-width: 248px;
  background: #ffffff;
  border: 1px solid #ececec;
  box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.11);
  padding: 10px 10px;
}

.app-box-item {
  padding: 8px 0px;
  line-height: normal;
}

.app-box-item-son {
  padding: 8px 10px;
}

.app-box-item+.app-box-item {
  border-top: 1px solid #ececec;
}

.app-box-item:hover {
  background-color: #fff !important;
}

.app-box-item:hover .app-box-item-son {
  border-radius: 4px;
  background: rgba(205, 205, 205, 0.21) !important;
}

.app-box-item h3 {
  font-weight: bold;
  font-size: 13px;
  color: #333;
  line-height: 18px;
}

.app-box-item p {
  font-size: 12px;
  color: #666;
  white-space: nowrap;
  line-height: 17px;
}
</style>
