<template>
  <div class="tool-bar-ri">
    <div class="header-icon">
      <el-button type="primary" @click="getOauthUrl" v-if="!isAuth" style="color: #fff">巨量授权</el-button>
      <el-button type="primary" @click="getOauthUrl" v-if="isAuth" style="color: #fff">刷新巨量授权</el-button>
      <AssemblySize id="assemblySize" />
      <Language id="language" />
      <SearchMenu id="searchMenu" />
      <ThemeSetting id="themeSetting" />
      <Message id="message" />
      <Fullscreen id="fullscreen" />
    </div>
    <span class="username">{{ username }}</span>
    <Avatar />
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import { useUserStore } from "@/stores/modules/user";
import AssemblySize from "./components/AssemblySize.vue";
import Language from "./components/Language.vue";
import SearchMenu from "./components/SearchMenu.vue";
import ThemeSetting from "./components/ThemeSetting.vue";
import Message from "./components/Message.vue";
import Fullscreen from "./components/Fullscreen.vue";
import Avatar from "./components/Avatar.vue";
import { getOauthUrlApi, isAuthApi, getAccessTokenApi } from "@/api/modules/common";
const userStore = useUserStore();
const username = computed(() => userStore.userInfo.name);

// 获取路由信息
const route = useRoute();

//是否需要授权
const isAuth = ref<boolean>(false);

const getOauthUrl = async () => {
  const res = await getOauthUrlApi();
  window.location.href = res.data;
};

onMounted(async () => {
  const res = await isAuthApi();
  console.log("是否认证222:", res);
  if (res.data) {
    isAuth.value = res.data;
  }
  console.log("所有查询参数:", route.query);
  if (route.query.auth_code) {
    getAccessTokenApi({ authCode: route.query.auth_code as string }).then(res => {
      console.log("获取巨量引擎 access token:", res);
      window.location.href = "https://advertiser.util.06zk.com/home/index";
    });
  }
});
</script>

<style scoped lang="scss">
.tool-bar-ri {
  display: flex;
  align-items: center;
  justify-content: center;
  padding-right: 25px;
  .header-icon {
    display: flex;
    align-items: center;
    & > * {
      margin-left: 21px;
      color: var(--el-header-text-color);
    }
  }
  .username {
    margin: 0 20px;
    font-size: 15px;
    color: var(--el-header-text-color);
  }
}
</style>
