import storage from 'store'
import { getMiddleUser, login, commonLogin } from '@/api/login'
import { ACCESS_TOKEN } from '@/store/mutation-types'

const user = {
  state: {
    token: '',
    user_id: '',
    org_id: '',
    username: '',
    nickname: '',
    avatar: '',
    is_manage: false,
    roles: [],
    info: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USER_ID: (state, userId) => {
      state.user_id = userId
    },
    SET_USERTYPE: (state, type) => {
      state.usertype = type
    },
    SET_ORG_ID: (state, userId) => {
      state.org_id = userId
    },
    SET_USERNAME: (state, name) => {
      state.username = name
    },
    SET_NICKNAME: (state, name) => {
      state.nickname = name
    },
    SET_IS_MANAGE: (state, isManage) => {
      state.is_manage = isManage
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    }
  },
  actions: {
    // 登录
    Login ({ dispatch, commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          storage.set(ACCESS_TOKEN, 'Bearer ' + response.access_token, 7 * 24 * 60 * 60 * 1000)
          commit('SET_TOKEN', 'Bearer ' + response.access_token)
          dispatch('GetMiddleUser')
          resolve(response.is_company)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 密码登录
    commonLogin ({ dispatch, commit }, userInfo) {
      return new Promise((resolve, reject) => {
        commonLogin(userInfo).then(response => {
          // 实名允许登录
          // if (response.real_name_status) {
          // // 实名且关注公众号让登录
          //   // if (response.is_subscribe) {
          //   storage.set(ACCESS_TOKEN, 'Bearer ' + response.access_token, 7 * 24 * 60 * 60 * 1000)
          //   commit('SET_TOKEN', 'Bearer ' + response.access_token)
          //   dispatch('GetMiddleUser')
          //   resolve(response)
          //   return
          // }
          storage.set(ACCESS_TOKEN, 'Bearer ' + response.access_token, 7 * 24 * 60 * 60 * 1000)
          commit('SET_TOKEN', 'Bearer ' + response.access_token)
          dispatch('GetMiddleUser')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetMiddleUser ({ commit }) {
      return new Promise((resolve, reject) => {
        getMiddleUser().then(res => {
          commit('SET_USER_ID', res.middleground_id)
          commit('SET_USERNAME', res.username)
          commit('SET_NICKNAME', res.name)
          commit('SET_IS_MANAGE', res.is_manage)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout ({ commit, state }) {
      return new Promise((resolve) => {
        commit('SET_TOKEN', '')
        commit('SET_ORG_ID', '')
        commit('is_subscribe', '')
        commit('SET_IS_MANAGE', false)
        commit('SET_ROLES', [])
        storage.remove(ACCESS_TOKEN)
        storage.remove('is_subscribe')
        resolve()
      })
    }

  }
}

export default user
