<template>
	<view class="page">
		<u-navbar title="设备控制" :border-bottom="false" :background="bgc" back-icon-color="#262B37" title-color='#262B37'
			title-size='36' height='50' id="navbar">
		</u-navbar>
        <view class="damen">
        	<view class="menbj" v-for="(item,index) in roomList" :key="index">
        		<view class="top">
        			<view class="lt">
        				<text style="font-weight: 600;">{{item.name}}</text> <text v-if="item.device.onlineStatus == 0" class="lixian" style="color: red;">设备离线</text>
        			</view>
					<view class="rt"  v-if="!item.device.sn">
						<text></text> 未绑定
					</view>
        		<!-- 	<view class="rt"  v-if="item.device.powerStatus == 0">
        				<text></text> 已关闭
        			</view>
        			<view class="rt" style="color: #48893B;"  v-if="item.device.powerStatus == 1">
        				<text style="background-color: #48893B;"></text> 已开启
        			</view> -->
        		</view>
				<view class="bot" v-if="item.device.sn && item.type == 1">
					<view class="lt">
						SN：{{item.device.sn}}
					</view>
					<view class="rt">
						<image src="https://api.ccttiot.com/smartmeter/img/static/u4Btme5ARjeAyvvTjgBF" @click.stop="btnroomkg(1,index,item)" mode=""></image>
					</view>
				</view>
        		<view class="bot" v-if="item.device.sn && item.type != 1">
        			<view class="lt">
        				SN：{{item.device.sn}}
        			</view>
        			<view class="rt">
        				<image src="https://api.ccttiot.com/smartmeter/img/static/u4Btme5ARjeAyvvTjgBF" @click.stop="btnroomkg(1,index,item)" mode="" v-if="item.device.powerStatus == 0"></image>
        				<image src="https://api.ccttiot.com/smartmeter/img/static/ua6R9hsgQZTfahcpaA8G" @click.stop="btnroomkg(0,index,item)" mode=""  v-if="item.device.powerStatus == 1"></image>
        			</view>
        		</view>
        	</view>
        </view>
		<!-- 离线蓝牙控制 -->
		<view class="lxlanya" v-if="lanyaflag">
			<view class="lxtit">
				蓝牙控制开关
			</view>
			<view class="cha" @click.stop="btncha">
				×
			</view>
			<view class="lxzt">
			    <view v-if="ver_dataflag == 1" style="color: red;">
			    	<image src="https://api.ccttiot.com/smartmeter/img/static/u6akzRBuxxP94XlUcMtV" mode=""></image>
			    	<text>未连接</text>
			    </view>
			    <view v-if="ver_dataflag == 2" style="color: #ccc;">
			    	<image src="https://api.ccttiot.com/smartmeter/img/static/u6akzRBuxxP94XlUcMtV" mode=""></image>
			    	<text>连接中</text>
			    </view>
			    <view v-if="ver_dataflag == 3" style="color: #48893B;">
			    	<image src="https://api.ccttiot.com/smartmeter/img/static/uU0nfPcXVEhbEsvECEQg" mode=""></image>
			    	<text>已连接</text>
			    </view>
			</view>
			<view class="kongzhi">
		        <view class="lxkai" v-if="lanyaobj.type != 1" @click.stop="btngb">
		        	关闭
		        </view>
		        <view class="lxkai" @click.stop="btnkq">
		        	开启
		        </view>				
			</view>
		</view>
		<view class="mask" v-if="lanyaflag"></view>
			<!-- <view class="anniu">
				<view class="xinjian" @click="btnkai">
					全开
				</view>
				<view class="xinjian" @click="btnguan">
					全关
				</view>
			</view> -->

	</view>
</template>

