import { defineStore } from 'pinia'

export const useAuthStore = defineStore('auth', {
  state: () => ({
    isLoggedIn: localStorage.getItem('isLoggedIn') === 'true',
    username: localStorage.getItem('username') || ''
  }),
  actions: {
    login(username, password) {
      if (username === 'Admin' && password === 'Admin') {
        this.isLoggedIn = true
        this.username = username
        localStorage.setItem('isLoggedIn', 'true')
        localStorage.setItem('username', username)
        // 记住密码：按需求将密码持久化到 localStorage
        localStorage.setItem('rememberedPassword', password)
        return true
      }
      return false
    },
    logout() {
      this.isLoggedIn = false
      this.username = ''
      localStorage.removeItem('isLoggedIn')
      localStorage.removeItem('username')
    }
  }
})
