<template>
	<view class="pages">
		<u-navbar title="设备详情" :border-bottom="false" :background="bgc" back-icon-color="#3D3D3D" title-color='#3D3D3D'
			title-size='36' height='44'></u-navbar>
		<view class="list_item">
			<view class="top">
				<view class="bh">设备编号：{{obj.deviceNo == undefined ? '--' : obj.deviceNo}}</view>
				<view class="zx" v-if="obj.onlineStatus == 1">
					<view class="yuan"></view>
					<view class="wz">在线</view>
				</view>
				<view class="zx" v-else>
					<view class="yuan" style="background-color: red;"></view>
					<view style="color: red;">离线</view>
				</view>
			</view>
			<view class="devixexq">
				<view class="xiaoav">
					<view class="">设备状态</view>
					<view style="color: rgb(18, 210, 172);;" v-if="obj.status == 2">使用中</view>
					<view style="color: rgb(18, 210, 172);;" v-if="obj.status == 1">正常</view>
					<view style="color: red;" v-if="obj.status == 3">不可用</view>
				</view>
				<view class="xiaoav">
					<view class="">所属合作伙伴</view>
					<view class="">{{obj.agentName == undefined ? '--' : obj.agentName}}</view>
				</view>
				<view class="xiaoav">
					<view class="">所属渠道商</view>
					<view class="">{{obj.bizManName == undefined ? '--' : obj.bizManName}}</view>
				</view>
				<view class="xiaoav">
					<view class="">所属创业者</view>
					<view class="">{{obj.userName == undefined ? '--' : obj.userName}}</view>
				</view>
			</view>
			<view class="zong">
				<view class="xiaoav">
					<view class="">订单总数</view>
					<view class="">{{obj.orderCount == undefined ? '--' : obj.orderCount}}</view>
				</view>
				<view class="xiaoav">
					<view class="">订单总金额</view>
					<view class="">￥{{obj.orderAmount == undefined ? '--' : obj.orderAmount}}</view>
				</view>
			</view>
		</view>
		<!-- 开关操作 -->
		<view class="lishi" v-if="type">
			<view class="lt">
				开关操作
			</view>
			<view class="rt">
				<image v-if="imgflag" @click="btnimg"
					src="https://api.ccttiot.com/smartmeter/img/static/uccaWar4FDp37op7haWM " mode=""></image>
				<image v-else @click="btnimgs" src="https://api.ccttiot.com/smartmeter/img/static/ur9HRtZKY7YcxGewJARz"
					mode=""></image>
			</view>
		</view>
		
		<!-- 蓝牙连接 -->
		<view class="bluetoothbox" v-if="bluetoothflag">
			<view class="stop">
				设备已离线，正在连接该设备蓝牙
			</view>
			<view class="xtop">
				请将手机与设备尽量靠近，连接成功后可操作
			</view>
			<image src="https://api.ccttiot.com/smartmeter/img/static/ucDphMKsozqwwllJTAKL" mode=""></image>
		</view>
		
		<!-- 蓝牙连接失败 -->
		<view class="bluetoothbox" v-if="shibaiflag">
			<image class="img" src="https://api.ccttiot.com/smartmeter/img/static/uTb3vSlbcHdPMFQMBjyL" mode=""></image>
			<view class="one">
				蓝牙连接失败
			</view>
			<view class="two">
				请确保手机足够靠近设备
			</view>
			<view class="thr">
				并且设备处于上电状态
			</view>
			<view class="anfour">
				<view class="qx" @click="btnlyqx">
					取消
				</view>
				<view class="cx" @click="topage">
					重新连接
				</view>
			</view>
		</view>
		<view v-if="btnmsk"
			style="width: 100%;height: 100vh;position: fixed;top: 0;left: 0;background-color: #000;opacity: .1;z-index: 999;">
		</view>
		
		<!-- 商家信息 -->
		<view class="sjmsg">
			<view class="tit">
				商家信息
			</view>
			<view class="jy">
				<view class="one">经营场所：</view>
				<view class="thr">{{obj.storeName == undefined ? '--' : obj.storeName}}</view>
			</view>
			<view class="jy">
				<view class="one" style="width: 180rpx;">经营地址：</view>
				<view class="two">{{obj.storeAddress == undefined ? '--' : obj.storeAddress}}</view>
			</view>
			<view class="jy">
				<view class="one">所在房间：</view>
				<view class="two">{{obj.room == undefined ? '--' : obj.room}}号</view>
			</view>
		</view>
		<!-- 设备规格 -->
		<view class="sjmsg">
			<view class="tit">
				设备规格
			</view>
			<view class="jy">
				<view class="one">设备名称：</view>
				<view class="two">{{obj.deviceName == undefined ? '--' : obj.deviceName}}</view>
			</view>
			<view class="jy">
				<view class="one">设备尺寸：</view>
				<view class="two">{{obj.size == undefined ? '--' : obj.size}}</view>
			</view>
			<view class="jy">
				<view class="one">设备面料：</view>
				<view class="two">{{obj.fabric == undefined ? '--' : obj.fabric}}</view>
			</view>
			<view class="jy">
				<view class="one">设备填充物：</view>
				<view class="two">{{obj.fill == undefined ? '--' : obj.fill}}</view>
			</view>
		</view>
		<!-- 在线信息 -->
		<view class="sjmsg">
			<view class="tit">
				在线信息
			</view>
			<view class="jy">
				<view class="one">首次上线时间：</view>
				<view class="two">{{obj.firstOnlineTime == undefined ? '--' : obj.firstOnlineTime}}</view>
			</view>
			<view class="jy">
				<view class="one">最后登录时间：</view>
				<view class="two">{{obj.lastOnlineTime == undefined ? '--' : obj.lastOnlineTime}}</view>
			</view>
		</view>
		<!-- 收益明细 -->
		<view class="sjmsg">
			<view class="tit">
				收益明细
			</view>
			<view class="jy" v-for="(item,index) in obj.bonusList" :key="index">
				<view class="one">{{item.arrivalName == undefined ? '--' : item.arrivalName}}
				（<text v-if="item.arrivalType == 1">平台</text>
				<text v-if="item.arrivalType == 2">公司</text>
				<text v-if="item.arrivalType == 3">合作伙伴</text>
				<text v-if="item.arrivalType == 4">渠道商</text>
				<text v-if="item.arrivalType == 5">创业者</text>
				<text v-if="item.arrivalType == 6">经营场所</text>）{{item.point}}%</view> 
			</view>
		</view>
	</view>
