import Vue from 'vue'
import { login, logout } from '@/api/login'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { welcome } from '@/utils/util'
import VueCookies from 'vue-cookies'
const user = {
  state: {
    token: '',
    name: '',
    mustChangePwd: null,
    welcome: '',
    avatar: '',
    roles: { permissionList: [] },
    info: {},
    authOrgs: [],
    changeOrg: ''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
      sessionStorage.setItem(ACCESS_TOKEN, token)
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
      Vue.ls.set('userName', name, 7 * 24 * 60 * 60 * 1000)
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
    SET_AUTHORGS: (state, authOrgs) => {
      state.authOrgs = authOrgs
    },
    SET_CHANGEORG: (state, changeOrg) => {
      state.changeOrg = changeOrg
    },
    SET_MUSTCHANGEPWD: (state, status) => {
      state.mustChangePwd = status
    }
  },

  actions: {
    // 登录
    Login ({ commit }, userInfo) {
      console.log(userInfo, 'userInfo----')
      return new Promise((resolve, reject) => {
        login(userInfo).then(ret => {
          console.log(ret, 'login success')
          if (ret.status == 200) {
            const res = ret.data
            if (res.access_token && res.auth_user) {
              const users = res.auth_user
              Vue.ls.set('hasError', 0)
              commit('SET_TOKEN', res.access_token)
              commit('SET_INFO', users)
              commit('SET_AUTHORGS', res.auth_orgs || [])
              commit('SET_NAME', { name: users.userNameCN, welcome: welcome() })
              commit('SET_MUSTCHANGEPWD', users.mustChangePwd)
              Vue.ls.set('rolesAccess-qpls-md', { permissionList: users.permCodes }, 7 * 24 * 60 * 60 * 1000)
              commit('SET_AVATAR', res.avatar ? res.avatar : '')
              // 记住密码
              if (userInfo.rememberMe) {
                VueCookies.set('REMEMBER_ME', JSON.stringify(userInfo))
              } else {
                VueCookies.remove('REMEMBER_ME')
              }
            }
            resolve(res)
          } else {
            reject(ret)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 从本地存储更新用户信息
    SetInfo ({ commit }, userInfo) {
      commit('SET_NAME', { name: userInfo, welcome: '' })
    },
    // 获取用户信息
    GetInfo ({ commit, state }) {
      const roles = Vue.ls.get('rolesAccess-qpls-md')
      commit('SET_ROLES', roles)
      // commit('SET_TOKEN', sessionStorage.getItem(ACCESS_TOKEN) || state.token)
      commit('SET_NAME', { name: Vue.ls.get('userName'), welcome: '' })
      return roles
    },
    // 切换账号
    SetOrgSn ({ commit, state }, info) {
      // console.log(info)
      commit('SET_CHANGEORG', info)
    },
    // 登出
    Logout ({ commit, state }) {
      console.log(state)
      return new Promise((resolve) => {
        logout(state.token).then((res) => {
          resolve(res.data)
        }).catch((error) => {
          resolve(error)
        }).finally(() => {
          commit('SET_INFO', {})
          commit('SET_AUTHORGS', [])
          commit('SET_CHANGEORG', '')
          commit('SET_NAME', { name: '', welcome: '' })
          commit('SET_MUSTCHANGEPWD', '')
          commit('SET_AVATAR', '')
          commit('SET_TOKEN', '')
          commit('SET_ROLES', { permissionList: [] })
          Vue.ls.remove(ACCESS_TOKEN)
          Vue.ls.remove('userName')
          Vue.ls.set('hasError', 0)
          sessionStorage.clear()
        })
      })
    }

  }
}

export default user
