<template>
  <NuxtPage />
</template>

<script setup>
import { reactive } from 'vue';
import { useRouter } from 'vue-router';
import { getToken } from 'apis/index'

const router = useRouter();

const user = useUser()
const userId = useUserId()
const projectId = useProjectId()
const taskId = useTaskId()

// 获取路径上的参数
const routeValue = router.currentRoute.value;
console.log('routeValue: ', routeValue);
userId.value = routeValue && routeValue.query && routeValue.query.userId ? routeValue.query.userId : '';
projectId.value = routeValue && routeValue.query && routeValue.query.projectId ? routeValue.query.projectId : '';
taskId.value = routeValue && routeValue.query && routeValue.query.id ? routeValue.query.id : '';

// const userString = sessionStorage.getItem('user') || null;
// if (userString) {
//   const userInfo = JSON.parse(userString);
//   user.value = userInfo;
// } else {
  getTokenByUserId(userId.value)
// }

/**
 * 根据userId获取token级user信息
 * @param {*} param
 * @param {string} userId 用户id
 */
async function getTokenByUserId(userId) {
  try {
    const data = await getToken(userId);
    user.value = data || null;
      console.log('data: ', data);
    if (data) {
      sessionStorage.setItem('token', data.token);
      sessionStorage.setItem('user', JSON.stringify(data));
    } else {
      sessionStorage.removeItem('token');
      sessionStorage.removeItem('user');
    }
  } catch (error) {
    console.error('error: ', error);
  }
}
</script>
