<template>
  <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <div class="container">
      <router-link class="navbar-brand" :to="{ name: 'home' }">King of Bots</router-link>
      <div class="collapse navbar-collapse" id="navbarText">
        <ul class="navbar-nav me-auto mb-2 mb-lg-0">
          <li class="nav-item">
            <!-- :class 是 v-bind:class 的简写 -->
            <router-link :class="route_name == 'pk_index' ? 'nav-link active' : 'nav-link'"
              :to="{ name: 'pk_index' }">对战</router-link>
          </li>
          <li class="nav-item">
            <router-link :class="route_name == 'record_index' ? 'nav-link active' : 'nav-link'"
              :to="{ name: 'record_index' }">对局列表</router-link>
          </li>
          <li class="nav-item">
            <router-link :class="route_name == 'ranklist_index' ? 'nav-link active' : 'nav-link'"
              :to="{ name: 'ranklist_index' }">排行榜</router-link>
          </li>


        </ul>

        <!-- 判断是否登录成功:如果登录成功,则修改导航栏这部分内容 -->
        <ul class="navbar-nav" v-if="$store.state.user.is_login">
          <!-- 下拉菜单样式 -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              <img class="user_photo" :src="$store.state.user.photo" alt="" />
              <!-- {{ $store.state.user.username }} -->
            </a>
            <ul class="dropdown-menu">
              <li>
                <router-link class="dropdown-item" :to="{ name: 'user_bot_index' }">我的Bot</router-link>
              </li>
              <!-- 下拉菜单分割线 -->
              <li>
                <hr class="dropdown-divider" />
              </li>
              <!-- v-on:click 也可以写为 @click ; 给退出按钮加上手形状按钮样式 style="cursor: pointer"-->
              <li>
                <a class="dropdown-item" style="cursor: pointer" @click="logout">退出</a>
              </li>
            </ul>
          </li>
        </ul>

        <!-- 当没有拉取信息时,再去展示登录注册内容 -->
        <ul class="navbar-nav" v-else-if="!$store.state.user.pulling_info">
          <!-- 下拉菜单样式 -->
          <li class="nav-item">
            <router-link class="nav-link" :to="{ name: 'user_account_login' }" role="button">
              登录
            </router-link>
          </li>

          <li class="nav-item">
            <router-link class="nav-link" :to="{ name: 'user_account_register' }" role="button">
              注册
            </router-link>
          </li>
        </ul>
      </div>
    </div>
  </nav>
</template>

<script>
import { useRoute } from "vue-router";
import { computed } from "vue";
import { useStore } from "vuex";

export default {
  setup() {
    const store = useStore();
    const route = useRoute();
    // 用于判断当前选中的是哪个 nav-link 链接,结合上文操作将选中的 nav-link 改为 nav-link active 模式
    let route_name = computed(() => route.name);

    const logout = () => {
      // console.log("退出前:" + store.state.user.token);
      store.dispatch("logout");
      // console.log("退出后:" + store.state.user.token);
    };

    return {
      route_name,
      logout,
    };
  },
};
</script>

<style scoped>
.user_photo {
  width: 30px;
  height: 30px;
  border-radius: 15%;
  margin: auto;
  /* display: flex;
  align-items: center; */
}
</style>
