<template>
	<view class="container">
		<view class="text" @click="openBluetoothAdapter" v-if="issearch">没有搜到想要的？点击重新搜索</view>
		<view class="point-area">
			<view class="point point-10"></view>
			<view class="point point-40"></view>
			<view class="point point-80"></view>
			<view class="point point-100"></view>
			<view class="point point-120"></view>
		</view>
		<view class="list">
			<view class="item" v-for="(item,index) in devList" :key="index" @click="handleWeight(item)">
				<image :src="item.pic"></image>
				<text>{{item.name}}</text>
			</view>
		</view>
		<view class="tips" v-if="isdevTip">
			<view>提示：</view>
			<text>1.请确定设备已绑定</text>
			<text>2.请确定设备已开机</text>
			<text>3.请确定手机蓝牙及位置信息已打开</text>
		</view>
	</view>
</template>

<script>
	let that;
	let myTime;
	import {
		mapState
	} from "vuex";
	export default {
		data() {
			return {
				Unload: false, //是否返回上一页
				issearch: false, //是否重新搜索
				isdevTip: false, //是否有提示
				devicesList: [],
				devList: [],
				id: 0
			}
		},
		computed: {
			...mapState(["user", "isConnected", "isBluetoothTyle"]),
		},
		onLoad(options) {
			that = this
			that.id = options ? options.id : 0
			that.openBluetoothAdapter()
			// 监听蓝牙连接状态
			that.$Bluetooth.onBLEConnectionStateChange()
			uni.onBluetoothAdapterStateChange(function(res) {
				that.$store.commit("changeBluetooth", res.available);
			})
		},
		onUnload() {
			let that = this
			console.log("onUnload", that.Unload)
			if (!that.Unload) {
				that.$Bluetooth.closeBluetoothAdapter() // 断开蓝牙模块
				that.$Bluetooth.stopBluetoothDevicesDiscovery() // 取消蓝牙搜索
			}
		},
		watch: {
			isBluetoothTyle: function() {
				let that = this
				if (!that.isBluetoothTyle) {
					that.issearch = true
					that.isdevTip = true
					that.devList = []
					clearTimeout(myTime);
				}
				console.log("蓝牙是否打开", that.isBluetoothTyle)
			}
		},
		methods: {
			// 初始化蓝牙
			openBluetoothAdapter() {
				let that = this
				uni.openBluetoothAdapter({
					success: e => {
						console.log("蓝牙初始化成功")
						that.issearch = false
						that.isdevTip = false
						that.devList = []
						that.startBluetoothDeviceDiscovery()
					},
					fail: e => {
						that.$Bluetooth.getBluetoothAdapter(e)
					}
				});
			},
			// 开始搜寻附近的蓝牙外围设备
			startBluetoothDeviceDiscovery() {
				let that = this
				uni.startBluetoothDevicesDiscovery({
					allowDuplicatesKey: true, //是否允许重复上报同一设备
					success: res => {
						that.onBluetoothDeviceFound();
					},
					fail: res => {}
				});
			},
			/**
			 * 发现外围设备
			 */
			onBluetoothDeviceFound() {
				var that = this;
				uni.onBluetoothDeviceFound(res => {
					res.devices.forEach(device => {
						if (device.name.indexOf("G02") != -1) {
							console.log("G02", device)
							clearTimeout(myTime);
							let buff = device.advertisData.slice(3, 9)
							device.mac = new Uint8Array(buff) // 保存广播数据中的mac地址，这是由于iOS不直接返回mac地址
							let tempMac = Array.from(device.mac)
							tempMac.reverse()
							device.macAddr = that.$tools.ab2hex(tempMac, ':').toUpperCase()
							that.deviceId = device.macAddr
							that.handleDevice(device)
							return;
						}
						if (device.name.indexOf("Yihejia_Lung") != -1) {
							console.log("肺活量PCV02", device)
							clearTimeout(myTime);
							device.macAddr = device.deviceId
							that.deviceId = device.deviceId
							that.handleDevice(device)
							return;
						}
						if (device.name.indexOf("YPC") != -1) {
							console.log("跳绳PCT02", device.name, device)
							clearTimeout(myTime);
							let buff = device.name.slice(7, 19)
							device.macAddr = that.$tools.str2Num(buff)
							device.deviceId = that.$tools.str2Num(buff)
							that.deviceId = that.$tools.str2Num(buff)
							if (device.macAddr != "") {
								that.handleDevice(device)
							}
							return
						}
					})
				});
				that.handleMyTime()
			},
			handleDevice(device) {
				let that = this
				const foundDevices = that.devicesList
				const idx = that.$tools.inArray(foundDevices, "deviceId", device.deviceId)
				console.log("device", device, idx)
				if (idx === -1) {
					that.devicesList.push(device);
					if (device.macAddr != "") {
						that.handleDevType(device.macAddr)
					}
				}
			},
			handleMyTime() {
				var that = this;
				myTime = setTimeout(function() {
					if (!that.devList.length) {
						that.isdevTip = true
						that.devList = []
					}
					that.issearch = true
					clearTimeout(myTime);
					that.$Bluetooth.stopBluetoothDevicesDiscovery()
				}, 15000);
			},
			// 排查设备
			handleDevType(sn) {
				that = this
				that.$model.getdevdetail({
					mac: sn,
					acd_id: that.id
				}).then(res => {
					console.log("排查返回", res, sn, that.id)
					if (res.code == 0) {
						res.data.deviceId = sn
						that.devList.push(res.data);
					}
				})
			},
			handleWeight(item) {
				let that = this
				clearTimeout(myTime);
				that.Unload = true
				console.log("跳转测量", item)
				if (item.bluetooth_type == '透传') {
					that.$Bluetooth.stopBluetoothDevicesDiscovery()
				}
				uni.redirectTo({
					url: item.page_measure + '?deviceId=' + that.deviceId
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.text {
		position: absolute;
		top: 0px;
		width: 100%;
		text-align: center;
		height: 50px;
		line-height: 50px;
		font-size: 16px;
		color: $textcolor;
		font-weight: bold;
	}

	.tips {
		position: absolute;
		width: 100%;
		bottom: 15px;
		line-height: 24px;

		view {
			font-size: 14px;
			color: $textcolor;
			font-weight: bold;
			margin-left: 15px;
		}

		text {
			font-size: 12px;
			width: 100%;
			display: block;
			margin-left: 20px;
			color: #999;
		}
	}

	.list {
		position: absolute;
		width: 100%;
		display: flex;
		align-items: center;
		flex-wrap: wrap;

		.item {
			width: 30%;
			display: flex;
			flex-wrap: wrap;
			align-items: center;
			justify-content: center;
			position: absolute;
			left: 55%;
			top: -10px;

			image {
				width: 45px;
				height: 45px;
				border-radius: 50%;
				background-color: #fff;
			}

			text {
				display: block;
				width: 100%;
				font-size: 12px;
				color: #666;
				margin-top: 5px;
				text-align: center;
			}
		}

		.item:nth-of-type(2) {
			left: calc(55% - 60px);
			top: 50px;
		}

		.item:nth-of-type(3) {
			left: calc(50% - 140px);
			top: 90px;
		}

		.item:nth-of-type(4) {
			left: calc(61% + 20px);
			top: 70px;
		}

		.item:nth-of-type(5) {
			left: 20%;
			top: -100px;
		}

		.item:nth-of-type(6) {
			left: calc(20% - 16px);
			top: -38px;
		}

		.item:nth-of-type(7) {
			left: 0;
			top: 15px;
		}

		.item:nth-of-type(8) {
			left: calc(20% + 75px);
			top: -150px;
		}

		.item:nth-of-type(9) {
			left: calc(20% + 96px);
			top: -80px;
		}

		.item:nth-of-type(10) {
			left: 37px;
			top: -170px;
		}

		.item:nth-of-type(11) {
			left: calc(20% + 75px);
			top: 130px;
		}

		.item:nth-of-type(12) {
			left: -10px;
			top: -104px;
		}

		.item:nth-of-type(13) {
			left: calc(47% + 75px);
			top: -150px;
		}

		.item:nth-of-type(14) {
			left: calc(53% + 75px);
			top: -50px;
		}

	}

	.container {
		width: 100%;
		height: 100vh;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		overflow: hidden;
	}

	// 中心园
	.container::after {
		content: "";
		width: 20px;
		height: 20px;
		border-radius: 50%;
		background-color: #fbb780;
		position: absolute;
		z-index: 9;

	}

	/* 定义范围*/
	.point-area {
		text-align: center;
		position: relative;
		width: 400rpx;
		height: 400rpx;
		transition: opacity 0.5s ease-out;
	}

	.point-10,
	.point-40,
	.point-80,
	.point-100,
	.point-120 {
		width: 100%;
		height: 100%;
	}

	.point-10:after,
	.point-40:after,
	.point-80:after,
	.point-100:after,
	.point-120:after {
		content: '';
		display: block;
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		border-radius: 50%;
		opacity: 0;
		border: 1px solid #f7cb6b;
		animation-play-state: paused;
		-webkit-animation-play-state: paused;
	}

	.point-10:after {
		content: '';
		animation: ripple 3000ms linear 0ms infinite;
	}

	.point-40:after {
		content: '';
		animation: ripple 3000ms linear 600ms infinite;

	}

	.point-80:after {
		content: '';
		animation: ripple 3000ms linear 1200ms infinite;
	}

	.point-100:after {
		content: '';
		animation: ripple 3000ms linear 1800ms infinite;
	}

	.point-120:after {
		content: '';
		animation: ripple 3000ms linear 2400ms infinite;
	}


	@keyframes ripple {

		0% {
			opacity: 0;
			transform: scale(0.1);
		}

		50% {
			opacity: 0.8;
			transform: scale(1);
		}

		100% {
			opacity: 0.2;
			transform: scale(2.2);
		}

	}
</style>