import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useTabsStore } from '@/stores/tabs'

export const useUserStore = defineStore('user', () => {
  const isLoggedIn = ref(sessionStorage.getItem('isLoggedIn') === 'true')
  const userInfo = ref(JSON.parse(sessionStorage.getItem('userInfo')) || null)

  // 登录
  const login = (info) => {
    const tabsStore = useTabsStore()
    tabsStore.clearTabs()
    isLoggedIn.value = true
    userInfo.value = info
    sessionStorage.setItem('isLoggedIn', 'true')
    sessionStorage.setItem('userInfo', JSON.stringify(info))
  }

  // 退出登录
  const logout = () => {
    isLoggedIn.value = false
    userInfo.value = null
    sessionStorage.clear()
    const tabsStore = useTabsStore()
    tabsStore.clearTabs()
  }

  return {
    isLoggedIn,
    userInfo,
    login,
    logout
  }
}) 