import Vue from 'vue'
import Vuex from 'vuex';
import $http from '../utils/http.js'
import $util from '../utils/util.js'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		setColor: '',
		color: '#a07c5a',
		is_show: false,
		userInfo: uni.getStorageSync('userInfo') || '',
		prevent: true,
		store_id: '',
		table_id: '',
		turntable_id: '',
		systemInfo: {},
		teen_index: 1,
		address: {},
		app_id: '',
		tabActive: 1,
		is_Refresh: true,
		cate_id: null,
		dining_index: 1,
		couponList: [],
		audioList: [],
		swiperList: [],
		is_business: true,
		is_creatOrder: false, //更新购物车
		descFood: {},
		// descTake:"外卖",
		descOrder: {},
		merchant_id:null,
		vx_carGoods:[],
	},

	getters: {
		systemInfo(state) {
			return state.systemInfo
		},
		userInfo(state) {
			return state.userInfo
		},
		store_id(state) {
			return state.store_id
		},

	},

	mutations: {
		SET_CARGOODS(state, desc){
			state.vx_carGoods = desc
		},
		 // 分账号授权id
		SET_MERCHANT_ID(state, desc){
			state.merchant_id = desc
		},
		// 下单页面文字
		SET_ORDER(state, desc) {
			state.descOrder = desc
		},
		// 点餐页面文字
		SET_DESC(state, desc) {
			state.descFood = desc
		},

		// 更新购物车
		SET_CREAT(state, creat) {
			state.is_creatOrder = creat
		},

		// 营业时间
		SET_BUSINESS(state, checked) {
			state.is_business = checked
		},

		// 弹窗优惠劵列表
		SET_SWIPER(state, list) {
			state.swiperList = list;
		},

		// 弹窗语音列表
		SET_AUDIO(state, list) {
			state.audioList = list;
		},

		// 弹窗优惠劵列表
		SET_COUPON(state, list) {
			state.couponList = list;
		},

		// 堂食1、自提2
		SET_SELFMENTION(state, index) {
			state.dining_index = index;
		},

		// 分类id SET_CATE_ID
		SET_CATE_ID(state, id) {
			// console.log(id, "cate_idcate_id")
			state.cate_id = id;
		},

		//支付宝切换页面标题名称不隐藏
		SET_REFRESH(state, Refresh) {
			state.is_Refresh = Refresh;
		},

		//订单状态跳转1已完成，2待完成
		SET_TABACTIVE(state, tabActive) {
			state.tabActive = tabActive;
		},

		// 设置颜色
		SET_MONITOR_COLOR(state, color) {
			state.setColor = color;
			state.color = color;
		},

		//门店id
		SET_STORE_ID: (state, store_id) => {
			state.store_id = store_id;
			uni.setStorageSync('store_id', store_id)
		},

		// 设置餐桌id
		SET_TABLE_ID: (state, table_id) => {
			state.table_id = table_id;
			uni.setStorageSync('table_id', table_id)
		},
		// 大转盘id
		SET_TURNTABLE_ID: (state, turntable_id) => {
			state.turntable_id = turntable_id;
			uni.setStorageSync('turntable_id', turntable_id)
		},
		// 获取配置信息
		SET_SYSTEM: (state, system) => {
			state.systemInfo = system;
			uni.setStorageSync('systemInfo', system)
		},

		// 获取用户信息
		SET_USER_INFO: (state, userInfo) => {
			state.userInfo = userInfo;
			uni.setStorageSync('userInfo', userInfo)
			uni.setStorageSync('user_id', userInfo.id)
		},

		// 获取堂食/外卖
		SET_TEEN_INDEX: (state, index) => {
			// console.log(index, "SET_TEEN_INDEX")
			state.teen_index = index;
		},

		// 获取堂食/自提
		SET_MENTION: (state, index) => {
			state.teen_index = index;
		},

		// 地址
		SET_ADDRESS(state, address) {
			state.address = address
		},
		// 全局appid
		SET_APP_ID(state, id) {
			state.app_id = id
		},
		// 获取手机授权弹窗
		showAttrPicker(state, picker) {
			state.is_show = picker;
		},
		// 重置扫码进入获取到的门店id
		SET_PREVENT(state, prevent) {
			state.prevent = prevent;
		},
	},

	actions: {
		// 获取颜色
		getSystemColor({
			commit,
			state
		}) {
			if (!state.store_id) {
				return
			}
			$http.request('xcx/store_color', {
				store_id: state.store_id,
			}, true).then(res => {
				commit('SET_MONITOR_COLOR', res.data.color || '#3387FF')
			})
		},
		// 获取店铺信息
		async getSystemStup({
			commit,
			state
		}) {
			
			if (!state.store_id) {
				return
			}
			
			await $http.request('xcx/system', {
				store_id: state.store_id,
				table_id: state.table_id,
			}, true).then(res => {
				// console.log(state.store_id,"state.store_id")
		       let set_index = res.data.is_hall==1?1:res.data.is_waimai==1?2:res.data.is_ziti==1?3:0;
		 
		 
		 // console.log("systemData",res.data);
			   commit('SET_TEEN_INDEX', set_index);
				commit('SET_TABLE_ID', res.data.table_id)
				commit('SET_SYSTEM', res.data)
				commit('SET_PREVENT', false)
				if (res.data.set.desc) {
					var desc = {
						food1: res.data.set.desc.split(",")[0] || '下单',
						food2: res.data.set.desc.split(",")[1] || '配送'
					}
				} else {
					var desc = {
						food1: '下单',
						food2: '配送'
					}
				}
				commit('SET_DESC', desc)
				if (res.data.set.eat_mode) {
					var desc = {
						title: res.data.set.eat_mode.split(",")[0] || '购买方式',
						food1: res.data.set.eat_mode.split(",")[1] || '店内下单',
						food2: res.data.set.eat_mode.split(",")[2] || '到店自取',
					}
				} else {
					var desc = {
						title: '购买方式',
						food1: '店内下单',
						food2: '到店自取'
					}
				}
				commit('SET_ORDER', desc)
				$http.request('xcx/template', {
					admin_id: res.data.admin_id,
				}, true).then(r => {
					if (r.code == '200') {
						uni.setStorageSync('balance_subscribe', r.data)
					}
				})
				let {
					time_content,
					is_rest
				} = res.data;
				function nowTime() { //获取当前时间
					let now = new Date();
					let _hour = (10 > now.getHours()) ? '0' + now.getHours() : now.getHours();
					let _minute = (10 > now.getMinutes()) ? '0' + now.getMinutes() : now
						.getMinutes();
					let _second = (10 > now.getSeconds()) ? '0' + now.getSeconds() : now
						.getSeconds();
					return _hour + ':' + _minute + ':' + _second;
				}
				// is_rest==1休息 2设置休息时间
				if (is_rest == 2) {
					// console.log("设置休息时间",time_content)
					if (!time_content) {
						commit('SET_BUSINESS', true)
					} else {
						for (let i of time_content) {
							commit('SET_BUSINESS', false)
							if ($util.time_range(i.start, i.end, nowTime())) {
								commit('SET_BUSINESS', true)
								return
							}
						}
					}
				}
				if (is_rest == 1) {
					commit('SET_BUSINESS', false)
				}
			})
		},
		// 获取用户信息
		async getOpenid({
			commit,
			state
		}) {
			// #ifdef H5  
			//临时用户信息配置
			// let tempCode="0d1YErFa1uLuhG0i3vGa1ZleM21YErFZ";
			let openid="obOpI5GhysDLz6F7neu8RkNdeew8";  
			let params = {
				openid: openid,
				type: 1,
				store_id: state.store_id
			} 
			$http.request('xcx/login', params, true).then(
				res => { 
					commit('SET_USER_INFO', res.data)
					// 获取默认地址
					// console.log("获取默认地址")
					$http.request('xcx/MyDefaultAddress', {
						user_id: res.data.id,
					}, true).then(res => {
						commit('SET_ADDRESS', res.data)
					}).catch(err => {})
					
					
			}).catch(err => {})
		
			
			
			// 	$http.request('xcx/openid', {
			// 		appid: state.app_id || '',
			// 		code: tempCode,
			// 		type:1,
			// 		store_id: state.store_id
			// 	}, true).then(res => {
			// 		// console.log(res.data,"data")
			// 		uni.setStorageSync('user_xcx_info', res.data);
					
			// 	})
			 
			
			// #endif
			 
			// #ifndef H5 
			uni.login({
				success: res => {
					console.log("start",res);
					let type, info = {};
					// #ifdef MP-WEIXIN
					type = 1
					// #endif
					// #ifdef MP-ALIPAY
					type = 2
					// #endif
					// console.log(res.code,"code")
					
					$http.request('xcx/openid', {
						appid: state.app_id || '',
						code: res.code,
						type,
						store_id: state.store_id
					}, true).then(res => {
						// console.log(res.data,"data")
						uni.setStorageSync('user_xcx_info', res.data);
						let params;
						// #ifdef MP-WEIXIN
						params = {
							openid: res.data.openid,
							type: 1,
							store_id: state.store_id
						}
						// #endif
						// #ifdef MP-ALIPAY
						params = {
							appid: state.app_id || '',
							user_id: res.data.user_id,
							type: 2,
							store_id: state.store_id
						}
						// #endif
						$http.request('xcx/login', params, true).then(
							res => {
								commit('SET_USER_INFO', res.data)
								// 获取默认地址
								// console.log("获取默认地址")
								$http.request('xcx/MyDefaultAddress', {
									user_id: res.data.id,
								}, true).then(res => {
									commit('SET_ADDRESS', res.data)
								}).catch(err => {})
							}).catch(err => {})
					})
				},
				complete: res => {}
			})
			// #endif
		},

	}
})
export default store
