import { defineStore } from 'pinia'

export const useUserStore = defineStore({
  id: 'userStore',
  state: () => {
    return {
      userInfo: {},
      place: {
        id: '',
        label: '请选择'
      },
      roles: []
    }
  },
  getters: {
    getUserInfo: (state) => {
      const { userId } = state.userInfo
      return userId ? state.userInfo : uni.getStorageSync('userInfo')
    },
    getPlace: (state) => {
      return state.place || uni.getStorageSync('place') || {}
    },
    isWeibao: (state) => {
      return state.roles.includes('maintenance')
    },
    isAdmin: (state) => {
      return state.roles.includes('superadmin')
    }
  },
  actions: {
    setUserInfo(value) {
      this.userInfo = value || uni.getStorageSync('userInfo') || {}
      uni.setStorageSync('userInfo', this.userInfo)
    },
    setPlace(value) {
      this.place = value || uni.getStorageSync('place') || {}
      uni.setStorageSync('place', this.place)
    },
    setUserRoles(value) {
      this.roles = value || uni.getStorageSync('roles') || []
      uni.setStorageSync('roles', this.roles)
    }
  }
})