<script>
	var xBlufi = require("@/components/blufi/xBlufi.js"); 
	export default {
		data() {
			return {
                bgc: {
                	backgroundColor: "#fff",
                },
				roomId:'',
				flag:false,
				fmflag:false,
				roomtxt:'',
				pricegzflag:false,
				roomobj:{},
				storeId:'',
				roomList: [],
				name:'',
				leixing:'请选择类型',
				type:'',
				show:false,
				txtname:'',
				ver_dataflag:2,
				devicesarr:[],
				deviceIds:'',
				name:'',
				deviceId:'',
				lanyaflag:false,
				lanyamac:'',
				lanyaobj:{}
			}
		},
		onLoad(option) {
            this.roomId = option.roomId
			this.storeId = option.storeId
		},
		onShow() {
			this.getfjxq()
			xBlufi.notifyStartDiscoverBle({'isStart': true})
			xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent)
		},
		onUnload() {
			
		},
		methods: {
			// 蓝牙开启
			btnkq(){
				if(this.lanyaobj.type == 1){
					if(this.lanyaobj.unlockMode == 1){
						xBlufi.notifySendCustomData({
							customData: '11km' + this.lanyaobj.unlockTime + '@'
						})
						let data = {
							mac:this.lanyamac,
							reason:'正常开门',
							command:'11km' + this.lanyaobj.unlockTime + '@',
							result:true
						}
						this.$u.post(`/app/commandLog/bluetooth`,data).then(res => {})
					}else{
						xBlufi.notifySendCustomData({
						    	customData: '11gm' + this.lanyaobj.unlockTime + '@'
						    })
						    let data = {
						    	mac:this.lanyamac,
						    	reason:'反向开门',
						    	command:'11gm' + this.lanyaobj.unlockTime + '@',
						    	result:true
						    }
						    this.$u.post(`/app/commandLog/bluetooth`,data).then(res => {})
					}
				}else{
					xBlufi.notifySendCustomData({
						customData: '11open'
					})
					let data = {
						mac:this.lanyamac,
						reason:'开启',
						command:'11open',
						result:true
					}
					this.$u.post(`/app/commandLog/bluetooth`,data).then(res => {})
				}
			},
			// 蓝牙关闭
			btngb(){
				xBlufi.notifySendCustomData({
					customData: '11close'
				})
				let data = {
					mac:this.lanyamac,
					reason:'关闭',
					command:'11close',
					result:true
				}
				this.$u.post(`/app/commandLog/bluetooth`,data).then(res => {})
			},
			// 点击蓝牙弹出隐藏
			btncha(){
				this.lanyaflag = false
				this.ver_dataflag = 2
				xBlufi.notifyConnectBle({
				    isStart: false,
				    deviceId: this.deviceId,
				    name: this.name
				})
			},
			// 连接蓝牙函数
			getlanya() {
				if (this.ver_dataflag != 3) {
					this.ver_dataflag = 2
					let that = this
					if (that.deviceId != '') {
						let systemInfo = uni.getSystemInfoSync()
							if (systemInfo.platform == 'android') {
								that.lanyaflag = true
								that.deviceId = that.deviceId.match(/.{2}/g).reverse().join(':')
								xBlufi.notifyStartDiscoverBle({
									'isStart': false 
								})
								console.log("进入蓝牙连接", that.deviceId)
								console.log("进入that.name", that.name)
								xBlufi.notifyConnectBle({
									isStart: true,
									deviceId: that.deviceId,
									name: that.name
								})
							} else if (systemInfo.platform == 'ios') {
								// 当前设备是 iOS 
								that.lanyaflag = true
							    that.findDevice()
						}else{
							console.log('其他');
						}
					}
				}
			},
			findDevice() {
			    const matchedDevice = this.devicesarr.find(device => {
			        console.log(device.name, '111') 
			        console.log("IOSMAC", device)
			        return device.name.slice(-12) == this.deviceId.slice(-12)
			    })
			    if (matchedDevice) {
			        xBlufi.notifyStartDiscoverBle({
			            'isStart': false
			        })
			        xBlufi.notifyConnectBle({
			            isStart: true,
			            deviceId: matchedDevice.deviceId,
			            name: matchedDevice.name
			        })
			        this.deviceId = matchedDevice.deviceId
			        this.name = matchedDevice.name
			        console.log(this.deviceId, '11221')
			    } else {
			        console.log("未找到匹配的设备，继续查找...")
			        setTimeout(this.findDevice.bind(this), 1000) // 使用 bind 保持 this 上下文
			    }
			},
			
			// 获取附近蓝牙设备列表
			funListenDeviceMsgEvent: function (options) {
				switch (options.type) {
					case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
						if (options.result) {
							this.devicesarr = options.data
						}
						break;
					case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
						console.log("连接回调：" + JSON.stringify(options))
						if (options.result == true) { 
							    setTimeout(()=>{
							    	this.ver_dataflag = 3
							    },2000)
								xBlufi.notifyInitBleEsp32({
									deviceId: this.deviceId
								})
								this.deviceIds = options.data.deviceId
								this.name = this.name
						} 
						break;
					case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
						this.ver_dataflag = 3
						console.log("1收到设备发来的自定义数据结果：", options.data)
						break;
					case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
						if (!options.result) {
							console.log('蓝牙未开启')
							return
						}
						break;
				}
			},
			// 设施全关
			btnguan(){
				const allDeviceIdsAreNull = this.roomList.every(item => item.deviceId == null)
				if (allDeviceIdsAreNull) {
				  uni.showToast({
				    title: '该房间设施暂无绑定设备',
				    icon: 'none',
				    duration: 2000
				  })
				} else {
				  this.$u.post(`/app/device/adminAll/${this.roomId}/switch?open=false`).then(res => {
				    if (res.code == 200) {
				      uni.showToast({
				        title: '全部关闭成功',
				        icon: 'success',
				        duration: 2000
				      })
				      this.roomList.forEach(item => {
				        item.device.powerStatus = 0
				      })
				    } else {
				      uni.showToast({
				        title: res.msg,
				        icon: 'none',
				        duration: 2000
				      })
				    }
				  })
				}
			},
			// 设施全开
			btnkai(){
				const allDeviceIdsAreNull = this.roomList.every(item => item.deviceId == null)
				if (allDeviceIdsAreNull) {
				  uni.showToast({
				    title: '该房间设施暂无绑定设备',
				    icon: 'none',
				    duration: 2000
				  })
				} else {
				  this.$u.post(`/app/device/adminAll/${this.roomId}/switch?open=true`).then(res => {
				    if (res.code == 200) {
				      uni.showToast({
				        title: '全部开启成功',
				        icon: 'success',
				        duration: 2000
				      })
				      this.roomList.forEach(item => {
				        item.device.powerStatus = 1
				      })
				    } else {
				      uni.showToast({
				        title: res.msg,
				        icon: 'none',
				        duration: 2000
				      })
				    }
				  })
				}
			},
			// 点击控制房间设施
			btnroomkg(num,index,item){
				console.log(item);
				this.lanyaobj = item
				if(item.device.onlineStatus == 1){
					let equipmentId = item.equipmentId
					if(num == 0){ //关闭
						this.$u.put(`/app/device/adminMch/${equipmentId}/switch?open=false`).then(res =>{
							if(res.code == 200){
								uni.showToast({
									title: '关闭成功',
									icon: 'success',
									duration: 2000
								})
								this.roomList.forEach(item =>{
									if(item.equipmentId == equipmentId){
										item.device.powerStatus = 0
									}
								})
							}else{
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 2000
								})
							}
						})
					}else{ //开启
						this.$u.put(`/app/device/adminMch/${equipmentId}/switch?open=true`).then(res =>{
							if(res.code == 200){
								uni.showToast({
									title: '开启成功',
									icon: 'success',
									duration: 2000
								})
								this.roomList.forEach(item =>{
									if(item.equipmentId == equipmentId){
										item.device.powerStatus = 1
									}
								})
							}else{
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 2000
								})
							}
						})
					}
				}else{
					this.deviceId = item.device.mac
					this.lanyamac = item.device.mac
					console.log(this.deviceId);
					this.getlanya()
				}
			},
			// 请求房间详情
			getfjxq(){
				this.$u.get(`/app/room/mch/${this.roomId}`).then(res =>{
					if(res.code == 200){
						this.roomobj = res.data
						this.roomList = res.data.equipmentList
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	/deep/ .u-iconfont,
	/deep/ .u-title{
		padding-bottom: 20rpx;
	}
	.mask {
		width: 100%;
		height: 100vh;
		background-color: #000;
		opacity: .3;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 1;
	}
	// 离线蓝牙开关
	.lxlanya{
		position: fixed;
		top: 40%;
		left: 50%;
		transform: translateX(-50%);
		width: 500rpx;
		max-height:600rpx;
		background-color: #fff;
		border-radius: 30rpx;
		z-index: 9;
		padding: 30rpx 40rpx;
		box-sizing: border-box;
		.cha{
			font-size: 50rpx;
			position: absolute;
			top: 20rpx;
			right: 30rpx;
			font-weight: 600;
		}
		.lxzt{
			margin-top: 30rpx;
			padding: 0 20rpx;
			box-sizing: border-box;
			view{
				display: flex;
				justify-content: space-between;
				align-items: center;
				image{
					width: 80rpx;
					height: 80rpx;
				}
			}
		}
		.lxtit{
			font-size: 32rpx;
			font-weight: 600;
			width: 100%;
			text-align: center;
			margin-top: 30rpx;
		}
		.kongzhi{
			display: flex;
			justify-content: space-between;
			margin-top: 30rpx;
			.lxkai{
				width: 200rpx;
				height: 80rpx;
				background-color: #48893B;
				color: #fff;
				text-align: center;
				line-height: 80rpx;
				border-radius: 20rpx;
			}
		}
	}
	.damen{
		padding: 0 30rpx;
		box-sizing: border-box;
		margin-top: 38rpx;
		height: 830rpx;
		overflow: scroll;
		.fjlist{
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			margin-top: 16rpx;
			width: 686rpx;
			max-height: 1408rpx;
			background: #FFFFFF;
			padding: 16rpx;
			padding-top: 0;
			box-sizing: border-box;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			.fj_item{
				margin-top: 16rpx;
				width: 318rpx;
				height: 178rpx;
				background: #F9F9F9;
				padding: 26rpx;
				box-sizing: border-box;
				.zt{
					font-size: 28rpx;
					color: #FC902A;
					margin-top: 8rpx;
				}
				.name{
					// display: flex;
					// justify-content: space-between;
					text{
						display: block;
						font-weight: 600;
						font-size: 36rpx;
						color: #3D3D3D;
					}
				}
			}
		}
		.name{
			font-weight: 600;
			font-size: 36rpx;
			color: #3D3D3D;
			text-align: left;
		}
		.menbj{
			width: 686rpx;
			max-height: 170rpx;
			background: #FFFFFF;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			padding: 30rpx;
			box-sizing: border-box;
			margin-top: 28rpx;
			.bot{
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-top: 20rpx;
				.lt{
					font-size: 28rpx;
					color: #3D3D3D;
				}
				.rt{
					image{
						width: 50rpx;
						height: 50rpx;
					}
				}
			}
			.top{
				display: flex;
				justify-content: space-between;
				.lt{
					text{ 
						font-size: 28rpx;
						color: #3D3D3D;
						margin-right: 54rpx;
					}
				}
				.rt{
					display: flex;
					align-items: center;
					font-size: 28rpx;
					color: #7C7C7C;
					font-weight: 600;
					text{
						width: 10rpx;
						height: 10rpx;
						background-color: #7C7C7C;
						border-radius: 50%;
						margin-right: 10rpx;
					}
				}
			}
		}
	}
	.boxmask{
		width: 100%;
		height: 100vh;
		background-color: #000;
		opacity: .3;
		z-index: 2;
		position: fixed;
		top: 0;
		left: 0;
	}
	.box{
		position: fixed;
		z-index: 2;
		top: 500rpx;
		left: 50%;
		transform: translateX(-50%);
		width: 580rpx;
		max-height: 408rpx;
		background: #FFFFFF;
		margin: auto;
		margin-top: 22rpx;
		padding-top: 20rpx;
		box-sizing: border-box;
		border-radius: 20rpx;
		.but{
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-top:1px solid #D8D8D8;
			font-size: 32rpx;
			font-weight: 600;
			.quxiao{
				border-right: 1px solid #D8D8D8;
			}
			view{
				width: 50%;
				height: 100rpx;
				line-height: 100rpx;
				text-align: center;
			}
		}
		.topss{
			width: 100%;
			text-align: center;
			margin-top: 20rpx;
			margin-bottom: 20rpx;
			font-size: 36rpx;
		}
		.name{
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin: auto;
			height: 80rpx;
			padding: 0 30rpx;
			margin-bottom: 10rpx;
			box-sizing: border-box;
			.lt{
				font-size: 32rpx;
				color: #3D3D3D;
			}
			.rt{
				display: flex;
				align-items: center;
				input{
					text-align: right;
					width: 200rpx;
				}
				image{
					width: 14rpx;
					height: 42rpx;
					margin-left: 10rpx;
				}
			}
		}
	}
	.pricegz{
		width: 636rpx;
		max-height: 438rpx;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		padding: 52rpx 46rpx;
		box-sizing: border-box;
		margin: auto;
		position: fixed;
		top: 490rpx;
		left: 50%;
		transform: translateX(-50%);
		z-index: 3;
		.top{
			font-weight: 600;
			font-size: 32rpx;
			color: #3D3D3D;
		}
		.shuom{
			font-size: 28rpx;
			color: #3D3D3D;
			margin-top: 24rpx;
		}
		.annius{
			width: 542rpx;
			height: 86rpx;
			background: #48893B;
			border-radius: 14rpx 14rpx 14rpx 14rpx;
			text-align: center;
			line-height: 86rpx;
			font-weight: 600;
			font-size: 36rpx;
			color: #FFFFFF;
			margin-top: 30rpx;
		}
	}
	.mask{
		width: 100%;
		height: 100vh;
		background-color: #000;
		opacity: .3;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 3;
	}
	.addmen{
		width: 694rpx;
		height: 702rpx;
		background: linear-gradient( 180deg, #DEF1DA 0%, #FFFFFF 100%, #FFFFFF 100%);
		border-radius: 36rpx 36rpx 36rpx 36rpx;
		position: fixed;
		top: 464rpx;
		left: 50%;
		transform: translateX(-50%);
		z-index: 3;
		box-shadow: 1px 2px 4px -3px;
		.one{
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 100%;
			.rt{
				image{
					width: 60rpx;
					height: 60rpx;
				}
			}
			.saoma{
				font-weight: 600;
				font-size: 36rpx;
				color: #48893B;
				margin-top: 10rpx;
			}
			.saomas{
	            font-size: 28rpx;
	            color: #48893B;		
				margin-top: 10rpx;
			}
			width: 584rpx;
			height: 224rpx;
			border: 2rpx solid #48893B;
			border-radius: 20rpx;
			margin: auto;
			margin-top: 48rpx;
			padding: 38rpx 44rpx;
			box-sizing: border-box;
		}
		.top{
			display: flex;
			flex-wrap: wrap;
			padding: 0 26rpx;
			box-sizing: border-box;
			.name{
				font-weight: 600;
				font-size: 44rpx;
				color: #48893B;
				width: 100%;
				text-align: center;
				position: absolute;
				top: 56rpx;
				left: 50%;
				transform: translateX(-50%);
			}
			.cha{
				font-size: 68rpx;
				font-weight: 600;
				width: 100%;
				text-align: right;
			}
		}
	}
	.anniu{
		width: 750rpx;
		height: 152rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 8rpx 20rpx 0rpx rgba(0,0,0,0.3);
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		position: fixed;
		left: 0;
		bottom: 0;
		display: flex;
		justify-content: space-between;
		padding: 0 36rpx;
		box-sizing: border-box;
		padding-top: 24rpx;
		.xinjian{
			width: 214rpx;
			height: 104rpx;
			border: 2rpx solid #48893B;
			border-radius: 20rpx;
			text-align: center;
			line-height: 104rpx;
			font-weight: 600;
			font-size: 36rpx;
			color: #48893B;
		}
	}
	page {
		background: #F6F6F6;
	}
	.list{
		padding: 0 32rpx;
		box-sizing: border-box;
		margin-top: 42rpx;
		.item_list{
			display: flex;
			justify-content: space-between;
			margin-top: 18rpx;
			max-height: 184rpx;
			.rt{
				.name{
					display: flex;
					align-items: center;
					text{
						width: 10rpx;
						height: 10rpx;
						background: #7C7C7C;
						border-radius: 50%;
						margin-right: 10rpx;
						font-size: 28rpx;
						color: #969696;
					}
				}
				.img{
					text-align: right;
					margin-top: 20rpx;
					image{
						width: 50rpx;
						height: 50rpx;
					}
				}
			}
			.lt{
				.name{
					font-weight: 600;
					font-size: 32rpx;
					color: #3D3D3D;
					text{
						font-size: 28rpx;
						color: #969696;
						font-weight: 400;
						margin-left: 30rpx;
					}
				}
				.sn{
					font-size: 26rpx;
					color: #3D3D3D;
					margin-top: 26rpx;
				}
			}
			padding: 32rpx 30rpx;
			box-sizing: border-box;
			width: 680rpx;
			background: #FFFFFF;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
		}
	}
	.boxtop{
		width: 680rpx;
		height: 244rpx;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		margin: auto;
		margin-top: 22rpx;
		padding: 34rpx 30rpx;
		box-sizing: border-box;
		.bot{
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 40rpx;
			view{
				display: flex;
				align-items: center;
				justify-content: center;
				width: 204rpx;
				height: 54rpx;
				border-radius: 27rpx 27rpx 27rpx 27rpx;
				border: 1rpx solid #7C7C7C;
				image{
					width: 32rpx;
					height: 32rpx;
					margin-right: 6rpx;
				}
			}
		}
		.name{
			font-weight: 600;
			font-size: 30rpx;
			color: #3D3D3D;
		}
		.yuny{
			display: flex;
			align-items: center;
			font-weight: 600;
			font-size: 28rpx;
			color: #48893B;
			text{
				width: 10rpx;
				height: 10rpx;
				background: #48893B;
				border-radius: 50%;
				margin-right: 10rpx;
			}
		}
		.top{
			display: flex;
			justify-content: space-between;
			border-bottom: 1px solid #D8D8D8;
			padding-bottom: 22rpx;
			box-sizing: border-box;
		}
	}
	.tops{
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 36rpx;
		box-sizing: border-box;
		margin-top: 70rpx;
		position: relative;
		.tianjia{
			width: 170rpx;
			height: 214rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 6rpx 0rpx rgba(0,0,0,0.3);
			border-radius: 12rpx 12rpx 12rpx 12rpx;
			position: absolute;
			top: 80rpx;
			right: 36rpx;
			view{
				width: 120rpx;
				height: 70rpx;
				line-height: 70rpx;
				text-align: center;
				margin: auto;
				border-bottom: 1px solid #D8D8D8;
				font-size: 28rpx;
				color: #3D3D3D;
			}
		}
		.lt{
			font-weight: 600;
			font-size: 36rpx;
			color: #3D3D3D;
		}
		.rt{
			width: 126rpx;
			height: 58rpx;
			background: #48893B;
			border-radius: 6rpx 6rpx 6rpx 6rpx;
			font-weight: 600;
			text-align: center;
			line-height: 58rpx;
			font-size: 28rpx;
			color: #FFFFFF;
		}
	}

 
</style>