import {
	login,
	logout,
	getInfo
} from '@/api/user'
import {
	getToken,
	setToken,
	removeToken,
	setName,
	setLevel
} from '@/utils/auth'
import {
	resetRouter
} from '@/router'

const getDefaultState = () => {
	return {
		token: getToken(),
		name: '',
		level: '',
		avatar: '',
		user_name: '',
		phone: ''
	}
}

const state = getDefaultState()

const mutations = {
	RESET_STATE: (state) => {
		Object.assign(state, getDefaultState())
	},
	SET_TOKEN: (state, token) => {
		state.token = token
	},
	SET_NAME: (state, name) => {
		state.name = name
	},
	SET_PHONE: (state, phone) => {
		state.phone = phone
	},
	SET_USER_NAME: (state, user_name) => {
		state.user_name = user_name
	},
	SET_PWD: (state, pwd) => {
		state.pwd = pwd
	},
	SET_LEVEL: (state, level) => {
		state.level = level
	},
	SET_AVATAR: (state, avatar) => {
		state.avatar = avatar
	}
}

const actions = {
	// user login
	login({
		commit
	}, userInfo) {
		const {
			username,
			password
		} = userInfo
		return new Promise((resolve, reject) => {
			login({
				username: username.trim(),
				password: password.trim()
			}).then(response => {
				if (response.code == 200) {
					// 修改：token固定
					const token = 'admin_token'
					commit('SET_TOKEN', token)
					setToken(token)
					setName(response.data[0].admin_name)
					setLevel(response.data[0].level)
					// 添加：
					commit('SET_LEVEL', response.data[0].level)
					commit('SET_NAME', response.data[0].admin_name)
					commit('SET_PWD', password)
					commit('SET_PHONE', response.data[0].phone)
					commit('SET_USER_NAME', response.data[0].user_name)
				} else {
					commit('SET_TOKEN', '')
					commit('SET_LEVEL', '')
					commit('SET_NAME', '')
					commit('SET_PWD', '')
					commit('SET_PHONE', '')
					commit('SET_USER_NAME', '')
				}
				// commit('SET_TOKEN', data.token)
				// setToken(data.token)
				resolve(response)
			}).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) {
	// 				return reject('验证失败，请重新登录。')
	// 			}

	// 			const {
	// 				name,
	// 				avatar
	// 			} = data

	// 			commit('SET_NAME', name)
	// 			commit('SET_AVATAR', avatar)
	// 			resolve(data)
	// 		}).catch(error => {
	// 			reject(error)
	// 		})
	// 	})
	// },

	// user logout
	logout({
		commit,
		state
	}) {
		return new Promise((resolve, reject) => {
			removeToken() // must remove  token  first
			resetRouter()
			commit('RESET_STATE')
			commit('SET_NAME', '')
			commit('SET_PWD', '')
			commit('SET_LEVEL', '')
			commit('SET_TOKEN', '')
			commit('SET_PHONE', '')
			commit('SET_USER_NAME', '')
			resolve()
		})
		// 修改：下面被注释了
		// return new Promise((resolve, reject) => {
		//   logout(state.token).then(() => {
		//     removeToken() // must remove  token  first
		//     resetRouter()
		//     commit('RESET_STATE')
		//     resolve()
		//   }).catch(error => {
		//     reject(error)
		//   })
		// })
	},

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

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