import { defineStore } from 'pinia'
import { currentUserInfo } from '@/http/apis/systemManage/userManage'
import { getUserRoleList } from '@/http/apis/systemManage/roleManage'

export const useUserStore = defineStore('userConfig', {
  state: () => {
    return {
      userInfo: {},
      roleList: []
    }
  },
  getters: {
    roleListDict () {
      const opt = this.roleList?.map(item => ({ id: item.id, name: item.name }))
      return opt || []
    } // 用户角色
  },
  actions: {
    setUserStore () {
      return new Promise((resolve, reject) => { // 获取当前用户信息
        currentUserInfo()
          .then(res => {
            this.userInfo = { ...res.data }
            resolve(res.data)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    setRoleStore () {
      return new Promise((resolve, reject) => {
        getUserRoleList({ pageSize: 100, pageNumber: 1 })
          .then(res => {
            this.roleList = res.data.records
            resolve(res.data)
          })
          .catch(err => {
            reject(err)
          })
      })
    }
  },
  persist: {
    storage: localStorage,
    paths: ['userInfo']
  }
})
