import { getBrandList, setBrandList } from '@/utils/toy'
import { getTenantList, setTenantList, setUserList } from '@/utils/cache/tenant'
import { fetchAllTenant } from '@/api/system/tenant'
import { listAllUser } from '@/api/system/user'

const tenant = {
  state: {
    tenantList: getTenantList() || [],
    userList: getBrandList() || []
  },

  mutations: {
    SET_TENANT_LIST: (state, list) => {
      state.tenantList = list
      setTenantList(list)
    },
    SET_USER_LIST: (state, list) => {
      state.userList = list
      setUserList(list)
    }
  },

  actions: {
   GetTenantList({ commit }) {
     return new Promise((resolve, reject) => {
       fetchAllTenant().then(({ data }) => {
         commit('SET_TENANT_LIST', data)
         resolve(data)
       }).catch(err => {
         reject(err)
       })
     })
   },
   GetUserList({ commit }) {
     return new Promise((resolve, reject) => {
       listAllUser().then(({ data }) => {
         commit('SET_USER_LIST', data)
         resolve(data)
       }).catch(err => {
         reject(err)
       })
     })
   }
  }
}
