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

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

import api from '@/api/api.js'
import Position from '@/utils/position.js'

//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state: {
		configList: [], //配置列表
		shop : false,
		position : {//定位信息
			latitude : 0,
			longitude : 0,
		},
		userInfo : {},//用户信息
		bindShop : {},//酒店绑定的水洗店
		statistics : {},//水洗店查询首页统计
		category : [],//分类
	},
	getters: {
		// 角色 true为水洗店 false为酒店
		userShop(state){
			return state.shop
		}
	},
	mutations: {
		// 初始化配置
		initConfig(state){
			// api('getConfig', res => {
			// 	if(res.code == 200){
			// 		state.configList = res.result
			// 	}
			// })
			
			let config = ['getPrivacyPolicy', 'getUserAgreement']
			config.forEach(k => {
				api(k, res => {
					if (res.code == 200) {
						state.configList[k] = res.result
					}
				})
			})
		},
		//获取绑定店铺的数据 
		getBindShop(state){
			api('bindShop', res => {
				if(res.code == 200){
					state.bindShop = res.result
				}
			})
		},
		// 水洗店查询首页统计
		getLaundryStoreHomeData(state){
			api('laundryStoreHomeData', {}, res => {
				uni.stopPullDownRefresh()
				if(res.code == 200){
					state.statistics = res.result
				}
			})
		},
		// 获取分类
		categoryList(state){
			api('categoryList', res => {
				if(res.code == 200){
					state.category = res.result
				}
			})
		},
		// 账号密码登录
		accountLogin(state, form){
			api('wxLogin', {
				latitude : state.position.latitude,
				longitude : state.position.longitude,
				loginType : 1,
				...form,
			}, res => {
				if(res.code == 200){
					uni.setStorageSync('token', res.result.token)
					state.userInfo = res.result.userInfo
					state.shop = true
					uni.redirectTo({
						url: '/pages/index/index'
					})
				}
			})
		},
		login(state){
			
			let self = this
			
			uni.showLoading({
				title: '登录中...'
			})
			
			uni.login({
				success(res) {
					if(res.errMsg != "login:ok"){
						return
					}
					
					api('wxLogin', {
						code : res.code,
						latitude : state.position.latitude,
						longitude : state.position.longitude,
						loginType : 0,
					}, res => {
						
						uni.hideLoading()
						
						if(res.code != 200){
							return
						}
						
						state.userInfo = res.result.userInfo
						
						// state.shop = !!state.userInfo.shop
						
						uni.setStorageSync('token', res.result.token)
						
						self.commit('getUserInfo')
						
						if(!state.userInfo.nickName || !state.userInfo.headImage){
							uni.navigateTo({
								url: '/pages_order/auth/wxUserInfo'
							})
						}else{
							uni.navigateBack(-1)
						}
					})
				}
			})
		},
		getUserInfo(state){
			api('getInfo', res => {
				if(res.code == 200){
					state.userInfo = res.result
					
					state.shop = !!state.userInfo.shop
					
					if(state.shop){
						// 查询水洗店首页统计
						this.commit('getLaundryStoreHomeData')
					}else{
						//获取绑定店铺的数据
						this.commit('getBindShop')
					}
				}
			})
		},
		getPosition(state){
			Position.getLocation(res => {
				console.log(res);
				state.position = res
			})
			// console.log('wx.getFuzzyLocation');
			// uni.getFuzzyLocation({
			// 	type: 'wgs84',
			// 	isHighAccuracy: true,
			// 	highAccuracyExpireTime: 1000,
			// 	success (res) {
			// 		const latitude = res.latitude
			// 		const longitude = res.longitude
			// 		state.position = {
			// 			latitude,
			// 			longitude
			// 		}
			// 		console.log(res);
			// 	}
			// })
		},
		logout(state){
			uni.showModal({
				title: '确认退出登录吗',
				success(r) {
					if(r.confirm){
						state.userInfo = {}
						state.shop = false
						uni.removeStorageSync('token')
						uni.redirectTo({
							url: '/pages/index/index'
						})
					}
				}
			})
		},
	},
	actions: {},
})

export default store