<template>
	<view class="weightPages">
		<view class="content ">
			<view class="title">{{textLink}}</view>
			<view class="text">{{text}}</view>
			<view class="image">
				<image src="/pageTwo/static/PCL.gif" class="image3"></image>
			</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="tips">
				<view>提示：</view>
				<text>1.请确定设备已开机</text>
				<text>2.请确定手机蓝牙及位置信息已打开</text>
			</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", "isConnected", "isBluetoothTyle"]),
			info() {
				return this.user
			},
		},
		onUnload: function() {
			let that = this
			if (!that.Unload) {
				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.deviceId = options.deviceId
			that.height = that.user.height
			that.handleDeviceFound()
		},
		watch: {
			isConnected: function() {
				let that = this
				if (!that.isConnected) {
					let text = '连接中断，请重新连接设备再开始测量'
					that.$tools.showModal(text)
				}
			},
		},
		methods: {
			/**
			 * 发现外围设备
			 */
			handleDeviceFound() {
				var that = this;
				uni.onBluetoothDeviceFound(res => {
					res.devices.forEach(device => {
						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
				console.log("结束", that.weight, that.unit, that.HeartRateList)
				that.$model.getmeasurefunit({
					adc: that.imp,
					weight: that.weight + that.unit,
					heart_rate_data: that.HeartRateList,
					height: that.height,
					aud_id: that.info.id
				}).then(res => {
					if (res.code == 0) {
						that.$store.dispatch("getResult", {
							aud_id: uni.getStorageSync('userid')
						})
						that.$store.dispatch('getUserInfo', {
							aud_id: uni.getStorageSync('userid')
						})
						that.$tools.msg("测量成功")
					} else {
						that.$tools.msg("测量失败")
					}
					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"
				})
			},
			/**
			 * 停止搜索蓝牙设备
			 */
			stopBluetoothDevicesDiscovery() {
				uni.stopBluetoothDevicesDiscovery({
					success: e => {
						console.log("停止搜索蓝牙设备", e)
					},
				});
			},
			/**
			 * 断开蓝牙模块
			 */
			closeBluetoothAdapter() {
				let that = this;
				uni.closeBluetoothAdapter({
					success: res => {
						console.log('蓝牙模块关闭成功');
					}
				})
			},
			/**
			 * 断开蓝牙连接
			 */
			closeBLEConnection() {
				var that = this;
				uni.closeBLEConnection({
					deviceId: that.deviceId,
					success: res => {
						that.$store.commit("changeConnected", false);
					}
				});
			},
		},
	}
</script>

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

	.btnGroup {
		justify-content: space-around;

		.Blue-btn {
			color: #fff;
			padding: 8px 25px;
			background: $maincolor;
			border-radius: 10px;
		}

		.Blue-close {
			color: #000;
			background: #dfdfdf;
		}
	}
</style>