import { getToken, setToken, setExpiresIn, removeToken } from '@/utils/auth'
import store from '@/store'
import router from '@/router'
import md5 from 'js-md5';

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
    messageNum:0,
    deptList:[],
    checkDeptIndex:0,
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_EXPIRES_IN: (state, time) => {
      state.expires_in = time
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_MESSAGE_NUM: (state, num) => {
      state.messageNum = num
    },
    SET_DEPT_LIST: (state, deptList) => {
      state.deptList = deptList
    },
    SET_CHECK_DEPT_INDEX: (state, index) => {
      state.checkDeptIndex = index
    }
  },

  actions: {
    //不调用退出接口直接清除token
    LogOutNoData({ commit, state }) {
      return new Promise((resolve, reject) => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        commit('SET_PERMISSIONS', [])
        removeToken()
        resolve()
      })
    },
    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },
    //清除登录数据
    delToken({ commit, state }){
      localStorage.clear();
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      commit('SET_PERMISSIONS', [])
      removeToken()
    },
    setCheckIndex({ commit }, index){
      console.log('SET_CHECK_DEPT_INDEX')
      commit('SET_CHECK_DEPT_INDEX', index)
    },
    setDept({ commit }, list){
      console.log('SET_DEPT_LIST')
      commit('SET_DEPT_LIST', list)
    },
  }
}

export default user
