import md5 from 'js-md5'
import { login, getInfo, getMenus } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { Message } from 'element-ui'

const user = {
  state: {
    token: getToken(),
    rolecode: '',
    rolename: '',
    name: '',
    usercode: '',
    userRole: '',
    userid: '',
    groupid: '',
    extension: '',
    avatar: '',
    seatflag: false,
    menus: [],
    mobile: '',
    groupcode: ''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_ROLE_CODE: (state, rolecode) => {
      state.rolecode = rolecode
    },
    SET_ROLE_NAME: (state, rolename) => {
      state.rolename = rolename
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_CODE: (state, usercode) => {
      state.usercode = usercode
    },
    SET_ROLE: (state, userRole) => {
      state.userRole = userRole
    },
    SET_UID: (state, userid) => {
      state.userid = userid
    },
    SET_GROUPID: (state, groupid) => {
      state.groupid = groupid
    },
    SET_GROUPCODE: (state, groupcode) => {
      state.groupcode = groupcode
    },
    SET_EXT: (state, extension) => {
      state.extension = extension
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_SEATFLAG: (state, seatflag) => {
      state.seatflag = seatflag
    },
    SET_MENUS: (state, menus) => {
      state.menus = menus
    },
    SET_MOBILE: (state, mobile) => {
      state.mobile = mobile
    }
  },

  actions: {
    // 登录
    Login ({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = md5(userInfo.password)
      window.localStorage.setItem('ext', userInfo.extension)
      return new Promise((resolve, reject) => {
        login(username, password).then(response => {
          const data = response.data
          setToken(data.token)
          commit('SET_TOKEN', data.token)
          commit('SET_ROLE_CODE', data.role_code)
          commit('SET_ROLE_NAME', data.role_name)
          commit('SET_GROUPCODE', data.group.toLowerCase()) // 用户的坐席组code
          window.localStorage.setItem('groupCode', data.group) // 用户的坐席组code
          window.localStorage.setItem('roleCode', data.role_code) // 用户的坐席组code
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo ({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          const data = response.data
          commit('SET_SEATFLAG', data.F_SeatFlag) // 坐席标志
          commit('SET_NAME', data.F_UserName) // 用户名
          commit('SET_CODE', data.F_UserCode) // 用户的usercode
          commit('SET_ROLE', data.F_Role) // 用户的 角色权限
          commit('SET_UID', data.F_UserId) // 用户的 id
          commit('SET_EXT', window.localStorage.getItem('ext'))// 分机号
          commit('SET_GROUPID', data.F_SeartGroupID) // 用户的坐席组id
          commit('SET_AVATAR', data.F_HeadImg) // 用户头像
          commit('SET_MOBILE', data.F_Mobile) // 手机
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取菜单信息
    GetMenus ({ commit, state }) {
      return new Promise((resolve, reject) => {
        getMenus().then(response => {
          const menusdata = response.authorizeMenu
          if (menusdata && menusdata.length > 0) { // 验证返回的menusdata是否是一个非空数组
            commit('SET_MENUS', menusdata)
          } else {
            removeToken()
            Message.error('获取菜单权限失败，请联系管理员分配菜单权限！')
            reject('getMneus: 菜单、按钮获取出错！')
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut ({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_ROLE_CODE', '')
        commit('SET_ROLE_NAME', '')
        commit('SET_GROUPID', '')
        commit('SET_EXT', '')
        removeToken()
        window.localStorage.removeItem('ext')// 分机号
        window.localStorage.removeItem('groupCode')// 用户的坐席组code   roleCode
        window.sessionStorage.removeItem('telIsLogin')// 签入状态
        window.sessionStorage.removeItem('roleCode') //角色code值
        resolve()
      })
    },

    // 修改用户头像
    SetAvatar ({ commit }, msg) {
      commit('SET_AVATAR', msg)// 用户头像
    }
  }
}

export default user
