<template>
  <div class="nav">
    <div class="title">贵州省精品展览展示平台</div>
    <div class="tabs" v-for="(item, index) in pages" :key="index">
      <router-link :to="item.path">{{ item.name }}</router-link>
    </div>
    <div class="operation">
      <span v-if="isLogged">欢迎你，{{ userInfo.userName }}</span>
      <router-link v-if="!isLogged" to="/login" class="operation-item"
        >登陆</router-link
      >|
      <router-link to="/personal" class="operation-item">个人中心</router-link>|
      <span class="logout">
        <el-popover
          placement="bottom"
          width="200"
          trigger="click"
          v-model="logoutDialogVisible"
        >
          <p>确定退出吗？</p>
          <div style="text-align: right; margin: 0">
            <el-button
              size="mini"
              type="text"
              @click="logoutDialogVisible = false"
              >取消</el-button
            >
            <el-button type="primary" size="mini" @click="handleLogOut"
              >确定</el-button
            >
          </div>
          <span class="operation-item" slot="reference">退出</span>
        </el-popover>
      </span>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  name: "NavBar",
  computed: {
    ...mapGetters(["token", "userInfo"]),
    isLogged() {
      return this.userInfo && this.userInfo.userName;
    },
  },
  data() {
    return {
      pages: [
        {
          name: "首页",
          path: "/",
        },
        {
          name: "精品展",
          path: "/boutique",
        },
        {
          name: "虚拟展厅",
          path: "/virtual",
        },
        {
          name: "布展",
          path: "/display",
        },
        {
          name: "文物展",
          path: "/culturalRelic",
        },
      ],
      userName: "",
      logoutDialogVisible: false,
    };
  },
  methods: {
    async handleLogOut() {
      await this.$store.dispatch("user/logout");
      this.logoutDialogVisible = false;
      this.$router.push(`/login?redirect=${this.$route.fullPath}`);
    },
  },
};
</script>

<style lang="scss" scoped>
a {
  color: #000;
  text-decoration: none;
}
.nav {
  display: flex;
  height: 120px;
  align-items: center;
  justify-content: space-between;
  margin: auto;
  width: 1200px;
  .title {
    font-size: 30px;
    font-weight: bold;
  }
  .tabs {
    display: flex;
    margin-right: 5px;
  }
  .operation {
    .operation-item {
      margin-right: 8px;
      cursor: pointer;
      color: #000;
    }
  }
}
</style>