import { defineStore } from 'pinia';
import piniaPersistConfig from '@/stores/helper/persist';
import { logout } from '@/api/auth';

export const useUserStore = defineStore({
  id: 'admin-user',
  state: () => ({
    token: '',
    userInfo: null,
    isTenant: false, // 是否操作了租户
    tenant: 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;
    },
    // Set setUserInfo
    setUserInfo(userInfo) {
      this.userInfo = userInfo;
    },
    // Set isTenant
    setIsTenant(flag) {
      this.isTenant = flag;
    },
    setTenant(tenant) {
      this.tenant = tenant;
    },
    async logout() {
      await logout();
      this.token = '';
      this.userInfo = null;
      this.isTenant = false;
      this.tenant = null;
    },
  },
  persist: piniaPersistConfig('admin-user'),
});
