<template>
	<view class="weightPages">
		<view class="content ">
			<view class="title">连接成功，开始测量</view>
			<view class="text">{{text}}</view>
			<view class="image">
				<image src="/pageTwo/static/PCL.gif" class="image3"></image>
			</view>
			<view class="tips">
				<view>提示：</view>
				<text>1.请确定设备已开机</text>
				<text>2.请确定手机蓝牙及位置信息已打开</text>
			</view>
		</view>
		<view class="btnGroup" v-if="!stopblue">
			<view class="Blue-btn Blue-close" @click="handleBack">返回上一页</view>
			<view class="Blue-btn" @click="handleGetMeasure">结束测量</view>
		</view>

		<!-- 手动记录 -->
		<!-- <view class="wrapper" v-if="isHeight">
			<view class="bg"></view>
			<view class="Blue">
				<view class="h4">测量结果提示</view>
				<view class="Blue-box">
					本次测量体重为：<text>{{weight}}{{unit}}</text>
				</view>
				<view class="Blue-box">
					上次测量身高为：<input v-model="height" type="digit" placeholder="请输入身高" />cm
				</view>
				<view class="Blue-btn Blue-close" @click="handleBack">取消</view>
				<view class="Blue-btn" @click="handleGetMeasure">保存测量结果</view>
			</view>
		</view> -->
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	export default {
		data() {
			return {
				text: "",
				imp: "",
				weight: "",
				height: "",
				deviceId: "",
				unit: "kg",
				Unload: false,
				isHeight: false,
				stopblue: true,
				HeartRateList: []
			}
		},
		computed: {
			...mapState(["user", "isUseBluetooth"]),
			info() {
				return this.user
			},
		},
		onUnload: function() {
			let that = this
			if (!that.Unload) {
				that.useBluetooth.stopSearchBluetooth()
			}
		},
		onLoad(options) {
			let that = this
			that.text = ""
			that.deviceId = options.deviceId
			that.height = that.user.height
			uni.onBluetoothDeviceFound(handleDeviceFound)
		},
		watch: {
			isUseBluetooth: function() {
				let that = this
				if (!that.isUseBluetooth) {
					that.$tools.showModal('蓝牙连接已断开，请重新连接后测量')
				}
			},
		},
		methods: {
			/**
			 * 发现外围设备
			 */
			handleDeviceFound(res) {
				var that = this;
				res.devices.forEach(device => {
					device.advertisData = device.advertisData ? device.advertisData : ''
					if (device.name.toLowerCase().indexOf('pc-x01') != -1 && device.deviceId.indexOf(
							that.deviceId) != -1) {
						let value = that.$tools.ab2hex(device.advertisData, "")
						let data = parseInt(value.substring(2, 6), 16) / 100
						let HeartRate = parseInt(value.substring(10, 12), 16)
						let type = value.substring(0, 2) //d0实时,e0稳定
						console.log("1111111111", value, data, HeartRate)
						that.text = "您的实时体重是：" + data + that.unit
						if (type == "e0") {
							that.text = "您的稳定体重是：" + data + that.unit
							that.imp = parseInt(value.substring(6, 10), 16)
							that.weight = data
							if (HeartRate != 0) {
								that.HeartRateList.push(HeartRate)
							}
							that.stopblue = false
							return
						}
						return;
					}
				})
			},
			// 保存测量结果
			handleGetMeasure() {
				let that = this
				that.$tools.msg("测量结束")
				that.handleBack()
				console.log("结束", that.weight, that.HeartRateList)
				return

				if (!that.height) {
					this.$tools.msg("请输入身高")
					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("getResult", {
							aud_id: uni.getStorageSync('userid')
						})
						that.$tools.msg("测量成功")
					} else {
						that.$tools.msg("测量失败")
					}
					setTimeout(function() {
						uni.switchTab({
							url: "/pages/body/body"
						})
					}, 200)
				})
			},
			//返回上一页
			handleBack() {
				let that = this
				that.text = ""
				that.$store.commit("getIsUseBluetooth", {
					isBleLink: true,
					BleBodyMsg: "开始测量",
					isUseConnect: false,
				});
				that.useBluetooth.stopSearchBluetooth() //取消蓝牙搜索
				uni.switchTab({
					url: "/pages/body/body"
				})
			},
		},
	}
</script>

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