import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex); //vue的插件机制

import api from '@/api/api.js'
import fetch from '@/api/fetch.js'

const setUserInfo = (state, data) => {
	const userInfo = data

	switch (userInfo.role) {
		case '0':
			userInfo.roleDesc = '家长'
			break
		case '1':
			userInfo.roleDesc = '学生'
			break
		default:
			break
	}

	state.userInfo = userInfo

}

//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state: {
		configList: {}, //配置列表
		shop : false,//身份判断如果不需要，可以删除
		userInfo : {}, //用户信息
		userCenterData: {},
		travelerList: null,
		orderInfo: null,
		couponInfo: null,
		memberInfo: null,
		liveInfo: null,
		markmeList: [],
	},
	getters: {
		// 角色 true为水洗店 false为酒店 ： 身份判断如果不需要，可以删除
		userShop(state){
			return state.shop
		}
	},
	mutations: {
		// 初始化配置
		async initConfig(state) {
			const records = (await fetch('getConfig'))?.records
			const configList = {
				...state.configList,
			}

			records.forEach(n => {
				configList[n.paramCode] = n.paramImage || n.paramText || n.paramTextarea;
			});

			const periodList = (await fetch('queryPeriodList')).records
			configList['periodList'] = periodList

			const commentOptionList = (await fetch('queryCommentOptionList')).records
			configList['commentOptionList'] = commentOptionList

			const experienceQuestionList = (await fetch('queryExperienceQuestionList')).records
			configList['experienceQuestionList'] = experienceQuestionList

			state.configList = configList

			uni.$emit('initConfig', state.configList)

		},
		// 微信登录
		login(state){
			uni.showLoading({
				title: '登录中...'
			})
			uni.login({
				success(res) {
					if(res.errMsg != "login:ok"){
						return
					}
            
					let data = {
						code: res.code,
					}

					if (uni.getStorageSync('shareId')) {
						data.vid = uni.getStorageSync('shareId')
					}
					
					api('wxLogin', data, res => {
						
						uni.hideLoading()
						
						if(res.code != 200){
							return
						}
						
						state.userInfo = res.result.userInfo
						uni.setStorageSync('token', res.result.token)
						
						if(!state.userInfo.nickName || !state.userInfo.headImage){
							uni.navigateTo({
								url: '/pages_order/auth/wxUserInfo'
							})
						} else if (!state.userInfo.role) {
							uni.navigateTo({
								url: '/pages_order/auth/roleChoose'
							})
						} else {
							uni.navigateBack(-1)
						}
					})
				}
			})
		},
		// 获取用户个人信息
		getUserInfo(state){
			api('getInfo', res => {
				if(res.code == 200){
					setUserInfo(state, res.result)
									
					if (!state.userInfo.role) {
						uni.navigateTo({
							url: '/pages_order/auth/roleChoose'
						})
					}
				}
			})
		},
		// 获取个人基础数据
		getUserCenterData(state){
			api('queryUserCenterData', res => {
				if(res.code == 200){
					state.userCenterData = res.result
				}
			})
		},
		// 退出登录
		logout(state){
			uni.showModal({
				title: '确认退出登录吗',
				success(r) {
					if(r.confirm){
						state.userInfo = {}
						state.role = false
						uni.removeStorageSync('token')
						uni.reLaunch({
							url: '/pages/index/index'
						})
					}
				}
			})
		},
		clearUserInfo(state) {
			state.userInfo = {}
			state.role = false
			uni.removeStorageSync('token')
		},
		setTravelerList(state, data) {
			state.travelerList = data
		},
		setOrderInfo(state, data) {
			state.orderInfo = data
		},
		setCouponInfo(state, data) {
			state.couponInfo = data
		},
		setMemberInfo(state, data) {
			console.log('memberInfo', data)
			state.memberInfo = data
		},
		setLiveInfo(state, data) {
			state.liveInfo = data
		},
		setMarkmeList(state, data) {
			state.markmeList = data
		},
	},
	actions: {
		async collect(state, activityId) {
			console.log('collect', activityId)

			try {

				const res =  await fetch('collectionActivity', { activityId }, false)
				
				uni.showToast({
					icon: 'success',
					title: res.message,
				});

				return true
			} catch (err) {
				console.log('collect err', err)
				uni.showToast({
					icon: 'error',
					title: err.message,
				});
				return false
			}
		},
	},
})

export default store