import { login, logout, getInfo } from '@/api/acl/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import api from '@/api/api'
const getDefaultState = () => {
    return {
        token: '',
        userName: '',
        headImage: '',
        time: new Date(),
        avatar: '',
        roles: [],
        buttons: [],
        carNumber: ''
    }
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_TOKEN: (state, token) => {
        state.token = token
    },

    SET_USERINFO: (state, userInfo) => {
        state.userName = userInfo.admin_account
        // state.headImage = userInfo.headImage
        // state.buttons = userInfo.buttons
    },

    SET_ROLES: (state, roles) => {
        state.roles = roles
    },
    SET_TIME: (state, time) => {
        state.time = time
    }
}

const actions = {
    // user login
    login({ commit }, userInfo) {
        // console.log(userInfo);
        const { username, password, token } = userInfo
        return new Promise((resolve, reject) => {
            api.role.adminlogin({ admin_account: username.trim(), admin_password: password })
                .then((response) => {
                    if (response.code === 200) {
                        commit('SET_TOKEN', token)
                        commit('SET_USERINFO', response.data)
                        setToken(token)
                        resolve()
                    } else {
                        commit('SET_TOKEN', "")
                        setToken("")
                        reject(error)
                    }
                })
                .catch((error) => {
                    reject(error)
                })
        })
    },

    // get user info
    getInfo({ commit, state }) {
        return new Promise((resolve, reject) => {
            getInfo(state.token)
                .then((response) => {
                    const { data } = response

                    if (!data) {
                        reject('Verification failed, please Login again.')
                    }

                    const { roles } = data

                    // roles must be a non-empty array
                    if (!roles || roles.length <= 0) {
                        reject('getInfo: roles must be a non-null array!')
                    }

                    commit('SET_ROLES', roles)
                    commit('SET_USERINFO', data)
                    resolve(data)
                })
                .catch((error) => {
                    reject(error)
                })
        })
    },

    // user logout
    logout({ commit, state }) {
        removeToken() // must remove  token  first
        // resetRouter()
        commit('RESET_STATE')

    },

    // remove token
    resetToken({ commit }) {
        return new Promise((resolve) => {
            // removeToken() // must remove  token  first
            commit('RESET_STATE')
            resolve()
        })
    }
}

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