<template>
	<view class="content weightPages">
		<view class="title" v-if="isConnection == 0">连接中，请稍后...</view>
		<view class="title" v-else-if="isConnection == 1">连接成功，请开始测量</view>
		<view class="title" style="color: brown;" v-else-if="isConnection == 2" @click="openBluetoothAdapter">
			连接失败，点击重新连接
		</view>
		<view class="text">{{textW}}</view>
		<view class="text">{{textH}}</view>
		<!-- <div class="result-wrap">
			<view class="result-item">
				<view class="result-title">身高</view>
				<view class="result-value">{{info.height}}cm</view>
			</view>
			<view class="result-item">
				<view class="result-title">体重</view>
				<view class="result-value">{{info.weight}}kg</view>
			</view>
			<view class="result-item">
				<view class="result-title">骨量</view>
				<view class="result-value">{{info.bone}}kg</view>
			</view>
			<view class="result-item">
				<view class="result-title">阻抗</view>
				<view class="result-value">{{info.imp}}Ω</view>
			</view>
			<view class="result-item">
				<view class="result-title">BMI</view>
				<view class="result-value">{{info.bmi}}</view>
			</view>
			<view class="result-item">
				<view class="result-title">内脏脂肪</view>
				<view class="result-value">{{info.visceralFat}}%</view>
			</view>
			<view class="result-item">
				<view class="result-title">脂肪率</view>
				<view class="result-value">{{info.fat_r}}%</view>
			</view>
			<view class="result-item">
				<view class="result-title">水分率</view>
				<view class="result-value">{{info.water}}%</view>
			</view>
			<view class="result-item">
				<view class="result-title">肌肉率</view>
				<view class="result-value">{{info.muscle}}%</view>
			</view>
		</div> -->
		<view class="image">
			<image mode="aspectFit" src="/BLEPages/static/EF06S.gif" class="image3"></image>
		</view>
		<view class="tips">
			<view>提示：</view>
			<view>1.请确定设备是开机状态</view>
			<view>2.请确定手机蓝牙、位置信息已打开</view>
			<view>3.ios系统需打开设置—>应用—>微信里的蓝牙权限</view>
		</view>
		<!-- <div class="info-dlg" v-if="show_info">
			<view class="info-inner">
				<view class="info-item">
					<text>性别：</text>
					<text class="man" :class="{'active':cur_sex==1}" @click="cur_sex=1">男</text>
					<text class="woman" :class="{'active':cur_sex==2}" @click="cur_sex=2">女</text>
				</view>
				<view class="info-item">
					<text>年龄：</text>
					<picker class="age-select" mode="selector" :value="cur_age" :range="ageRange" @change="onAgeChange">
						{{cur_age+1}}岁
					</picker>
				</view>
				<view class="confirm" @click="setUserInfo">确定</view>
			</view>
		</div> -->
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	let cnt = 0
	export default {
		data() {
			return {
				deviceId: "",
				serviceId: "",
				write: "",
				notify: "",
				// cur_sex: 1,
				// cur_age: 27,
				// show_info: false,
				// ageRange: [],
				// ageIndex: 30,
				textW: "",
				textH: "",
				send_err_count: 0,
				BLEResult: {
					bmi: 0,
					weight: 0,
					fat_r: 0, //脂肪率
					water: 0, //水分率
					muscle: 0, //肌肉率,
					bone: 0, //骨量
					imp: 0, //阻抗
					height: 0, //身高
					value: 0,
					bodyage: 0,
					kcal: 0,
					visceral: 0,
					sfr: 0,
					fatlevlval: 0,
					protein: 0,
				},
				isConnection: 0,
				devicesList: [],
			}
		},
		computed: {
			...mapState(["user", "isConnected", "isBluetoothTyle", "appTheme"]),
			info() {
				return this.user
			}
		},
		mounted() {
			let that = this
			// for (let i = 1; i <= 100; i++) {
			// 	that.ageRange.push(i);
			// }

			// let sex = uni.getStorageSync('sex')
			// let age = uni.getStorageSync('age')
			// that.cur_sex = sex ? sex : 1
			// that.cur_age = age ? age : 27
		},
		onLoad(options) {
			let that = this
			cnt = 0
			that.textW = ""
			that.textH = ""
			// 导航栏颜色
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.appTheme,
			})
			if (options && options.deviceId) {
				that.macAddr = options.deviceId
				that.deviceId = options.deviceId
				that.closeBLEConnection()
				that.closeBluetoothAdapter()
				that.openBluetoothAdapter()
			}
			that.onBLEConnectionStateChange()
			uni.onBluetoothAdapterStateChange(function(res) {
				that.$store.commit("changeBluetooth", res.available);
			})
		},
		onUnload: function() {
			let that = this
			if (!that.Unload) {
				that.handleBack()
				uni.switchTab({
					url: "/pages/index/index"
				})
				console.log("返回上一个页面")
			}
		},
		watch: {
			isConnected: function() {
				let that = this
				if (!that.isConnected) {
					that.handleBack()
					that.isConnection = 2
				}
			},
			isBluetoothTyle: function() {
				let that = this
				if (!that.isBluetoothTyle) {
					that.handleBack()
					that.isConnection = 2
				}
			},
		},
		methods: {
			setUserInfo() {
				let that = this
				uni.setStorageSync('sex', that.cur_sex)
				uni.setStorageSync('age', that.cur_age)
				that.show_info = false
				that.sendCommand(that.cur_sex, that.cur_age + 1);
			},
			onAgeChange(e) {
				this.cur_age = parseInt(e.detail.value)
			},
			openBluetoothAdapter() {
				let that = this
				cnt = 0
				uni.openBluetoothAdapter({
					success: e => {
						console.log("初始化设备")
						that.isConnection = 0
						that.startBluetoothDeviceDiscovery()
					},
					fail: e => {
						that.isConnection = 2
						that.$tools.msg("请确定设备是开机状态、手机蓝牙权限已打开！")
					}
				});
			},
			// 开始搜寻附近的蓝牙外围设备
			startBluetoothDeviceDiscovery() {
				let that = this
				uni.startBluetoothDevicesDiscovery({
					allowDuplicatesKey: true,
					success: res => {
						console.log("开始搜索")
						that.isConnection = 0
						that.onBluetoothDeviceFound();
					},
					fail: res => {
						that.isConnection = 2
						that.$tools.msg("请确定设备是开机状态、手机蓝牙权限已打开！")
					}
				});
			},
			/**
			 * 发现外围设备
			 */
			onBluetoothDeviceFound() {
				var that = this;
				uni.onBluetoothDeviceFound(res => {
					res.devices.forEach(device => {
						if (!device.name && !device.localName) {
							return
						}
						if (device.name.indexOf('JS-B68T') != -1 || (device.localName && device.localName
								.toLowerCase().indexOf('JS-B68T') != -1)) {
							const bytes = new Uint8Array(device.advertisData);
							const macBytes = bytes.slice(10, 16);
							device.macAddr = that.$tools.ab2hex(macBytes, ':').toUpperCase()

							if (device.deviceId.indexOf(that.deviceId) != -1 || device.macAddr.indexOf(that
									.deviceId) != -1) {
								that.stopBluetoothDevicesDiscovery()
								that.deviceId = device.deviceId
								that.macAddr = device.macAddr
								that.createBLEConnection()
								return;
							}
						}
					})
				});
			},
			// 连接蓝牙
			createBLEConnection() {
				let that = this;
				uni.createBLEConnection({
					deviceId: that.deviceId,
					success: res => {
						that.isConnection = 0
						that.getBLEDeviceServices()
					},
					fail: res => {
						that.isConnection = 2
						console.log("设备连接失败，请重新连接", res);
					}
				});
			},
			/**
			 * 获取设备的UUID
			 */
			getBLEDeviceServices() {
				let serviceList = [];
				let that = this;
				uni.getBLEDeviceServices({
					deviceId: that.deviceId,
					success: res => {
						console.log("获取设备的UUID成功", res)
						serviceList = res.services;
						for (let i = 0; i < serviceList.length; i++) {
							let service = serviceList[i];
							if (service.uuid.indexOf("00001910") != -1) {
								that.isConnection = 1
								that.serviceId = service.uuid;
								that.getBLEDeviceCharacteristics();
								break;
							}
						}
					},
					fail: res => {
						console.log('获取设备的UUID失败:', res)
					}
				});
			},
			/**
			 * 获取指定服务的特征值
			 */
			getBLEDeviceCharacteristics() {
				let characteristicsList = [];
				let that = this;
				uni.getBLEDeviceCharacteristics({
					deviceId: that.deviceId,
					serviceId: that.serviceId,
					success: res => {
						console.log("服务的特征值成功", res)
						for (let i = 0; i < res.characteristics.length; i++) {
							let item = res.characteristics[i];
							if (item.uuid.indexOf('00002B10') != -1) {
								that.write = item.uuid
							} else if (item.uuid.indexOf('00002B11') != -1) {
								that.notify = item.uuid
							}
						}
						that.sendCommand()
						uni.notifyBLECharacteristicValueChange({
							deviceId: that.deviceId,
							serviceId: that.serviceId,
							characteristicId: that.notify,
							state: true,
							success: () => {
								uni.onBLECharacteristicValueChange(function(res) {
									const raw = new Uint8Array(res.value);
									let value = that.$tools.ab2hex(res.value, "");
									cnt++
									console.log("value", value, cnt)

									if (raw[0] == 0x55 && raw[1] == 0xAA) {
										that.isConnection = 1
										that.send_err_count = 0
										uni.showToast({
											title: '年龄信息发送成功，请开始测量'
										})
										return
									}
									if (raw[0] == 0x55 && raw[1] == 0xBB) {
										if (that.send_err_count > 2) {
											that.isConnection = 2
											uni.showToast({
												title: '年龄信息发送失败，请重新连接'
											})
											return
										}
										that.send_err_count++
										return
									}

									if (raw[0] == 0x5A && cnt > 1) {
										// 体重(高低位组合, 实际值的100倍)
										that.BLEResult.weight = ((raw[2] << 8) | raw[3]) /
											100.0
										if (that.BLEResult.weight < 1) {
											that.BLEResult.weight = ((raw[2] << 8) | raw[
												3])
										}
										// 脂肪率(高低位组合, 实际值的10倍)
										that.BLEResult.fat_r = ((raw[4] << 8) | raw[5]) /
											10.0

										// 水分率(高低位组合, 实际值的10倍)
										that.BLEResult.water = ((raw[6] << 8) | raw[
											7]) / 10.0

										// 肌肉率(高低位组合, 实际值的10倍)
										that.BLEResult.muscle = ((raw[8] << 8) | raw[
											9]) / 10.0

										// 骨量(高低位组合, 实际值的10倍)
										that.BLEResult.bone = ((raw[10] << 8) | raw[
											11]) / 10.0

										// 阻抗(高位和低位组合)
										that.BLEResult.imp = (raw[12] << 8) | raw[13]

										//BMI
										that.BLEResult.bmi = ((raw[14] << 8) | raw[16]) /
											10.0;

										// 身高(高低位组合, 厘米)
										that.BLEResult.height = raw[15]+ "cm"
										that.textW = "您的体重是：" + that.BLEResult.weight +'kg'
										that.textH = "您的身高是：" + raw[15] + "cm"
										console.log("体重", that.BLEResult.weight)
										console.log("身高", raw[15])
										if (raw.length == 18 && raw[17] == 0xA5) {
											that.BLEResult.ecode = that.macAddr
											that.BLEResult.familyid = that.info.familyid
											that.BLEResult.weight  = that.BLEResult.weight +'kg'
											that.handleGetMeasure()
										}
									}
								})
							},
							fail: res => {
								console.log('获取特征值失败:', JSON.stringify(res))
							}
						})
					},
					fail: res => {
						console.log('获取特征值失败:', JSON.stringify(res))
					}
				})
			},
			// 发送指令到设备
			sendCommand() {
				let that = this;
				// 创建11字节Buffer (协议要求长度)
				const buffer = new ArrayBuffer(10);
				const bytes = new Uint8Array(buffer);

				// 填充固定数据
				bytes[0] = 0x02; // 开始字节
				bytes[1] = 0x53; // 备用1

				// 设置性别 (根据参数动态设置)
				bytes[2] = that.info.gender === 2 ? 0x31 : 0x30; // 女0x31, 男0x30

				// 填充4字节固定数据 (0x30)
				bytes[3] = 0x30;
				bytes[4] = 0x30;
				bytes[5] = 0x30;

				// 处理年龄：转换为两位ASCII字符串
				const ageStr = that.info.age.toString().padStart(2, '0');
				bytes[6] = ageStr.charCodeAt(0); // 十位ASCII
				bytes[7] = ageStr.charCodeAt(1); // 个位ASCII

				// 动态计算校验码 (异或0x02到年龄个位)
				let checksum = bytes[0];
				for (let i = 1; i <= 7; i++) {
					checksum ^= bytes[i];
				}
				bytes[8] = checksum;

				// 结束字节
				bytes[9] = 0x03;

				// 发送数据
				uni.writeBLECharacteristicValue({
					deviceId: that.deviceId,
					serviceId: that.serviceId,
					characteristicId: that.write,
					value: buffer,
					success: () => {
						console.log("发送成功")
					},
					fail: (err) => {
						uni.showToast({
							title: '发送失败'
						})
					}
				});
			},
			// 保存测量结果
			handleGetMeasure() {
				let that = this
				console.log("BLEResult", that.BLEResult)
				that.$model.getmeasuredata(that.BLEResult).then(res => {
					if (res.code == 0) {
						that.$tools.msg("测量成功")
						that.$store.dispatch("getUserInfo", {
							familyid: that.info.familyid,
						});
						that.$store.dispatch("getResult", {
							birthday: that.info.birthday,
							familyid: that.info.familyid,
							height: that.height ? that.height : that.info.height,
							sex: that.info.sex,
						});
					} else {
						console.log("测量失败", res.message)
						that.$tools.msg(res.message)
					}
					that.Unload = true
					setTimeout(function() {
						that.closeBLEConnection()
						that.closeBluetoothAdapter()
						uni.switchTab({
							url: "/pages/index/index"
						})
					}, 200)
				})
			},
			onBLEConnectionStateChange() {
				let that = this
				uni.onBLEConnectionStateChange(function(res) {
					console.log("监听蓝牙连接状态", res.connected)
					if (!res.connected) {
						that.Unload = true
						that.isConnection = 2
						that.closeBLEConnection()
						that.closeBluetoothAdapter()
					}
					that.$store.commit("changeConnected", res.connected);
				})
			},
			handleBack(ind) {
				let that = this
				that.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
				that.closeBLEConnection()
				that.closeBluetoothAdapter()
			},
			/**
			 * 停止搜索蓝牙设备
			 */
			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 => {
						console.log('断开蓝牙连接成功');
					}
				});
			},
		}
	}
