import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
// import qs from 'qs'
const state = {
  token: getToken(),
  name: '',
  avatar: '',
  organ: '',
  organName: '',
  phone: ''
}
// organName
const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ORGAN: (state, organ) => {
    state.organ = organ
  },
  SET_ORGANNAME: (state, organName) => {
    state.organName = organName
  },
  SET_REFRESH_TOKEN: (state, refreshToken) => {
    state.refreshToken = refreshToken
  },
  SET_EXPIRES_IN: (state, expiresIn) => {
    state.expiresIn = expiresIn
  },
  SET_PHONE: (state, phone) => {
    state.phone = phone
  }
}

const actions = {
  // user login
  login ({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      //qs.stringify({ username: username.trim(), password: password, clientId: 'app', clientSecret: 'app' })
      // { username: username.trim(), password: password }
      login({ username: username.trim(), password: password, clientId: 'system', clientSecret: 'system' }).then(response => {
        const { data } = response
        if (response.code == 200) {

          let token = data.authentication.token_type + ' ' + data.authentication.access_token;
          commit('SET_REFRESH_TOKEN', data.authentication.refresh_token)
          commit('SET_EXPIRES_IN', data.authentication.expires_in)
          commit('SET_TOKEN', token)
          setToken(token)
          resolve()
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  // get 获取用户信息
  getInfo ({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        if (response.code == 200) {
          const data = response
          if (!data.data) {
            reject('获取用户信息错误,请重新登录')
          }
          const username = data.data.realName || data.data.username;
          const avatar = data.data.avatar;
          const organ = data.data.organId;
          const organName = data.data.organName;
          const phone = data.data.phone
          // const { name, avatar } = data
          commit('SET_NAME', username)
          commit('SET_AVATAR', avatar)
          commit('SET_ORGAN', organ)
          commit('SET_ORGANNAME', organName)
          commit('SET_PHONE', phone)
          resolve(data)
        }
      }).catch(error => {
        reject('获取用户信息错误,请重新登录')
      })
    })
  },

  // 登出
  logout ({ commit }) {
    return new Promise((resolve, reject) => {
      logout().then(() => {
        commit('SET_TOKEN', '')
        removeToken()
        resetRouter()
        commit('SET_NAME', '')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken ({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  }
}

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

