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

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

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

//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state: {
		configList: {}, //配置列表
		configListMax: {}, //配置列表
		priceMap : {},//各项费用配置
		shop : false,
		userInfo : {}, //用户信息
		city : {},//当前城市
		cityList : [],//城市列表
		category : [],//动态分类
		headInfo : [],//首页配置
		vipLogList : [],//当前用户vip开通记录，用于计算结束时间
	},
	getters: {
		// 角色 true为水洗店 false为酒店
		userShop(state){
			return state.shop
		}
	},
	mutations: {
		// 初始化配置
		initConfig(state){
			api('getOrderPrice', res => {
				if(res.code == 200){
					res.result.forEach(n => {
						state.priceMap[n.keyValue] = n.price
					})
				}
			})
			
			api('getConfig', res => {
				if(res.code == 200){
					res.result.forEach(n => {
						state.configList[n.keyIcon] = n.keyContent || n.keyDetails || n.keyImage;
						state.configList[n.keyIcon + '_keyValue'] = n.keyValue;
						
						
						state.configListMax[n.keyIcon] = n;
					});
				}
			})
			
			let config = ['getPrivacyPolicy', 'getUserAgreement']
			config.forEach(k => {
				api(k, res => {
					if (res.code == 200) {
						state.configList[k] = res.result
					}
				})
			})
		},
		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.shareId = 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 || !state.userInfo.phone){
							uni.navigateTo({
								url: '/pages_order/auth/wxUserInfo'
							})
						}else{
							uni.navigateBack(-1)
						}
					})
				}
			})
		},
		getUserInfo(state){
			api('getInfo', res => {
				if(res.code == 200){
					state.userInfo = res.result
					
					if(!state.userInfo.nickName || !state.userInfo.headImage || !state.userInfo.phone){
						uni.navigateTo({
							url: '/pages_order/auth/wxUserInfo'
						})
					}
				}
			})
		},
		// 获取城市
		getCityList(state){
			api('getCityList', res => {
				if(res.code == 200){
					state.cityList = res.result
					state.city = res.result[0] || {}
				}
			})
		},
		// 获取动态分类
		getCategory(state){
			// 发起请求
			api('getClassInfo', res => {
				if(res.code == 200){
					state.category = res.result
				}
			})
		},
		// 退出登录
		logout(state){
			uni.showModal({
				title: '确认退出登录吗',
				success(r) {
					if(r.confirm){
						state.userInfo = {}
						state.role = false
						uni.removeStorageSync('token')
						uni.redirectTo({
							url: '/pages/index/index'
						})
					}
				}
			})
		},
		// 获取小程序首页配置
		getIndexHeaderInfo(state){
			api('getIndexHeaderInfo', res =>{
				if(res.code == 200){
					state.headInfo = res.result
				}
			})
		},
		// 查询当前开通会员以及过期时间
		getMemberInfo(state){
			api('getMemberInfo', res =>{
				if(res.code == 200){
					state.vipLogList = res.result
				}
			})
		},
	},
	actions: {},
})

export default store