import api from "@/axios";
import { defineStore } from "pinia";
import { ref } from "vue";
import { useTabsStore } from "./tabsStore";
import { usePermissionStore } from "./permission";

// 定义用户信息的类型

export const useUserStore = defineStore(
  "user",
  () => {
    // 用户token信息
    const tokenInfo = ref<TokenInfo | null>(null);
    const userInfo = ref<SysUserInfo | null>(null);
    function setTokenInfo(user: TokenInfo) {
      tokenInfo.value = user;
    }
    // 刷新用户信息
    // const refreshUserInfo = async () => {
    //   const resp = await api.get<SysUserInfo>("/management/user/userInfo");
    //   userInfo.value = resp.data as SysUserInfo;
    //   console.log(userInfo.value);
    //   return userInfo.value;
    // };

    //重置用户所有信息
    const resetUserInfo = () => {
      tokenInfo.value = null;
      userInfo.value = null;
      useTabsStore().resetTab();
      usePermissionStore().clearAuthMenu();
    };

    return {
      tokenInfo,
      setTokenInfo,
      resetUserInfo,
    };
  },
  {
    persist: {
      key: "userStore", // 存储的 key
      storage: localStorage, // 持久化到 localStorage,
      pick: ["tokenInfo", "userInfo"],
    },
  }
);