</script>

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

	.title {
		width: 100%;
		text-align: center;
		font-size: 36rpx;
		font-weight: 700;
		margin: 20rpx 0;
	}

	.result-wrap {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding: 30rpx;
		box-sizing: border-box;

		.result-item {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			width: 48%;
			padding: 20rpx;
			margin-top: 16rpx;
			box-sizing: border-box;
			border-radius: 12rpx;
			background-color: #fff;
			box-shadow: 0 4px 10px rgba(0, 0, 0, 0.08);

			.result-title {
				flex: 150rpx 0 0;
				font-size: 32rpx;
				font-weight: 600;
				color: #4b6cb7;
			}

			.result-value {
				flex: 1;
				font-size: 32rpx;
				font-weight: 700;
				color: #182848;
				margin-top: 5rpx;
				text-align: right;
			}
		}
	}

	.tips {
		font-weight: 700;
		padding: 0 20rpx;
		box-sizing: border-box;
	}

	.image {
		display: flex;
		justify-content: center;
		margin-top: 20rpx;
	}

	.info-dlg {
		display: flex;
		justify-content: center;
		align-items: center;
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		z-index: 99;
		background-color: rgba(0, 0, 0, 0.8);

		.info-inner {
			width: 65%;
			height: 350rpx;
			border-radius: 20rpx;
			background-color: #fff;
			padding: 80rpx 50rpx;
			box-sizing: border-box;

			.info-item {
				display: flex;
				margin-bottom: 30rpx;

				text {
					font-size: 36rpx;
					font-weight: 700;
				}

				.man,
				.woman {
					width: 120rpx;
					height: 50rpx;
					font-size: 28rpx;
					line-height: 50rpx;
					text-align: center;
					border-radius: 8rpx;
					border: 1px solid #e1e1e1;
					margin-right: 10rpx;

					&.active {
						color: #fff;
					}
				}

				.man.active {
					background-color: #74b9ff;
				}

				.woman.active {
					background-color: #fd79a8;
				}

				.age-select {
					width: 210rpx;
					height: 50rpx;
					line-height: 50rpx;
					border-radius: 8rpx;
					padding: 0 20rpx;
					border: 1px solid #e1e1e1;
				}
			}

			.confirm {
				width: 200rpx;
				height: 50rpx;
				color: #fff;
				font-size: 32rpx;
				line-height: 50rpx;
				text-align: center;
				margin: 0 auto;
				background-color: #2ecc71;
			}

			.info-item:last-child {
				margin-top: 50rpx;
			}
		}
	}
</style>