</template>

<script>
	var xBlufi = require("@/components/blufi/xBlufi.js")
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "#F7FAFE",
				},
				id:'',
				obj:{},
				setMode: null,
				qrResult: '',
				isUsing: false,
				bluetoothflag: false,
				shibaiflag: false,
				btnmsk: false,
				deviceId:'',
				name: '',
				imgflag:false,
				pictrim:true,
				type:''
			}
		},
		onLoad(option) {
			this.$u.get('/app/user/userInfo').then(res =>{
				this.type = res.data.type
			})
            this.id = option.deviceId
			this.getxq()
		},
		// 分享到好友（会话）
		onShareAppMessage: function() {
			return {
				title: '创亿康',
				path: '/pages/index/index'
			}
		},

		// 分享到朋友圈  
		onShareTimeline: function() {
			return {
				title: '创亿康',
				query: '',
				path: '/pages/index/index'
			}
		},
		methods: {
			
             getxq(){
             	this.$u.get(`/app/device/${this.id}`).then(res =>{
             		if(res.code == 200){
             			this.obj = res.data
						this.imgflag = res.data.isUsing
						this.qrResult = res.data.mac
             		}
             	})
             },
			 
			 // 点击临时开关
			 btnimg() {
			 	if(this.pictrim == true){  //防止频繁点击
			 		this.pictrim = false
			 		setTimeout(()=>{
			 			this.pictrim = true
			 		},5000)
			 		if (this.obj.onlineStatus == 1) {
			 			this.$u.put(`/app/device/${this.id}/changePower?status=0`).then(res => {
			 				if (res.code == 200) {
			 					this.imgflag = false
			 				} else {
			 					uni.showToast({
			 						title: res.msg,
			 						icon: 'none',
			 						duration: 1000
			 					})
			 				}
			 			})
			 		} else {
			 			if (this.setMode == null) {
			 				this.btnmsk = false
			 				this.shibaiflag = false
			 				this.bluetoothflag = true
			 				xBlufi.initXBlufi(1)
			 				xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent)
			 				xBlufi.notifyStartDiscoverBle({
			 					'isStart': true
			 				})
			 				// 停止蓝牙搜索  
			 				setTimeout(() => {
			 					xBlufi.notifyStartDiscoverBle({
			 						'isStart': false
			 					})
			 					xBlufi.notifyConnectBle({
			 						isStart: true,
			 						deviceId: this.deviceId,
			 						name: this.name
			 					})
			 					xBlufi.notifyInitBleEsp32({
			 						deviceId: this.deviceId
			 					})
			 				}, 1000)
			 			} else { 
			 				let that = this
			 				uni.getNetworkType({
			 					success(res) {
			 						if (res.networkType !== 'none') {
			 							uni.getConnectedBluetoothDevices({
			 								success(res) {
			 									setTimeout(() => {
			 										xBlufi.notifySendCustomData({
			 											customData: '11close'
			 										})
			 										uni.showToast({
			 											title: '操作成功',
			 											icon: 'success',
			 											duration: 1000
			 										})
			 										this.imgflag = false
			 										that.btnmsk = false
			 										that.shibaiflag = false
			 										that.bluetoothflag = false
			 									}, 1000)
			 								},
			 								fail(err) {
			 									console.error('获取已连接蓝牙设备信息失败：', err)
			 								}
			 							})
			 						}
			 					}
			 				})
			 			}
			 		}
			 	}else{
			 		uni.showToast({
			 			title: '请不要频繁点击',
			 			icon: 'none',
			 			duration: 1000
			 		})
			 	}
			 },
			 btnimgs() {
			 	if(this.pictrim == true){ //防止频繁点击
			 		this.pictrim = false
			 		setTimeout(()=>{  
			 			this.pictrim = true
			 		},5000)
			 		if (this.obj.onlineStatus == 1) {
					///app/bill/switchDevice?billId=${this.id}&open=true
			 			this.$u.put(`/app/device/${this.id}/changePower?status=1`).then(res => {
			 				if (res.code == 200) {
			 					this.imgflag = true
			 				} else {
			 					uni.showToast({
			 						title: res.msg,
			 						icon: 'none',
			 						duration: 1000
			 					})
			 				}
			 			})
			 		} else {
			 			if (this.setMode == null) {
			 				this.btnmsk = false
			 				this.shibaiflag = false
			 				this.bluetoothflag = true
			 				xBlufi.initXBlufi(1)
			 				xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent)
			 				xBlufi.notifyStartDiscoverBle({
			 					'isStart': true
			 				})
			 				// 停止蓝牙搜索  
			 				setTimeout(() => {
			 					xBlufi.notifyStartDiscoverBle({
			 						'isStart': false
			 					})
			 					xBlufi.notifyConnectBle({
			 						isStart: true,
			 						deviceId: this.deviceId,
			 						name: this.name
			 					})
			 					xBlufi.notifyInitBleEsp32({
			 						deviceId: this.deviceId
			 					})
			 				}, 1000)
			 			} else {
			 				let that = this
			 				uni.getNetworkType({
			 					success(res) {
			 						if (res.networkType !== 'none') {
			 							uni.getConnectedBluetoothDevices({
			 								success(res) {
			 									setTimeout(() => {
			 										xBlufi.notifySendCustomData({
			 											customData: '11open'
			 										})
			 										uni.showToast({
			 											title: '操作成功',
			 											icon: 'success',
			 											duration: 1000
			 										})
			 										this.imgflag = true
			 										that.btnmsk = false
			 										that.shibaiflag = false
			 										that.bluetoothflag = false
			 									}, 1000)
			 								},
			 								fail(err) {
			 									console.error('获取已连接蓝牙设备信息失败：', err)
			 								}
			 							})
			 						}
			 					}
			 				})
			 			}
			 		}
			 	}else{
			 		uni.showToast({
			 			title: '请不要频繁点击',
			 			icon: 'none',
			 			duration: 1000
			 		})
			 	}
			 },
			 // 蓝牙连接取消
			 btnlyqx() {
			 	this.shibaiflag = false
			 	this.bluetoothflag = false
			 },
			 // 蓝牙重新连接
			 topage() {
			 	this.btnmsk = false
			 	this.shibaiflag = false
			 	this.bluetoothflag = true
			 	if (this.setMode == null) {
			 		xBlufi.initXBlufi(1)
			 		xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent)
			 		xBlufi.notifyStartDiscoverBle({
			 			'isStart': true
			 		})
			 		// 停止蓝牙搜索  
			 		setTimeout(() => {
			 			xBlufi.notifyStartDiscoverBle({
			 				'isStart': false
			 			})
			 			xBlufi.notifyConnectBle({
			 				isStart: true,
			 				deviceId: this.deviceId,
			 				name: this.name
			 			})
			 			xBlufi.notifyInitBleEsp32({
			 				deviceId: this.deviceId
			 			})
			 		}, 1000)
			 	}
			 },
			 
			 // 搜索蓝牙部分
			 funListenDeviceMsgEvent: function(options) {
			 	switch (options.type) {
			 		case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_MY_DATA:
			 			let loadPercent = options.data;
			 			let loadText = '文件读取中'
			 			// console.log("文件读取中", options.data)
			 			break;
			 		case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
			 			let ver_data = this.parseCustomData(options.data)
			 			this.setMode = Math.floor(ver_data.setMode / 60)
			 			console.log("1收到设备发来的自定义数据结果：", ver_data, this.setMode)
			 			break;
			 		case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
			 			if (options.result) {
			 				let devicesarr = options.data
			 				devicesarr.forEach(device => {
			 					const mac = device.name.substring(5)
			 					if (device.name.slice(5, 17) == this.qrResult) {
			 						this.deviceId = device.deviceId
			 						this.name = device.name
			 						this.mac = device.name.slice(5, 17)
			 					}
			 				})
			 			}
			 			break;
			 		case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
			 			// console.log("连接回调：" + JSON.stringify(options))
			 			if (options.result) {
			 				setTimeout(() => {
			 					this.bluetoothflag = false
			 				}, 3000)
			 				uni.hideLoading(); {
			 					console.log("连接回调options.data.deviceId：" + options.data.deviceId,
			 						"连接回调options.data.name：" + options.data.name)
			 				}
			 			} else {
			 				this.shibaiflag = true
			 				this.bluetoothflag = false
			 				uni.hideLoading()
			 			}
			 			break;
			 		case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
			 			if (!options.result) {
			 				setTimeout(() => {
			 					uni.hideLoading()
			 					this.bluetoothflag = false
			 				}, 3000)
			 				console.log("蓝牙未开启", options)
			 				this.shibaiflag = true
			 				return
			 			}
			 			break;
			 		case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
			 			if (options.result) {
			 				let uniqueDevicesList = Array.from(new Set(this.devicesList))
			 				// 将去重后的数组重新赋值给 this.devicesList
			 				this.devicesList = uniqueDevicesList
			 				let list = []
			 				setTimeout(() => {
			 					this.devicesList = list
			 				}, 200)
			 				console.log('蓝牙停止搜索ok')
			 			} else {
			 				//蓝牙停止搜索失败
			 				console.log('蓝牙停止搜索失败')
			 			}
			 			this.searching = false
			 			break
			 	}
			 },
			 parseCustomData(data) {
			 	const dataArray = data.split('@');
			 	const parsedData = {};
			 	const prefixMap = {
			 		'V': 'voltage',
			 		'S': 'switchState',
			 		'A': 'current',
			 		'P': 'power',
			 		'M': 'remainingPower',
			 		'T': 'setMode',
			 		'W': 'temperature'
			 	};
			 	// 遍历数组并解析每个字段  
			 	for (let i = 0; i < dataArray.length; i++) {
			 		const field = dataArray[i];
			 		for (const prefix in prefixMap) {
			 			if (field.startsWith(prefix)) {
			 				const value = field.substring(1);
			 				const propertyName = prefixMap[prefix];
			 				parsedData[propertyName] = isNaN(parseFloat(value)) ? value : parseFloat(value);
			 				break;
			 			}
			 		}
			 	}
			 	return parsedData;
			 },

		}
	}
