import { toRaw } from 'vue'
import Request from '@/utils/request'

const state = {
  userInfo: {},
  orgIdArr:[],
  orgId:'',
}

const getters = {
  // 获取用户菜单权限
  getUserAuthList: state => {
    if (state.userInfo.length <= 0) {
      return []
    }
    if (typeof state.userInfo.auth_list === undefined) {
      return []
    }
    return toRaw(state.userInfo.auth_list)
  }
}

const mutations = {
  SET_USER_INFO: (state, user) => {
    state.userInfo = user
  },
  SET_ORGID: (state, orgId) => {
    state.orgId = orgId
  },
  SET_ORGID_ARR: (state, orgIdArr) =>{
    state.orgIdArr = orgIdArr
  }
}

const actions = {
  // 设置用户信息
  setUserInfo (context, user) {
    context.commit('SET_USER_INFO', user)
  },

  // 设置用户信息
  setorgId (context, orgId) {
    context.commit('SET_ORGID', orgId)
  },

  // 设置组织
  setorgIdArr (context, orgIdArr){
    context.commit('SET_ORGID_ARR', orgIdArr)
  },

  // 获取用户信息
  getUserInfo (context) {
    return new Promise((resolve, reject) => {
      if (context.state.userInfo.length <= 0 || typeof context.state.userInfo.auth_list === undefined) {
        // 服务器获取用户信息
        // return (new Request()).get('user/info')
        // .then(r => {
        //   const { errno, data, errmsg } = r
        //   if (errno !== 0) {
        //     reject(errmsg)
        //   }
        //   context.commit('SET_USER_INFO', data)
        //   resolve(data)
        // })
        // .catch(e => {
        //   reject(e)
        // })
      }
      resolve(toRaw(context.state.userInfo))
    })
  },
  getOrdInfo (context) {
    return new Promise((resolve, reject) => {
      resolve(toRaw(context.state.orgIdArr))
    })
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
