import { getUserMenu, getUserInfo } from '~/service/user';
import {
  userLogout,
} from '~/service/login';
import useLoginStore from "./login";
import useMenuStore from './menu'

import { defineStore } from 'pinia';

export const TOKEN = 'token';
export default defineStore('user', {
  state: () => {
    return {
      user: {},
    };
  },
  actions: {
    setUserInfo(payload) {
      this.user = payload;
    },
    /**
     * 用户登出
     */
    async userLogout() {
      const { code } = await userLogout();
      if (code == 0) {
        const login = useLoginStore()
        login.logout()
      }
    },
    /**
     * 设置用户信息
     */
    async getUserInfo() {
      const { code, data } = await getUserInfo();
      this.setUserInfo(code == 0 ? data : {})
    },
    /**
     * 获取用户菜单
     */
    async getUserMenu() {
      const { code, data } = await getUserMenu();
      const menu = useMenuStore()
      menu.setMenu(code == 0 ? data : [])
    },
  }
});
