<template>
  <div class="navbar">
    <div class="left-box">
      <img src="../../assets/change_img/top_logo.png" @click="jump_home">
      <h1 class="top-title">自定义表单服务平台</h1>
    </div>
    <div class="right-menu">
      <!-- <ul class="list-item">
        <li
          v-for="(item, index) in listArr"
          :key="index"
          :class="{ active: clickIndex == index }"
          @click="openPage(item, index)"
        >
          {{ item.name }}
        </li>
      </ul> -->
      <z-dropdown class="avatar-container">
        <a class="ant-dropdown-link">
          <img :src="avatarUrl" class="user-avatar">
          <span class="user-name">{{ userName }}</span>
          <DownOutlined />
        </a>
        <template #overlay>
          <z-menu>
            <z-menu-item>
              <span style="display: block" @click="logout">退出登录</span>
            </z-menu-item>
          </z-menu>
        </template>
      </z-dropdown>
    </div>
  </div>
</template>

<script>
// import VueBus from '@/utils/temp-vue'
import { getPersonalInfo } from '@/api/personal-center/personal-center'
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      baseApi: process.env.VUE_APP_BASE_API,
      userName: '',
      avatarUrl: require('@/assets/avatar.png'),
      listArr: [
        {
          name: '首页',
          url: window.location.origin + '/#/home'
        }
      ],
      clickIndex: 0
    }
  },
  computed: {
    ...mapGetters(['sidebar'])
  },
  mounted() {
    this.init()
  },
  beforeUnmount() {
    // VueBus.$off('avatarOid')
  },
  methods: {
    // 返回首页
    jump_home() {
      this.$router.push('/')
    },
    openPage(item, index) {
      this.clickIndex = index
      window.location.href = item.url
    },
    init() {
      this.getPersonalInfo()
      // VueBus.$on('avatarOid', (res) => {
      //   if (res) {
      //     this.avatarUrl = this.baseApi + '/oss/api/download/' + res
      //   }
      // })
    },

    async getPersonalInfo() {
      const res = await getPersonalInfo()
      if (res.data) {
        this.userName = res.data.userName
      }
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      location.reload()
    }
  }
}
</script>

<style lang="less">
.ant-dropdown-menu {
  li {
    text-align: center;
  }
}
.avatar-container {
  height: 100%;
  display: inline-block;
  margin-right: 30px;
  .user-avatar {
    cursor: pointer;
    width: 48px;
    height: 48px;
    border-radius: 50%;
  }
  .user-name {
    font-size: 16px;
    cursor: pointer;
    margin: 0 20px 0 16px;
    color: #2a2c2c;
  }
  .anticon {
    cursor: pointer;
    font-size: 16px;
    // color: #c4c4c4;
  }
}

.navbar {
  width: 100%;
  height: 80px;
  overflow: hidden;
  position: fixed;
  background: #fff;
  z-index: 1001;
  padding-left: 30px;
  box-shadow: 0px 4px 20px 0px rgba(91, 105, 135, 0.1);

  .left-box {
    height: 80px;
    float: left;

    img {
      margin-top: 10px;
      transition: transform 0.5s;
      cursor: pointer;
      &:hover {
        transform: scale(1.1,1.2);
      }
    }

    .top-title {
      font-size: 24px;
      color: #2a2c2c;
      margin: 0 0 0 20px;
      float: right;
      line-height: 80px;
    }
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 80px;

    &:focus {
      outline: none;
    }

    .list-item {
      float: left;
      margin-right: 60px;
      font-size: 16px;

      li {
        height: 80px;
        cursor: pointer;
        float: left;
        padding: 0 10px;
        margin: 0 10px;
        color: #2a2c2c;
        font-weight: bold;
        &:hover {
          color: #2956f2;
        }
      }

      .active {
        color: #2956f2;
        position: relative;
        &::after {
          content: "";
          display: block;
          height: 6px;
          width: 100%;
          background-color: #2956f2;
          border-radius: 2px;
          position: absolute;
          bottom: 0;
          left: 0;
        }
      }
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #2a2c2c;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background 0.3s;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }
  }
}
.a-dropdown-menu__item {
  line-height: 30px;
}
</style>