</script>

<style lang="scss" scoped>
	/deep/ .u-title {
		padding-bottom: 15rpx;
	}

	/deep/ .u-icon__icon {
		padding-bottom: 15rpx;
	}
	.bluetoothbox {
		width: 100%;
		height: 100vh;
		background-color: #f4f1f6;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 99;
		text-align: center;
	
		.img {
			margin-top: 400rpx;
			width: 320rpx;
			height: 320rpx;
		}
	
		.one {
			font-size: 36rpx;
			color: #000;
		}
	
		.two {
			margin-top: 30rpx;
			font-size: 24rpx;
			color: #ccc;
		}
	
		.thr {
			margin-top: 30rpx;
			font-size: 28rpx;
			color: #ccc;
		}
	
		.anfour {
			display: flex;
			margin-top: 30rpx;
			justify-content: space-between;
			padding: 0 220rpx;
			box-sizing: border-box;
	
			.qx {
				width: 100rpx;
				height: 70rpx;
				line-height: 70rpx;
				text-align: center;
				border: 1px solid #3D3D3D;
				border-radius: 10rpx;
			}
	
			.cx {
				width: 200rpx;
				height: 70rpx;
				line-height: 70rpx;
				text-align: center;
				background-color: #8883F0;
				color: #fff;
				border-radius: 10rpx;
			}
		}
	
		.stop {
			font-size: 36rpx;
			color: #000;
			margin-top: 380rpx;
		}
	
		.xtop {
			margin-top: 30rpx;
			font-size: 24rpx;
			color: #ccc;
		}
	
		.xqx {
			width: 530rpx;
			height: 90rpx;
			line-height: 90rpx;
			border-radius: 20rpx;
			text-align: center;
			background-color: #fff;
			margin: auto;
			margin-top: 200rpx;
		}
	
		image {
			width: 500rpx;
			height: 500rpx;
			margin-top: 100rpx;
		}
	}
    .lishi {
    	width: 100%;
    	height: 110rpx;
    	background-color: #fff;
    	padding: 24rpx;
    	box-sizing: border-box;
    	margin-top: 30rpx;
    	border-radius: 20rpx;
    	display: flex;
    	justify-content: space-between;
    	align-items: center;
    
    	.lt {
    		font-size: 32rpx;
    	}
    
    	.rt {
    		image {
    			width: 160rpx;
    			height: 80rpx;
    		}
    	}
    }
	.pages {
		background-color: #F7FAFE !important;
		height: 100%;
		width: 100%;
		padding-bottom: 100rpx !important;
		padding: 0 38rpx;
		box-sizing: border-box;

		.sjmsg {
			margin-top: 50rpx;

			.tit {
				font-size: 28rpx;
				color: #3D3D3D;
				font-weight: 600;
			}

			.jy {
				margin-top: 24rpx;
				display: flex;
				justify-content: space-between;

				.one {
					font-size: 28rpx;
					color: #808080;
				}

				.two {
					font-size: 28rpx;
					color: #3D3D3D;
				}

				.thr {
					font-size: 28rpx;
					color: #8883F0;
				}
			}
		}

		.list_item {
			margin-top: 32rpx;
			max-height: 348rpx;
			padding: 24rpx 28rpx;
			box-sizing: border-box;
			background-color: #fff;
			background: #FFFFFF;
			box-shadow: 0rpx 4rpx 22rpx 0rpx rgba(0, 0, 0, 0.07);
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			.devixexq {
				display: flex;
				justify-content: space-between;
				margin-top: 14rpx;
				text-align: center;

				.xiaoav {
					font-size: 24rpx;
					color: #3D3D3D;

					view {
						margin-top: 12rpx;
					}
				}
			}

			.zong {
				margin-top: 30rpx;
				display: flex;
				justify-content: space-between;
				text-align: center;
				padding: 0 50rpx;
				box-sizing: border-box;
				font-size: 24rpx;
				color: #3D3D3D;

				view {
					margin-top: 12rpx;
				}
			}

			.cen {
				display: flex;
				justify-content: space-between;
				margin-top: 22rpx;
				align-items: center;
				border-bottom: 1px solid #D8D8D8;
				padding-bottom: 34rpx;
				box-sizing: border-box;

				.fj {
					font-size: 24rpx;
					color: #3D3D3D;
				}

				input {
					width: 346rpx;
					height: 38rpx;
					background: #F0F0F0;
					border-radius: 8rpx 8rpx 8rpx 8rpx;
				}

				.btn {
					width: 110rpx;
					height: 38rpx;
					background: #8883F0;
					border-radius: 8rpx 8rpx 8rpx 8rpx;
					text-align: center;
					line-height: 38rpx;
					font-size: 20rpx;
					color: #FFFFFF;
				}
			}

			.top {
				display: flex;
				justify-content: space-between;
				border-bottom: 1px solid #D8D8D8;
				padding-bottom: 26rpx;
				box-sizing: border-box;

				.bh {
					font-size: 24rpx;
					color: #3D3D3D;
				}

				.zx {
					display: flex;
					align-items: center;

					.yuan {
						width: 14rpx;
						height: 14rpx;
						background: #12D2AC;
						border-radius: 50%;
						margin-right: 10rpx;
					}

					.wz {
						font-size: 24rpx;
						color: #12D2AC;
					}
				}
			}
		}
	}
</style>