import { getState, setState } from '@/store/modules/vuexStorange'
import { getSelfInfo } from '@/utils/auth'

/** 员工用户信息 */
const state = {
	selfInfo: null,
	user: null,
	shopUser: null
}

const getters = {
	// 员工用户信息
	getSelfInfo: (state) => {
		getState(state, 'selfInfo', 'selfInfo')
		// if (state.selfInfo) return state.selfInfo
		// let data = {}
		// getSelfInfo().then(res => {
		// 	data = res
		// })
		// return data
		return state.selfInfo
	},
	// 用户信息
	getUser: (state) => {
		getState(state, 'user', 'userInfo')
		return state.user
	},
	// 商城用户信息
	getShopUser: (state) => {
		getState(state, 'shopUser', 'shopUser')
		return state.shopUser
	}
}

const actions = {
	// 员工用户信息
	setSelfInfo({
		commit,
		dispatch,
		getters,
		rootGetters,
		rootState,
		state
	}, userInfo) {
		commit('SET_BASIC_USER', userInfo)
	},
	delSelfInfo({
		commit,
		dispatch,
		getters,
		rootGetters,
		rootState,
		state
	}) {
		commit('DEL_BASIC_USER')
	},
	// 后台管理用户信息
	setUser({
		commit,
		dispatch,
		getters,
		rootGetters,
		rootState,
		state
	}, userInfo) {
		commit('SET_USER', userInfo)
	},
	delUser({
		commit,
		dispatch,
		getters,
		rootGetters,
		rootState,
		state
	}) {
		commit('DEL_USER')
	},
	// 商城用户信息
	setShopUser({
		commit,
		dispatch,
		getters,
		rootGetters,
		rootState,
		state
	}, userInfo) {
		commit('SET_SHOP_USER', userInfo)
	},
	delShopUser({
		commit,
		dispatch,
		getters,
		rootGetters,
		rootState,
		state
	}) {
		commit('DEL_SHOP_USER')
	}
}

const mutations = {
	// 员工用户信息
	SET_BASIC_USER(state, userInfo = {}) {
		setState(state, userInfo, 'selfInfo', 'selfInfo')
	},
	DEL_BASIC_USER(state) {
		state.selfInfo = ''
	},
	// 后台管理用户信息
	SET_USER(state, userInfo = {}) {
		setState(state, userInfo, 'user', 'userInfo')
	},
	DEL_USER(state) {
		state.user = ''
	},
	// 商城用户信息
	SET_SHOP_USER(state, userInfo = {}) {
		setState(state, userInfo, 'shopUser', 'shopUser')
	},
	DEL_USER(state) {
		state.shopUser = ''
	}
}

export default {
	state,
	getters,
	actions,
	mutations
}
