<template>
	<view class="hm-text-center">
		<view class="hm-header-xian"></view>
		<view class="wifi">
			<view class="hm-dis-block-inline">
				<u-icon :name="is_connected?'wifi':'wifi-off'" size="100" :color="is_connected?'#19be6b':'#fa3534'"></u-icon>
			</view>
			<view>
				<view class="hm-dis-block-inline">
					<u--text :color="is_connected?'#19be6b':'#fa3534'" :text="is_connected?'已连接':'未连接'" bold block>
					</u--text>
				</view>
			</view>
		</view>
		<view class="title">
			{{shop.ss_id}}
		</view>
		<view class="but">
			<u-button @click="connected" :loading="loading" :disabled="is_connected" type="success" text="链接">
			</u-button>
		</view>
		<!-- 技术支持 -->
		<view class="hm">
			{{applet.copyright}} <text class="v">V{{applet.version}}</text>
		</view>
	</view>
</template>

<script>
	const App = getApp();

	export default {
		data() {
			return {
				shop_id:'',
				shop: {},
				applet: {},
				is_connected: false,
				loading: false
			};
		},

		components: {},
		props: {},
		onLoad: function(options) {
			let _this = this; //接收小程序码参数
			_this.shop_id = options.shop_id;
			_this.applet = uni.getStorageSync('applet');
			_this.getShop(); //获取门店信息
		},
		methods: {
			/**
			 * 获取门店信息
			 */
			getShop: function() {
				let _this = this;
				App._get('shop/detail', {
					shop_id: _this.shop_id
				}, function(result) {
					_this.shop = result.data.detail;
					uni.setStorageSync('shop', result.data.detail);
					wx.startWifi({
						success(res) {
							if (res.errMsg == 'startWifi:ok') {
								wx.getConnectedWifi({
									success(res) {
										if (res.errMsg == 'getConnectedWifi:ok') {
											if (res.wifi.SSID == _this.shop.ss_id) {
												_this.is_connected = true;
												return true;
											}
										}
									}
								});
							} else {
								App.showError('WIFI模块启动失败');
							}
						}
					});
				});
			},
			/**
			 * 链接WIFI
			 */
			connected: function() {
				let _this = this;
				if (_this.shop.ss_id == '' || _this.shop.ss_key == '') {
					App.showError('商家未配置WIFI链接信息');
					return false;
				}
				_this.loading = true;
				wx.startWifi({
					success(res) {
						if (res.errMsg == 'startWifi:ok') {
							wx.connectWifi({
								SSID: _this.shop.ss_id,
								password: _this.shop.ss_key,
								success(res) {
									if (res.errMsg == 'connectWifi:ok') {
										_this.is_connected = true;
										_this.loading = false;
										App.showSuccess('WIFI链接成功', function() {
											App.goTo('index/index');
										});
									} else {
										_this.loading = false;
										App.showError('WIFI链接失败');
									}
								},
								fail(res) {
									_this.loading = false;
									if(res.errMsg == 'connectWifi:fail:wifi is disable'){
										App.showError('请先开启手机WIFI模块');
									}else{
										App.showError('WIFI链接失败');
									}
								}
							});
						} else {
							_this.loading = false;
							App.showError('WIFI模块启动失败');
						}
					}
				});
			}
		}
	};
</script>
<style lang="scss" scoped>
	page {
		background-color: #fff;
	}

	.wifi {
		padding-top: 50rpx;
	}

	.title {
		padding-top: 20rpx;
		font-size: 35rpx;
		font-weight: bold;
		font-family: "黑体";
	}

	.but {
		padding: 100rpx 30% 0 30%;

	}
</style>
