import {
  login,
  logout,
  getUserInfo
} from '@/api/user'
import { setToken, getToken } from '@/libs/util'

// 格式化菜单
const initMenu = function (data) {
  data.forEach(item => {
    item.meta = {
      title: item.name,
      icon: item.icon
    }
    item.name = item.key
    if (item.children) {
      initMenu(item.children)
    }
  })
  return data
}

export default {
  state: {
    id: '',
    name: '',
    account: '',
    color: '',
    isOnline: true,
    jobNo: '',
    token: getToken(),
    access: '',
    accessName: '',
    hasGetInfo: false,
    menu: [],
    opreates: [],
    station: ''
  },
  mutations: {
    setId (state, id) {
      state.id = id
    },
    setAccount (state, account) {
      state.account = account
    },
    setColor (state, color) {
      state.color = color
    },
    setName (state, name) {
      state.name = name
    },
    setJobNo (state, jobNo) {
      state.jobNo = jobNo
    },
    setIsOnline (state, isOnline) {
      state.isOnline = isOnline
    },
    setToken (state, token) {
      state.token = token
      setToken(token)
    },
    setHasGetInfo (state, status) {
      state.hasGetInfo = status
    },
    setAccess (state, access) {
      state.access = access
    },
    setAccessName (state, accessName) {
      state.accessName = accessName
    },
    setMenu (state, menu) {
      state.menu = menu
    },
    setOpreates (state, opreates) {
      state.opreates = opreates
    },
    setStation (state, station) {
      state.station = station
    }
  },
  getters: {
    messageUnreadCount: state => state.messageUnreadList.length,
    messageReadedCount: state => state.messageReadedList.length,
    messageTrashCount: state => state.messageTrashList.length
  },
  actions: {
    // 登录
    handleLogin ({ commit }, { userName, password }) {
      userName = userName.trim()
      return new Promise((resolve, reject) => {
        login({
          account: userName,
          password
        }).then(res => {
          if (res.code === 0) {
            commit('setToken', res.result.token)
            resolve()
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 退出登录
    handleLogOut ({ state, commit }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          localStorage.setItem('tagNaveList', '')
          commit('setToken', '')
          commit('setAccess', [])
          commit('setAccessName', '')
          commit('setAccount', '')
          commit('setColor', '')
          commit('setId', '')
          commit('setName', '')
          commit('setHasGetInfo', false)
          commit('setJobNo', '')
          commit('setIsOnline', false)
          commit('setMenu', [])
          commit('setOpreates', [])
          commit('setStation', [])
          resolve()
        }).catch(err => {
          reject(err)
        })
        // 如果你的退出登录无需请求接口，则可以直接使用下面三行代码而无需使用logout调用接口
        // commit('setToken', '')
        // commit('setAccess', [])
        // resolve()
      })
    },
    // 获取用户相关信息
    getUserInfo ({ state, commit }) {
      return new Promise((resolve, reject) => {
        try {
          getUserInfo().then(res => {
            if (res.code === 0) {
              commit('setId', res.result.id)
              commit('setName', res.result.name)
              commit('setJobNo', res.result.jobNo)
              commit('setAccount', res.result.account)
              commit('setColor', res.result.color)
              commit('setIsOnline', res.result.isOnline)
              commit('setHasGetInfo', true)
              commit('setAccess', res.result.roleKey)
              commit('setAccessName', res.result.roleName)
              commit('setMenu', initMenu(res.result.menus || []))
              commit('setOpreates', res.result.opreates)
              commit('setStation', res.result.station)
              resolve(res)
            } else {
              console.log(res)
            }
          }).catch(err => {
            reject(err)
          })
        } catch (error) {
          reject(error)
        }
      })
    }
  }
}
