import { getLogin, getUser } from '../../api/login'
import router, { resetRouter } from '@/router'
import cache from '@/utils/cache'

export default {
    namespaced: true,
    state: {
        user: ''
    },
    mutations: {
        SET_USER(state, val) {
            state.user = val
        },
        // 只做权限测试页使用
        SET_ROLE(state, val) {
            state.user.roles = val
        }
    },
    actions: {
        // 获取登录数据
        async getLoginToken({ commit }, params) {
            // let res = await getLogin(params);
            // if (res && res.code == 0) {
            //     cache.setToken(res.data.token)
            //     cache.setLocal('user', res.data)
            //     return res
            // } else {
            //     reject(new Error('nothing login data'))
            // }
            return new Promise((resolve, reject) => {
                getLogin(params).then(res => {
                    if (res && res.code == 0) {
                        cache.setToken(res.data.token)
                        cache.setLocal('user', res.data)
                        resolve(res)
                    } else {
                        reject(new Error('nothing login data'))
                    }
                }).catch(err => {
                    reject(err)
                })
            })

        },
        // 获取用户数据
        async getUserData({ commit }) {
            return new Promise((resolve, reject) => {
                let token = cache.getToken()
                let res = getUser(token)
                if (res) {
                    commit('SET_USER', res)
                    resolve(res)
                } else {
                    reject(new Error('nothing user data'))
                }
            })
        },
        logout({ commit }) {
            return new Promise((resolve, reject) => {
                // 删除本地token
                cache.removeToken()
                // 重置路由
                resetRouter()
                // 删除用户信息
                commit('SET_USER', '')
                // 跳转到登录页
                router.push('/login')
                resolve()
            })
        }
    }
}
