<template>
	<view class="weightPages">
		<view class="content ">
			<view class="title">{{$t("linkBluetooth.linkBluetoothSuccess")}}</view>
			<view class="text">{{text}}</view>
			<view class="image">
				<image src="/pageTwo/static/PCL.gif" class="image3"></image>
			</view>
			<view class="tips">
				<text>{{$t("tips.msgTitle")}}:</text>
				<text>{{$t("linkBluetooth.onDeviceMeasureTips")}}</text>
				<text>{{$t("linkBluetooth.openDeviceeMeasureTips")}}</text>
			</view>
		</view>
		<!-- 手动记录 -->
		<view class="wrapper" v-if="isHeight">
			<view class="bg"></view>
			<view class="Blue">
				<view class="h4">{{$t("linkBluetooth.measureResultTips")}}</view>
				<view class="Blue-box">
					{{$t("linkBluetooth.nowWeight")}}<text>{{weight}}{{unit}}</text>
				</view>
				<view class="Blue-box">
					{{$t("linkBluetooth.lastHeight")}}<input v-model="height" type="digit" :placeholder="$t('tips.verifyHeight')" />cm
				</view>
				<view class="Blue-btn Blue-close" @click="handleBack">{{$t("tips.btnSancellation")}}</view>
				<view class="Blue-btn" @click="handleGetMeasure">{{$t("linkBluetooth.SaveResult")}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	let myTime;
	export default {
		data() {
			return {
				text: "",
				imp: "",
				weight: "",
				height: "",
				deviceId: "",
				unit: "kg",
				Unload: false,
				isHeight: false,
				isConnection: 0,
				isdevice: false,
				stopblue: false,
				typeInfo: -1,
			}
		},
		computed: {
			...mapState(["user"]),
			info() {
				return this.user
			},
		},
		onUnload: function() {
			let that = this
			if (!that.Unload) {
				clearTimeout(myTime)
				that.closeBluetoothAdapter()
				that.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
				let pages = getCurrentPages();
				pages[pages.length - 2].$vm.getBlereload();
				console.log("页面返回onUnload", pages, pages[pages.length - 2])
			}
		},
		onLoad(options) {
			let that = this
			that.text = ""
			that.typeInfo = -1
			that.stopblue = false
			that.deviceId = options.deviceId
			that.height = that.user.height
			that.onBluetoothDeviceFound()
		},
		watch: {
			stopblue: function() {
				let that = this
				if (!that.stopblue) {
					clearTimeout(myTime);
					that.isHeight = false
					return
				}
				if (that.stopblue && that.typeInfo == '1') {
					clearTimeout(myTime);
					that.isHeight = true
				}
			}
		},
		methods: {
			/**
			 * 发现外围设备
			 */
			onBluetoothDeviceFound() {
				var that = this;
				uni.onBluetoothDeviceFound(res => {
					res.devices.forEach(device => {
						device.advertisData = device.advertisData ? device.advertisData : ''
						if (device.name.toLowerCase().indexOf("da") !== -1) {
							clearTimeout(myTime);
							let value = that.$tools.ab2hex(device.advertisData, "")
							let type = value.substring(0, 1) //d测量，e锁定
							let type2 = value.substring(1, 2) //0~9体脂，A~F体重
							let weight = parseInt(value.substring(2, 6), 16) / 100
							console.log("VALUE", value, type, isNaN(type2), weight)
							if (type == "d") {
								that.typeInfo = 0
								that.stopblue = false
								that.text = that.$t("linkBluetooth.realTimeWeightText") + weight + 'kg'
							}
							if (type == "e") {
								that.typeInfo = 1
								that.text = that.$t("linkBluetooth.StableWeightText") + weight + 'kg'
								that.imp = !isNaN(type2) ? parseInt(value.substring(6, 10), 16) : ''
								that.weight = weight
								that.stopblue = true
								return
							}
							return;
						}
					})
				});
				// that.handleMyTime()
			},
			handleMyTime() {
				var that = this;
				myTime = setTimeout(function() {
					if (!that.macAddr) {
						clearTimeout(myTime);
						that.text = ""
						that.Unload = true
						that.isHeight = false
						that.startBluetoothDeviceDiscovery()
						that.closeBluetoothAdapter()
					}
				}, 30000);
			},
			/**
			 * 停止搜索蓝牙设备
			 */
			stopBluetoothDevicesDiscovery() {
				uni.stopBluetoothDevicesDiscovery({
					success: e => {
						console.log("停止搜索蓝牙设备", e)
					},
				});
			},
			// 保存测量结果
			handleGetMeasure() {
				let that = this
				if (!that.height) {
					this.$tools.msg(that.$t("tips.verifyHeight"))
					return
				}
				that.$model.getmeasurefunit({
					adc: that.imp,
					weight: that.weight + that.unit,
					height: that.height,
					aud_id: that.info.id
				}).then(res => {
					that.isHeight = false
					console.log("res", res, that.imp)
					if (res.code == 0) {
						that.$store.dispatch('getUserInfo', {
							aud_id: uni.getStorageSync('userid')
						})
						that.$store.dispatch("getResult", {
							aud_id: uni.getStorageSync('userid')
						})
						that.$tools.msg(that.$t("tips.msgSuccess"))
					} else {
						that.$tools.msg(that.$t("tips.msgFail"))
					}
					that.Unload = true
					setTimeout(function() {
						that.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
						let pages = getCurrentPages();
						pages[pages.length - 2].$vm.getBlereload();
						uni.switchTab({
							url: "/pages/home/home"
						})
					}, 200)
				})
			},
			//
			handleBack() {
				let that = this
				that.text = ""
				that.Unload = true
				that.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
				let pages = getCurrentPages();
				console.log("页面返回onUnload", pages, pages[pages.length - 2])
				pages[pages.length - 2].$vm.getBlereload();
				uni.switchTab({
					url: "/pages/home/home"
				})
			},
			/**
			 * 断开蓝牙模块
			 */
			closeBluetoothAdapter() {
				let that = this;
				uni.closeBluetoothAdapter({
					success: res => {
						console.log('蓝牙模块关闭成功');
					}
				})
			},
		},
	}
</script>

<style scoped lang="scss">
	.image3 {
		width: 200px !important;
		height: 340px !important;
	}
</style>