<script>
	import siteinfo from './siteinfo.js';
	import $http from './utils/http.js'
	let q, store_id, table_id, turntable_id, app_id, defInfo = {},merchant_id,
		number = 0; //门店id 餐桌id  大转盘id
	export default {
		onLaunch: function(options) {
			uni.hideTabBar();
			uni.removeStorageSync('baseUrl');
		},
		async onShow(options) {
			uni.hideTabBar();
			this.initSocket();
			let query = options.query,
				queryCode = null;
			if (query && query.share_user_id) {
				uni.setStorageSync('share_user_id', query.share_user_id)
			}
	
			// #ifdef MP-ALIPAY
			 // 合伙人邀请
			if (query && query.give_user_id) {
				let use= {
				   store_id:query.store_id,
				   give_user_id:query.give_user_id
				}	
				getApp().globalData.give_user = use
				    //由于这里是网络请求，可能会在 Page.onLoad 之后才返回
				     // 所以此处加入 callback 以防止这种情况
				if (this.employIdCallback){
				     
				}
			}
			
			if (query) {
				queryCode = query.qrCode || '';
			}
			
			// #endif
		
			// #ifdef MP-WEIXIN
			queryCode = query.q;
			// #endif
			
			//跳转选择地址的时候,返回会走默认门店,阻止默认门店
			if (!this.$store.state.prevent && !queryCode) {
				return
			}
			if (queryCode) {
				q = decodeURIComponent(queryCode)
                 if(q.indexOf("?") == -1){
					let code;
					 code = q.split("/smdc/");
					 code = code[1]?code:q.split("/zhjy/"); 
					await $http.request('yy/empty_code', {
						 code:code[1]
					 }).then(res => {
						store_id = res.data.store_id;
						table_id = res.data.table_id;
						merchant_id = '';
					 })
				 }else{
					 store_id = await this.gettParameter(q, 'store_id')
					 table_id = await this.gettParameter(q, 'table_id')
					 turntable_id = await this.gettParameter(q, 'id')
					 merchant_id = await this.gettParameter(q, 'merchant_id')
				 }
				// console.log(merchant_id,q,"merchant_id")
			}else {
				// #ifdef MP-ALIPAY
				defInfo = await my.getExtConfigSync()
				// #endif
				// #ifdef MP-WEIXIN
				defInfo = await wx.getExtConfigSync()
				// #endif
				store_id = defInfo.store_id;
				table_id = defInfo.table_id;
				app_id = defInfo.appId;
			}
			
			// var store_id=176,table_id=211;
			this.$store.commit('SET_STORE_ID', 3)
			this.$store.commit('SET_MERCHANT_ID', merchant_id)
			this.$store.commit('SET_TABLE_ID', 1)
			this.$store.commit('SET_TURNTABLE_ID', turntable_id)
			this.$store.commit('SET_APP_ID', app_id)
			this.$store.dispatch('getSystemColor', store_id);
			this.$store.dispatch('getSystemStup', store_id);
			this.$store.dispatch('getOpenid', store_id);
			// 更新提示
			this.getUpdateManager();
			//判断打烊
			this.getIsClose()
		},

		onHide: function() {
			getApp().globalData.isApp = true; //清除控制赋能，只赋能一次
		},

		onUnload() {},
 
		methods: {
			getIsClose(options) {
                // console.log('options', options)
                $http.request('xcx/system', {
                    store_id: uni.getStorageSync('store_id'),
                    table_id: uni.getStorageSync('table_id'),
                }).then(res => {
                    // console.log(res)
                    // #ifdef MP-ALIPAY
                    if (res.data.is_alipay == 2) {
                        uni.reLaunch({
                            url: '/pages/closeInfo/closeInfo'
                        })
                    }
                    // #endif
			         // #ifdef MP-WEIXIN
                    if (res.data.is_wechat == 2) {
                        uni.reLaunch({
                            url: '/pages/closeInfo/closeInfo'
                        })
                    }
                    // #endif
			       })
            },
			

			

			//统一发送消息，可以在其他页面调用此方法发送消息
			sendSocketMessage: function(msg) {
				let that = this
				return new Promise((resolve, reject) => {
					if (this.globalData.localSocket.readyState === 1) {
						uni.sendSocketMessage({
							data: msg,
							success: function(res) {
								resolve(res)
								that.globalData.readyReset = 1;
							},
							fail: function(e) {
								reject(e)
							}
						})
					} else {
						// console.log('已断开')
						that.globalData.readyReset = 2;
					}
				})
			},

			storeColor() {
				let that = this;
				return new Promise((resolve, reject) => {
					$http.request('xcx/store_color', {
						store_id: store_id,
					}, "", "", true).then(res => {
						resolve(res)
					})
				})
			},

			storeSystem() {
				return new Promise((resolve, reject) => {
					$http.request('xcx/system', {
						store_id: store_id,
						table_id: table_id,
					}, "", "", true).then(res => {
						resolve(res)
						$http.request('xcx/template', {
							admin_id: res.data.admin_id,
						}, "", "", true).then(r => {
							if (r.code == '200') {
								uni.setStorageSync('balance_subscribe', r.data)
							}
						})
					})
				})
			},

			getUpdateManager: function() {
				const updateManager = uni.getUpdateManager(); // 获取更新管理器对象
				updateManager.onCheckForUpdate(function(res) {
					if (res.hasUpdate) {
						updateManager.onUpdateReady(function() {
							uni.showModal({
								title: '更新提示',
								content: '新版本已经准备好，是否重启应用?',
								showCancel: false,
								success: res => {
									if (res.confirm) {
										updateManager.applyUpdate();
									}
								}
							})
						})
						updateManager.onUpdateFailed(function() {
							uni.showModal({
								title: '提示',
								content: '检查到有新版本，但是下载失败，请检查网络设置',
								showCancel: false
							})
						})
					}
				})
			},
			gettParameter: function(url, name) {
				let val = ''
				if (url.indexOf('?' + name + '=') !== -1) {
					val = url.split('?' + name + '=')[1].split(/&|#/)[0];
				} else if (url.indexOf('&' + name + '=') !== -1) {
					val = url.split('&' + name + '=')[1].split(/&|#/)[0];
				}
				return val
			},
		},

		globalData: {
			localSocket: {},
			callback: function() {},
			closeCallback: function() {},
			onSocketOpen: function() {},
			URL: siteinfo.siteroot,
			isApp: true,
			SocketOpen: false,
			readyState: 0,
			readyReset: 1,
			give_user:{
				store_id:'',
				give_user_id:''
			}
		}
	}

</script>

<style lang="scss">
    @import url("/static/css/iconfont.css");
    @import url("/static/css/app.css");
</style>
