// eslint-disable-next-line no-unused-vars
import { login, getInfo } from '@/api/user'
export default {
  state: {
    token: '',
    user: {}
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USER: (state, user) => {
      state.user = user
    },
    SET_LOGOUT: (state) => {
      state.token = ''
      state.user = {}
    }
  },
  actions: {
    refreshToken({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          let isAdmin = false
          response.data.app_list.forEach(app => {
            if (app.app_code === 'EXPOREG') {
              isAdmin = true
            }
          })
          commit('SET_USER', {
            username: response.data.user_name,
            nickname: response.data.nick_name,
            avatar: response.data.avatar,
            email: response.data.email,
            phone: response.data.phone,
            isAdmin: isAdmin,
            app_list: response.data.app_list,
            permission: response.data.permission
          })
        })
      })
    },
    tokenLogin({ commit }, playload) {
      return new Promise((resolve, reject) => {
        commit('SET_TOKEN', playload.token)
        getInfo().then(response => {
          let isAdmin = false
          response.data.app_list.forEach(app => {
            if (app.app_code === 'EXPOREG') {
              isAdmin = true
            }
          })
          commit('SET_USER', {
            username: response.data.user_name,
            nickname: response.data.nick_name,
            avatar: response.data.avatar,
            email: response.data.email,
            phone: response.data.phone,
            isAdmin: isAdmin,
            app_list: response.data.app_list,
            permission: response.data.permission
          })
          if (!isAdmin) {
            reject('您没有权限访问！')
          } else {
            resolve('登录成功')
          }
        }).catch(error => {
          console.log(error)
          reject('获取用户信息失败')
        })
      })
    },
    /**
     * 登录
     * @param commit
     * @param payload
     * @param payload.savePassword 是否保存密码
     * @param payload.username 用户名
     * @param payload.password 密码
     * @returns {Promise<unknown>}
     */
    login({ commit }, payload) {
      return new Promise((resolve, reject) => {
        login(payload.username, payload.password, payload.login_type, payload.login_portal, payload.phone, payload.vaild_code).then(response => {
          commit('SET_TOKEN', response.data.api_token)
          getInfo().then(response => {
            let isAdmin = false
            response.data.app_list.forEach(app => {
              if (app.app_code === 'EXPOREG') {
                isAdmin = true
              }
            })
            commit('SET_USER', {
              username: response.data.user_name,
              nickname: response.data.nick_name,
              avatar: response.data.avatar,
              email: response.data.email,
              phone: response.data.phone,
              isAdmin: isAdmin,
              app_list: response.data.app_list,
              permission: response.data.permission
            })
            if (payload.savePassword) {
              const savedAccount = {
                username: payload.username,
                password: payload.password
              }
              localStorage.setItem('savedAccount', JSON.stringify(savedAccount))
            } else {
              localStorage.removeItem('savedAccount')
            }
            if (!isAdmin) {
              reject('您没有权限访问！')
            } else {
              resolve('登录成功')
            }
          })
        }).catch(error => {
          console.log(error)
          reject('登录失败')
        })
      })
    },
    /**
     * 登出
     * @param commit
     * @returns {Promise<unknown>}
     */
    logout({ commit }) {
      return new Promise((resolve, reject) => {
        commit('SET_LOGOUT')
        resolve('退出登录成功')
      })
    }
  }
}
