import { defineStore } from 'pinia';
import piniaPersistConfig from '@/stores/helper/persist';
import { logout } from '@/api/auth';
import useSocketStore from './websocket';
export const useUserStore = defineStore({
  id: 'admin-user',
  state: () => ({
    token: '',
    userInfo: null,
    isTenant: false, // 是否操作了调解中心
    tenant: null, // 当前调解中心
    time: null, // 本地时间
  }),
  getters: {
    getUserTenants() {
      return (
        this.userInfo?.tenants || [
          { code: 123, name: 'tenant1', id: '1' },
          { code: 1234, name: 'tenant2tenant2', id: '2' },
          { code: 1235, name: 'tenant3tenant2tenant2', id: '3' },
        ]
      );
    },
  },
  actions: {
    // Set Token
    setToken(token) {
      this.token = token;
    },
    setTime(time) {
      this.time = time;
    },
    // Set setUserInfo
    setUserInfo(userInfo) {
      this.userInfo = userInfo;
    },
    // Set isTenant
    setIsTenant(flag) {
      this.isTenant = flag;
    },
    setTenant(tenant) {
      this.tenant = tenant;
    },
    async logout() {
      await logout();
      const socketStore = useSocketStore();
      socketStore.closeWs();
      this.token = '';
      this.userInfo = null;
      this.isTenant = false;
      this.tenant = null;
    },
    async logoutPass() {
      const socketStore = useSocketStore();
      socketStore.closeWs();
      this.token = '';
      this.userInfo = null;
      this.isTenant = false;
      this.tenant = null;
    },
  },
  persist: piniaPersistConfig('admin-user'),
});
