<template>
  <div class="header">
    <div class="container">
      <h2>公众文化平台</h2>
      <div class="box">
        <el-menu
          :default-active="this.$route.path"
          router
          class="el-menu-demo"
          mode="horizontal"
          @select="handleSelect"
        >
          <el-menu-item index="/index">首页</el-menu-item>
          <el-menu-item index="/zhishi">文化小知识</el-menu-item>
          <el-menu-item index="/liuyan">留言板</el-menu-item>
        </el-menu>
        <el-dropdown>
        <div class="ronde" @click="handleOut">
          <img src="../assets/mine.png" alt="" v-if="!token"/>
          <img :src="img" alt="">
        </div>
        <el-dropdown-menu slot="dropdown" v-if="token">
          <el-dropdown-item>
            <a href="javascript:;" @click="gomine">个人中心</a>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userName: localStorage.getItem("userName"),
      token:'',
      img:''
    };
  },

  mounted() {
    this.token =  localStorage.getItem('token')
    this.img =  localStorage.getItem('img')
  },

  methods: {
    handleOut() {
      let token =  localStorage.getItem('token')
      if(!token){
        this.$confirm("是否前去登录?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 跳转登录
          this.$router.push("/login");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
      }
      
    },
    handleClick() {
      // this.$store.state.Collapse.isCollapse = !this.$store.state.Collapse.isCollapse
      this.$store.commit(
        "changeCollapse",
        !this.$store.state.Collapse.isCollapse
      );
    },
    // 触发全局组件 在vuex 中存储的状态
    openUserDialog() {
      this.$store.commit("changeDialogUserId", "");
      this.$store.commit("changeDialogUserForm", true);
    },
    gomine(){
      this.$router.push("/mine");
    }
  },
};
</script>

<style lang="less" scoped>
.header {
  display: flex;
  justify-content: center;
  align-items: center;

  .container {
    width: 1200px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .box {
    display: flex;
    align-items: center;

    .el-menu-demo {
      margin-right: 30px;
      // background-color: #ccc;
    }
  }

  .ronde {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 50px;
    img {
      width: 30px;
      height: 30px;
      margin-top: 10px;
    }
  }
}
span {
  color: #fff;
}
</style>
