<template>
	<view class="page">
		<u-navbar title="设备信息" :border-bottom="false" :background="bgc" back-icon-color="#fff" title-color='#fff'
			title-size='36' height='48' id="navbar">
		</u-navbar>
		<view class="">
			<view class="toufangbox">
				<view class="shebeibj">
					<view class="lt">
						<view class="zt">
							设备名称：<text>{{xxobj.deviceName}}</text>
						</view>
						<view class="zt">
							SN：<text>{{xxobj.deviceNo}}</text>
						</view>
						<view class="zt">
							MAC：<text>{{xxobj.mac}}</text>
						</view>
						<view class="zt">
							设备型号：<text>{{xxobj.modelName}}</text>
						</view>
						<view class="zt">
							设备状态：<text v-if="xxobj.powerStatus == 1">开启</text><text v-if="xxobj.powerStatus == 0">关闭</text>
						</view>
						<view class="zt">
							在线状态：<text v-if="xxobj.onlineStatus == 1">在线</text><text v-if="xxobj.onlineStatus == 0">离线</text>
						</view>
					</view>
					<view class="rt">
					    <image :src="xxobj.picture" mode=""></image>	
					</view>
				</view>
			</view>
			<view class="kongzhi">
				<view class="jiebang" @click="btngb">
					关闭
				</view>
				<view class="tougang" @click="btnkq">
					开启
				</view>
			</view>
			<view class="kongzhi" v-if="ver_dataflag == 1">
				<view class="jiebang" style="width: 100%;" @click="findDevice">
				   连接蓝牙 
				</view>
			</view>
			<view class="kongzhi" v-if="ver_dataflag == 3">
				<view class="jiebang" style="width: 100%;" @click="btnly">
				   蓝牙已连接
				</view>
			</view>
			<view class="kongzhi" v-if="ver_dataflag == 3">
				<view class="jiebang" @click="btnlyclose">
					蓝牙关闭
				</view>
				<view class="tougang" @click="btnlyopen">
					蓝牙开启
				</view>
			</view>
			
			<view class="anniu">
				<view class="jiebang" style="width: 100%;" @click="btndel">
					删除
				</view>
			</view>
		</view>
		
		<view class="mask" v-if="editnameflag"></view>
		<view class="editname" v-if="editnameflag">
			<view class="top">
				修改设备名称
			</view>
			<input type="text" v-model="name" placeholder="请输入设备名称"/>
			<view class="btnanniu">
				<view class="quxiao" @click="editnameflag = false">
					取消
				</view>
				<view class="queren" @click="btneditname">
					确认
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var xBlufi = require("@/components/blufi/xBlufi.js")
	export default {
		data() {
			return {
                bgc: {
                	backgroundColor: "#0B0B0B",
                },
				deviceId:'',
				xxobj:{},
				xuanzeflag:false,
				editnameflag:false,
				name:'',
				mdianobj:{},
				sn:'',
				
				ver_dataflag: 1,
				mac: '',
				deviceid: '',
				devicename: '',
				devicesarr: [],
				findDeviceTimer: null,
			}
		},
		onLoad(option) {
			console.log(option)
            this.deviceId = option.deviceId
			this.getxx()
		},
		onShow() {
			xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent)
		},
		methods: {
			btnly(){
				uni.showToast({
					title: '蓝牙已连接',
					icon: 'none',
					duration: 2000
				})
			},
			btnlyopen(){
				xBlufi.notifySendCustomData({
					customData: "11open@"
				})
				this.xxobj.powerStatus = 1
			},
			btnlyclose(){
				xBlufi.notifySendCustomData({
					customData: "11close@"
				})
				this.xxobj.powerStatus = 0
			},
			// 判断离线连接蓝牙
			findDevice() {
				uni.showLoading({
					title: '蓝牙连接中...'
				})
				const matchedDevice = this.devicesarr.find(device => device.name.slice(-12) === this.mac.slice(-12))
				if (matchedDevice) {
					xBlufi.notifyStartDiscoverBle({
						'isStart': false
					})
					xBlufi.notifyConnectBle({
						isStart: true,
						deviceId: matchedDevice.deviceId,
						name: matchedDevice.name
					})
					this.deviceid = matchedDevice.deviceId
					this.devicename = matchedDevice.name
				} else {
					this.findDeviceTimer = setTimeout(this.findDevice.bind(this), 1000)
				}
			},
			// 获取附近蓝牙设备列表
			funListenDeviceMsgEvent: function(options) {
				switch (options.type) {
					case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
						if (!options.result) {
							this.ver_dataflag = 1
							uni.showToast({
								title: '蓝牙已断开',
								icon: 'none',
								duration: 2000
							})
						}
						break;
					case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
						if (options.result) {
							this.devicesarr = options.data
						}
						break;
					case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
						if (options.result == true) {
							xBlufi.notifyInitBleEsp32({
								deviceId: this.deviceid
							})
							this.deviceid = options.data.deviceId
							this.name = this.name
							setTimeout(() => {
								this.ver_dataflag = 3
								uni.hideLoading()
								uni.showToast({
									title: '连接成功',
									icon: 'success',
									duration: 2000
								})
							}, 2000)
						} else {
							uni.hideLoading()
                            uni.showToast({
                            	title: '连接失败',
                            	icon: 'none',
                            	duration: 2000
                            })
						}
						break;
					case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
						if (!options.result) {
							uni.hideLoading()
							// uni.showToast({
							// 	title: '请开启蓝牙',
							// 	icon: 'none',
							// 	duration: 2000
							// })
							return
						}
						break;
				}
			},
			
			// 获取设备信息
			getxx(){
				this.$u.get(`/bst/device/${this.deviceId}`).then(res => {
					if (res.code == 200) {
						this.xxobj = res.data
						this.mac = res.data.mac
						this.name = this.xxobj.deviceName
					}
				})
			},
			// 开启设备
			btnkq(){
				this.$u.put(`/bst/device/iot/${this.deviceId}/switch?open=true`).then(res =>{
					if(res.code == 200){
						uni.showToast({
							title: res.msg,
							icon: 'success',
							duration: 2000
						})
						this.getxx()
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			// 关闭设备
			btngb(){
				this.$u.put(`/bst/device/iot/${this.deviceId}/switch?open=false`).then(res =>{
					if(res.code == 200){
						uni.showToast({
							title: res.msg,
							icon: 'success',
							duration: 2000
						})
						this.getxx()
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			// 点击删除设备
			btndel(){
				let that = this
				uni.showModal({
					    title: '温馨提示',  
					    content: '您确定要删除该设备吗?',  
					    success: function (res) {  
					        if (res.confirm) {
								that.$u.delete(`/bst/device/${that.deviceId}/unbind`).then(res => {
									if (res.code == 200) {
										uni.showToast({
											title: '删除成功',
											icon: 'success',
											duration: 2000
										})
										setTimeout(()=>{
											uni.navigateBack()
										},1000)
									}else{
										uni.showToast({
											title: res.msg,
											icon: 'none',
											duration: 2000
										})
									}
								})
					        } else if (res.cancel) {
					          
					    }  
					}  
				})
			},
			// 点击确定修改设备名称
			btneditname(){
				let data = {
					deviceName:this.name
				}
				this.$u.post(`/app/device/editName/${this.deviceId}?deviceName=${this.name}`,data).then(res =>{
					if(res.code == 200){
						this.getxx()
						this.name = ''
						this.editnameflag = false
						uni.showToast({
							title: '修改成功',
							icon: 'success',
							duration: 2000
						})
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			// 点击选择门店
			btnshop(){
				uni.navigateTo({
					url:'/page_shanghu/xuanzeshop?deviceId=' + this.deviceId
				})
			},
			
			// 点击解绑设备
			btnjiebang(){
				let that = this
				uni.showModal({
					    title: '温馨提示',  
					    content: '您确定要解绑该设备吗?',  
					    success: function (res) {  
					        if (res.confirm) {
								that.$u.post(`app/device/offline/${that.deviceId}`).then(res => {
									if (res.code == 200) {
										uni.showToast({
											title: '解绑成功',
											icon: 'success',
											duration: 2000
										})
										setTimeout(()=>{
											uni.navigateBack()
										},1000)
									}else{
										uni.showToast({
											title: res.msg,
											icon: 'none',
											duration: 2000
										})
									}
								})
					        } else if (res.cancel) {
					          
					    }  
					}  
				})
			}
		}
	}
</script>

<style lang="scss">
	::v-deep .u-iconfont,
	::v-deep .u-title{
		padding-bottom: 20rpx;
		box-sizing: border-box;
	}
	page{
		background-color: #0B0B0B;
	}
	.editname{
		width: 580rpx;
		max-height: 400rpx;
		background-color: #1A1A1A;
		border-radius: 24rpx;
		position: fixed;
		top: 414rpx;
		left: 50%;
		transform: translateX(-50%);
		.btnanniu{
			display: flex;
			margin-top: 60rpx;
			border-top: 1px solid #333333;
			.quxiao{
				border-right: 1px solid #333333;
			}
			view{
				width: 50%;
				height: 120rpx;
				line-height: 120rpx;
				text-align: center;
				font-size: 32rpx;
				font-weight: 600;
				color: #FFFFFF;
			}
		}
		input{
			width: 480rpx;
			height: 80rpx;
			line-height: 80rpx;
			margin: auto;
			background-color: #333333;
			margin-top: 50rpx;
			padding-left: 30rpx;
			box-sizing: border-box;
			border-radius: 12rpx;
			color: #FFFFFF;
		}
		.top{
			font-size: 32rpx;
			font-weight: 600;
			text-align: center;
			width: 100%;
			margin-top: 30rpx;
			color: #FFFFFF;
		}
	}
	.jieb{
		width: 750rpx;
		height: 152rpx;
		background: #0B0B0B;
		box-shadow: 0rpx 8rpx 20rpx 0rpx rgba(0,0,0,0.3);
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		.bt{
			width: 680rpx;
			height: 104rpx;
			background: #FF8998;
			margin: auto;
			margin-top: 24rpx;
			border-radius: 24rpx;
			font-weight: 600;
			font-size: 36rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 104rpx;
		}
	}
	.lianwang{
		width: 680rpx;
		height: 112rpx;
		background: #0B0B0B;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: auto;
		margin-top: 22rpx;
		border-radius: 24rpx;
		padding: 36rpx 32rpx;
		box-sizing: border-box;
		.two{
			font-size: 32rpx;
			color: #fff;
		}
		.thr{
			font-size: 32rpx;
			color: #fff;
			display: flex;
			align-items: center;
			image{
				width: 12rpx;
				height: 34rpx;
				margin-left: 10rpx;
			}
		}
	}
	.jichuxx{
		width: 680rpx;
		max-height: 458rpx;
		background: #0B0B0B;
		box-sizing: border-box;
		padding: 44rpx 34rpx;
		box-sizing: border-box;
		margin: auto;
		margin-top: 22rpx;
		border-radius: 20rpx;
		.top{
			border-left: 14rpx solid #FF8998;
			font-weight: 600;
			font-size: 36rpx;
			color: #fff;
			padding-left: 24rpx;
			box-sizing: border-box;
		}
		.one{
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 28rpx;
			.two{
				font-size: 32rpx;
				color: #fff;
			}
			.thr{
				font-size: 32rpx;
				color: #fff;
			}
		}
	}
	.toufangbox{
		width: 680rpx;
		background: #1A1A1A;
		border-radius: 24rpx;
		margin: 30rpx auto;
		padding: 40rpx 34rpx;
		box-sizing: border-box;
		.shebeibj{
			display: flex;
			justify-content: space-between;
			align-items: flex-start;
			.lt{
				flex: 1;
				margin-right: 30rpx;
				.name{
					font-weight: 600;
					font-size: 40rpx;
					color: #FFFFFF;
					margin-bottom: 20rpx;
				}
				.zt{
					font-size: 28rpx;
					color: #999999;
					margin-top: 16rpx;
					display: flex;
					align-items: center;
					text{
						color: #FFFFFF;
						margin-left: 10rpx;
					}
				}
			}
			.rt{
				image{
					width: 196rpx;
					height: 196rpx;
					border-radius: 12rpx;
				}
			}
		}
	}
	.xuanze{
		width: 750rpx;
		height: 494rpx;
		background: #0B0B0B;
		border-radius: 40rpx 40rpx 0 0;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 1;
		padding: 42rpx 62rpx;
		box-sizing: border-box;
		.changj{
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;
			.changj_item{
				width: 302rpx;
				height: 110rpx;
				background: #F0F0F0;
				border-radius: 6rpx 6rpx 6rpx 6rpx;
				font-weight: 600;
				text-align: center;
				line-height: 110rpx;
				font-size: 36rpx;
				margin-top: 40rpx;
				color: #fff;
			}
		}
		.top{
			display: flex;
			justify-content: space-between;
			view{
				font-weight: 600;
				font-size: 36rpx;
				color: #fff;
				image{
					width: 50rpx;
					height: 50rpx;
				}
			}
		}
	}
	.mask{
		width: 100%;
		height: 100vh;
		background-color: rgba(0,0,0,0.6);
		position: fixed;
		top: 0;
		left: 0;
		backdrop-filter: blur(4rpx);
	}
	.kongzhi{
		width: 100%;
		height: 152rpx;
		justify-content: space-between;
		display: flex;
		padding: 24rpx 36rpx;
		.jiebang{
			text-align: center;
			width: 330rpx;
			height: 104rpx;
			line-height: 104rpx;
			border: 2rpx solid #FF8998;
			font-size: 32rpx;
			color: #FF8998;
			border-radius: 52rpx;
			transition: all 0.3s;
			&:active {
				opacity: 0.8;
			}
		}
		.tougang{
			text-align: center;
			width: 330rpx;
			height: 104rpx;
			background: #FF8998;
			font-size: 32rpx;
			color: #FFFFFF;
			line-height: 104rpx;
			border-radius: 52rpx;
			transition: all 0.3s;
			&:active {
				opacity: 0.8;
			}
		}
	}
	.anniu{
		width: 100%;
		height: 152rpx;
		background: #1A1A1A;
		box-shadow: 0 -4rpx 20rpx rgba(0,0,0,0.2);
		display: flex;
		padding: 24rpx 36rpx;
		box-sizing: border-box;
		justify-content: space-between;
		text-align: center;
		position: fixed;
		left: 0;
		bottom: 0;
		.jiebang{
			width: 100%;
			height: 104rpx;
			line-height: 104rpx;
			background: #FF8998;
			font-size: 32rpx;
			color: #FFFFFF;
			border-radius: 52rpx;
			transition: all 0.3s;
			&:active {
				opacity: 0.8;
			}
		}
	}
	.box{
		width: 680rpx;
		height: 708rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		margin: auto;
		margin-top: 28rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		.ulbox{
			width: 100%;
			height: 118rpx;
			line-height: 118rpx;
			display: flex;
			justify-content: space-between;
			border-bottom: 1px solid #D8D8D8;
			.one{
				font-size: 32rpx;
				color: #3D3D3D;
			}
			.two{
				font-size: 32rpx;
				color: #fff;
				display: flex;
				align-items: center;
				image{
					width: 14rpx;
					height: 42rpx;
					margin-left: 20rpx;
				}
			}
		}
	}
	page {
		background: #0B0B0B;
	}

</style>