import Vue from 'vue'
import Vuex from 'vuex'
import api from '@/common/api.js'
Vue.use(Vuex);
const store = new Vuex.Store({
	state: {
		login: true,
		token: '',
		avatarUrl: '',
		userName: '',
		userInfo: null,
		dictObj: null,
		allowLoading: true,
		goodsAuditionConfigIdList: [], //当前访问页面的试听节ID
		shoppingCartList: [], //购物车支付商品
		applyData: {}, //预约考试数据存放
		playSectionId: 0,
		copyData: null, //存放审核资料数据
	},
	getters: {
		userInfo: state => {
			if (state.userInfo == null) {
				if (uni.getStorageSync('user_account')) {
					getUserInfo(state)
				}
			}
			return state.userInfo
		},
		dictObj: state => {
			if (state.dictObj == null) {
				api.dictList().then(res => {
					if (res.data.code === 200) {
						let newList = {}
						let list = res.data.data
						for (let i = 0; i < list.length; i++) {
							let item = list[i]
							if (newList.hasOwnProperty(item.dictType)) {
								newList[item.dictType].push(item.dictLabel)
							} else {
								newList[item.dictType] = [item.dictLabel]
							}
						}
						state.dictObj = newList;
					}
				});
			}
			return state.dictObj
		},
		allowLoading: state => {
			return state.allowLoading
		},
		goodsAuditionConfigIdList: state => {
			return state.goodsAuditionConfigIdList
		},
		shoppingCartList: state => {
			return state.shoppingCartList
		},
		getApplyData: state => {
			return state.applyData
		},
		playSectionId: state => {
			return state.playSectionId
		},
		getCopyData: state => state.copyData
	},
	mutations: {
		updataCopyData(state, objs) {
			state.copyData = objs
		},
		updateApplyData(state, arrays) {
			state.applyData = arrays;
		},
		updateUserInfo(state, provider) {
			state.userInfo = provider.userInfo;
		},
		setGoodsAuditionConfigIdList(state, provider) {
			state.goodsAuditionConfigIdList = provider.goodsAuditionConfigIdList;
		},
		setShoppingCartList(state, provider) {
			state.shoppingCartList = provider.shoppingCartList;
		},
		setPlaySectionId(state, provider) {
			state.playSectionId = provider.playSectionId;
		},
	}
})
async function getUserInfo(state) {
	const resdata = await api.getInfo()
	if (resdata.data.code == 200) {
		state.userInfo = resdata.data.data;
	}
}
export default store
