import userApi from '@/api/modules/user.js'
import openApi from '@/api/modules/open.js'
const crypto = require("crypto");
const user = {
	state: {
		tokenObj: {
			token: '',
			date: 0
		},
		jpcjUserInfo: {

		},
		isVideoFree:"0",
		roadExamList:[],
		openExamRoom: [],
		userInfo: '',
		name: '',
		avatar: '',
		roles: [],
		permissions: [],
		lightConfig: {
			interval: 5,
			directives: 5,
			isTipSound: false,
			type: 0,
			typelist: [{
				name: "随机抽取多条指令"
			}, {
				name: "随机抽取一个组合"
			}]
		}
	},
	mutations: {
		SET_ISVIDEOFREE:(state,isVideoFree)=>{
			state.isVideoFree = isVideoFree

		},
		SET_ROADEXAMLIST:(state,roadExamList)=>{
			state.roadExamList = roadExamList
		},
		SET_OPENEXAMROOM: (state, openExamRoom) => {
			state.openExamRoom = openExamRoom
		},
		SET_TOKENOBJ: (state, tokenObj) => {
			state.tokenObj = tokenObj
		},
		//jpcjUserInfo
		SET_JPCJUSERINFO(state, jpcjUserInfo) {
			state.jpcjUserInfo = jpcjUserInfo
		},
		REFRESH_TOKEN(state) {
			if (state.tokenObj.token !== '')
				state.tokenObj.date = new Date().getTime()
		},
		SET_USERINFO: (state, userInfo) => {

			return state.userInfo = userInfo
		},
		SET_NAME: (state, name) => {
			state.name = name
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar
		},
		SET_ROLES: (state, roles) => {
			state.roles = roles
		},
		SET_PERMISSIONS: (state, permissions) => {
			state.permissions = permissions
		},
		SET_LIGHTCONFIG: (state, lightConfig) => {
			state.lightConfig = lightConfig
		}
	},
	actions: {
		/**
		 * 用户登录储存token
		 * @param {Object} store
		 */
		async Login(store) {
			wx.showLoading({
				title: '登陆中...',
			})
			let res = await userApi.login()
			console.log('调用了store的登陆')
			if (res.data) {
				store.commit('SET_TOKENOBJ', {
					token: res.data.token,
					date: new Date().getTime()
				})
			}

			// store.commit('SET_JPCJUSERINFO',res.data.jpcjUserInfo)
			wx.hideLoading()

			//#ifdef MP-TOUTIAO
			uni.setStorageSync('unionid', res.data.thirdResult.data.unionid)
			//#endif
			return res
		},
		async getToken(store) {
			let tokenObj = store.state.tokenObj
			if (tokenObj.token) {
				return tokenObj.token
			} else {
				let res = await store.dispatch("Login")
				if (res.data) {
					store.commit('SET_TOKENOBJ', {
						token: res.data.token,
						date: new Date().getTime()
					})
				}

				return res.data && res.data.token
			}
		},
		async GetRoadExamVip(store) {
			let res =await userApi.studentVipExamList()
			let rows = res.rows
			store.commit('SET_ROADEXAMLIST',rows)
		},
		async GetRoadExamIsAllFree(store){
			let res = await openApi.systemDictDataType("nbjk_set")
			let IS_VIDEO_FREE = res.data.filter(item=>{
				return item.dictLabel=='IS_VIDEO_FREE'
			})
			console.log(IS_VIDEO_FREE,"IS_VIDEO_FREE")
			store.commit("SET_ISVIDEOFREE",IS_VIDEO_FREE[0].dictValue)
			
		},
		/**
		 * 获取用户信息
		 * @param {Object} store
		 */
		async GetInfo(store) {
			let res
			//#ifdef MP-WEIXIN
			res = await userApi.getInfo()
			//#endif
			//#ifdef MP-TOUTIAO
			res = await userApi.userInfoVipInfo()
			//#endif			
			uni.setStorageSync("userInfo", res.data);
			store.commit('SET_USERINFO', res.data)
			return res
		},
		async getOpenExamRoom(store) {

			userApi.studentVipExamList().then(res => {
				store.commit('SET_OPENEXAMROOM', res.rows)
			})
			console.log(store.state.openExamRoom)

		}
	}
}

export default user
