// useUserStore.js
import { defineStore } from 'pinia';
import { loginByUsername, logout, getUserInfo, verifyCode,getInfo } from '@/api/login';
import { getToken, setToken, removeToken } from '@/utils/auth';

export const useUserStore = defineStore('user', {
  state: () => ({
    user: '111',
    status: '',
    code: '',
    token: getToken(),
    name: '',
    avatar: '',
    introduction: '',
    roles: [],
    perms: [],
    setting: {
      articlePlatform: []
    },
    userId: '',
    departmentList: [],
    userInfo: {},
    addDepartmentId:'',
    selectedMember:{}

  }),

  actions: {
    getUserInfo() {
      return new Promise((resolve, reject) => {
        getInfo().then((res) => {
          this.userInfo=res.data.data.userInfo
          resolve();

        }).catch(error => {
          reject(error);
        });
      });
    },
    verifyCode() {
      return new Promise((resolve, reject) => {
        verifyCode().then(response => {
          const token = response.headers['session_id'];
          this.token = token;
          setToken(token);
          resolve(response);
        }).catch(error => {
          reject(error);
        });
      });
    },

    LoginByUsername(userInfo) {
      const username = userInfo.username.trim();
      return new Promise((resolve, reject) => {
        loginByUsername(username, userInfo.password).then(response => {
          const token = response.data.data.token;
          this.token = token;
          setToken(token);
          resolve();
        }).catch(error => {
          reject(error);
        });
      });
    },

    GetUserInfo() {
      return new Promise((resolve, reject) => {
        getUserInfo().then(response => {
          const data = response.data.data;

          if (data.perms && data.perms.length > 0) {
            this.perms = data.perms;
          } else {
            reject('getInfo: perms must be a non-null array !');
          }

          this.roles = data.roles;
          this.name = data.name;
          this.fraId = data.fraId;
          this.avatar = data.avatar;
          this.introduction = data.introduction;
          resolve(response);
        }).catch(error => {
          reject(error);
        });
      });
    },

    LogOut() {
      return new Promise((resolve, reject) => {
        logout(this.token).then(() => {
          this.token = '';
          this.roles = [];
          this.perms = [];
          removeToken();
          resolve();
          window.location.reload(); // 为了重新实例化vue-router对象 避免bug
        }).catch(error => {
          reject(error);
        });
      });
    },

    FedLogOut() {
      return new Promise(resolve => {
        this.token = '';
        removeToken();
        resolve();
      });
    },

    ChangeRoles(role) {
      return new Promise(resolve => {
        this.token = role;
        setToken(role);
        getUserInfo(role).then(response => {
          const data = response.data;
          this.roles = data.roles;
          this.perms = data.perms;
          this.name = data.name;
          this.fraId = data.fraId;
          this.avatar = data.avatar;
          this.introduction = data.introduction;
          resolve();
        });
      });
    }
  },
  persist: true
});
