import { defineStore } from 'pinia';
import { ref } from 'vue';
import { login as loginApi } from '../api/CommonApi'; // 假设你的API调用函数是这样组织的

export const useUserStore = defineStore('user', {
  state: () => ({
    token: '',
    userInfo: null,
  }),
  actions: {
    async login(username, password) {
      try {
        const response = await loginApi({ username, password });
        if (response && response.token) {
          this.token = response.token;
          // 你可能还需要一个接口来获取用户信息
          // this.userInfo = await getUserInfo();
          return true;
        }
        return false;
      } catch (error) {
        console.error('Login failed:', error);
        return false;
      }
    },
    logout() {
      this.token = '';
      this.userInfo = null;
    },
  },
  getters: {
    isLoggedIn: (state) => !!state.token,
  },
  persist: true,
});