/*
 * @Author: donghao donghao@supervision.ltd
 * @Date: 2025-03-06 17:57:05
 * @LastEditors: donghao donghao@supervision.ltd
 * @LastEditTime: 2025-03-10 13:48:21
 * @FilePath: \5G-Loading-Bay-Web\src\stores\user.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: donghao donghao@supervision.ltd
 * @Date: 2025-03-06 13:51:46
 * @LastEditors: donghao donghao@supervision.ltd
 * @LastEditTime: 2025-03-10 13:40:39
 * @FilePath: \vite-ai\data-dashboard\src\stores\user.ts
 * @Description: 用户状态管理
 */
import { defineStore } from "pinia";
import router, { resetRouter } from "@/router";
import { setLocal, removeLocal} from "@/utils/local";
interface UserState {
  token: string | null;
}

export const useUserStore = defineStore("user", {
  state: (): UserState => ({
    token: localStorage.getItem("token"),
  }),
  actions: {
    login(
      token: string,
      form: { remember: boolean; username: string; password: string }
    ) {
      this.token = token;
      setLocal("token", token)
      form.remember
        ? setLocal("userLoginInfo", form)
        : removeLocal("userLoginInfo");
    },
    logout() {
      this.token = null;
      localStorage.removeItem("token");
      resetRouter();
      router.replace("/login");
    },
  },
});
