<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>
		<u-select v-model="show" :list="list" @confirm="confirm"></u-select>
		<view class="boxmask" v-if="flag"></view>
		<view class="box" v-if="flag">
			<view class="topss">
				添加设施
			</view>
			<view class="name">
				<view class="lt">
					名称
				</view>
				<view class="rt">
					<input type="text" v-model="name" placeholder="请输入设施名称"/> <image src="https://api.ccttiot.com/smartmeter/img/static/uCta8cp7uXd1lGjZS6sc" mode=""></image>
				</view> 
			</view>
			<view class="name" style="border: none;">
				<view class="lt">
					类型
				</view>
				<view class="rt" @click="show = true">
					{{leixing}}  <image src="https://api.ccttiot.com/smartmeter/img/static/uCta8cp7uXd1lGjZS6sc" mode=""></image>
				</view> 
			</view>
			<view class="but">
				<view class="quxiao" @click="flag = false">
					取消
				</view>
				<view class="queding" @click="btnmd">
					确定
				</view>
			</view>
		</view>
        <view class="boxtop">
        	<view class="top">
        		<view class="name">
        			{{roomobj.roomName == null ? '--' : roomobj.roomName}}
        		</view>
				<view class="yuny" v-if="roomobj.status == 1">
					<text></text> 运营中
				</view>
				<view style="color: #ccc;" class="yuny" v-if="roomobj.status == 0">
					<text style="background-color: #ccc;"></text> 歇业中
				</view>
				<view style="color: #ccc;" class="yuny" v-if="roomobj.status == 8">
					<text style="background-color: #ccc;"></text> 下线维护
				</view>
        	</view>
			<view class="bot">
				<!-- <view class="" @click="btnjfgz">
					<image src="https://api.ccttiot.com/smartmeter/img/static/udeYbjiOdwddMbUM90QB" mode=""></image> 计费规则
				</view> -->
				<view class="" @click="btnewm">
					<image src="https://api.ccttiot.com/smartmeter/img/static/uT7NCRBVoRy8KFIF9Gtq" mode=""></image> 下载二维码
				</view>
				<view class="" @click="btnroomsz">
					<image src="https://api.ccttiot.com/smartmeter/img/static/uTBNF3xzr0EAJjzJ3Ggc"mode=""></image> 房间设置
				</view>
			</view>
        </view>
		<view class="tops">
			<view class="lt">
				房间设施列表
			</view>
			<view class="rt" @click="btnaddsb">
				添加
			</view>
			<!-- <view class="tianjia" v-if="flag">
				<view class="" @click="btnaddfm(1)">
					添加房门
				</view>
				<view class="" @click="btnaddfm(2)">
					添加电源
				</view>
				<view class="" @click="btnaddfm(3)" style="border: none;">
					添加设施
				</view>
			</view> -->
		</view>
        <view class="damen" v-if="roomList != ''">
        	<view class="menbj" v-for="(item,index) in roomList" :key="index" @click="btnitem(item)">
        		<view class="top">
        			<view class="lt">
        				<text style="font-weight: 600;">{{item.name}}</text> <text v-if="item.device.onlineStatus == 0" class="lixian">设备离线</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">
        			<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="mask" v-if="fmflag"></view>
		<view class="addmen" v-if="fmflag">
			<view class="top">
				<view class="cha" @click="fmflag = false">
					×
				</view>
				<view class="name">
					请选择添加{{roomtxt}}方式
				</view>
			</view>
			<view class="one" @click="btntoufcesuo">
				<view class="lt">
					<view class="saoma">
						扫码添加
					</view>
					<view class="saomas">
						扫描设备上的二维码
					</view>
					<view class="saomas">
						设置{{roomtxt}}
					</view>
				</view>
				<view class="rt">
					<image src="https://api.ccttiot.com/smartmeter/img/static/ukzEfdUUjPKUUOxonIgm" mode=""></image>
				</view>
			</view>
			<view class="one" @click="btntouffm">
				<view class="lt">
					<view class="saoma">
						未投放设备列表添加
					</view>
					<view class="saomas">
						选择列表中的设备
					</view>
					<view class="saomas">
						设置{{roomtxt}}
					</view>
				</view>
				<view class="rt">
					<image src="https://api.ccttiot.com/smartmeter/img/static/ukzEfdUUjPKUUOxonIgm" mode=""></image>
				</view>
			</view>
		</view>
        <view class="mask" v-if="pricegzflag"></view>
		<view class="pricegz" v-if="pricegzflag">
			<view class="top">
				下单须知：
			</view>
			<view class="shuom">
				基础单价每1小时10元，最低消费时长2小 时，押金金额100元（订单结束30分钟内退 还）注：订单提前结束剩余消费时长、金额 不返还
			</view>
			<view class="annius" @click="pricegzflag = false">
				确认
			</view>
		</view>
		
		<view class="anniu">
			<view class="xinjian" @click="btnxia">
				{{txtname}}
			</view>
			<view class="xinjian" @click="btnkai">
				全开
			</view>
			<view class="xinjian" @click="btnguan">
				全关
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
                bgc: {
                	backgroundColor: "#fff",
                },
				roomId:'',
				flag:false,
				fmflag:false,
				roomtxt:'',
				pricegzflag:false,
				roomobj:{},
				storeId:'',
				roomList: [],
				name:'',
				leixing:'请选择类型',
				type:'',
				show:false,
				list:[
					{
						value: '1',
						label: '房间门'
					},
					{
						value: '4',
						label: '房间灯'
					},
				],
				txtname:''
			}
		},
		onLoad(option) {
            this.roomId = option.roomId
			this.storeId = option.storeId
		},
		onShow() {
			this.getfjxq()
		},
		methods: {
			// 点击添加设施
			btnaddsb(){
				uni.navigateTo({
					url:'/page_shanghu/roomsheshi?roomId='  + this.roomId
				})
			},
			// 点击房间下线维护
			btnxia(){
				let that = this
				if(that.txtname == '下线维护'){
					uni.showModal({
						    title: '温馨提示',  
						    content: '您确定要下线维护吗?',  
						    success: function (res) {  
						        if (res.confirm) {
									let data = {
										roomId:that.roomId,
										status:8
									}
					                that.$u.put(`/app/room`,data).then(res =>{
										if(res.code == 200){
											uni.showToast({
												title: '下线成功',
												icon: 'success',
												duration: 2000
											})
											that.txtname = '上线'
											that.roomobj.status = 8
										}else{
											uni.showToast({
												title: res.msg,
												icon: 'none',
												duration: 2000
											})
										}
									})
						        } else if (res.cancel) {
						          
						    }  
						}  
					})
				}else{
                    uni.showModal({
						    title: '温馨提示',  
						    content: '您确定要将此房间上线吗?',  
						    success: function (res) {  
						        if (res.confirm) {
									let data = {
										roomId:that.roomId,
										status:1
									}
					                that.$u.put(`/app/room`,data).then(res =>{
										if(res.code == 200){
											uni.showToast({
												title: '上线成功',
												icon: 'success',
												duration: 2000
											})
											that.txtname = '下线维护'
											that.roomobj.status = 1
										}else{
											uni.showToast({
												title: res.msg,
												icon: 'none',
												duration: 2000
											})
										}
									})
						        } else if (res.cancel) {
						          
						    }  
						}  
					})
				}
			},
			// 设施全关
			btnguan(){
				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(){
				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){
				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
							})
						}
					})
				}
			},
			// 点击房间中设施
			btnitem(item){
				// console.log(item);
				uni.navigateTo({
					url:'/page_shanghu/roomsheshi?equipmentId=' + item.equipmentId
				})
			},
			// 点击确定新建设施
			btnmd(){
				let data = {
					name:this.name,
					roomId:this.roomId,
					type:this.type,
					picture:''
				}
				this.$u.post(`/app/equipment`,data).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '新建成功',
							icon: 'success',
							duration: 2000
						})
						this.flag = false
						this.roomList = []
						this.getfjxq()
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			// 确认选择类型
			confirm(e){
				this.type = e[0].value
				this.leixing = e[0].label
			},
			// 点击弹出计费规则
			btnjfgz(){
				this.pricegzflag = true
			},
			// 跳转到编辑房间页
			btnroomsz(){
				uni.navigateTo({
					url:'/page_shanghu/roomedit?roomId=' + this.roomId
				})
			},
			// 跳转到保存二维码
			btnewm(){
				uni.navigateTo({
					url:'/page_shanghu/erwm'
				})
			},
			// 点击进行扫码
			btntoufcesuo(){
				this.fmflag = false
				uni.scanCode({
					onlyFromCamera: true,
					scanType: ['qrCode'],
					success: res => {
						function getQueryParam(url, paramName) {
							let regex = new RegExp(`[?&]${paramName}=([^&]*)`)
							let results = regex.exec(url);
							return results ? decodeURIComponent(results[1].replace(/\+/g, ' ')) : null
						}
						console.log(res);
						let sceneValue = res.result
						let decodedValue = decodeURIComponent(sceneValue)
						let id = getQueryParam(decodedValue, 'sn')
						let that = this
						let data = {
							sn: id,
							storeId:this.storeId
						}
                        uni.navigateTo({
                        	url:'/page_shanghu/weishebei'
                        })
					}
				})
			},
			// 点击未投放列表选择设备
			btntouffm(){
				this.fmflag = false
				uni.navigateTo({
					url:'/page_shanghu/weishebei'
				})
			},
			// 点击添加房门
			btnaddfm(num){
				if(num == 1){
					this.roomtxt = '房门'
					this.fmflag = true
					this.flag = false
				}else if(num == 2){
					this.roomtxt = '电源'
					this.fmflag = true
					this.flag = false
				}else if(num == 3){
					this.roomtxt = '设施'
					this.fmflag = true
					this.flag = false
				}
			},
			// 请求房间详情
			getfjxq(){
				this.$u.get(`/app/room/mch/${this.roomId}`).then(res =>{
					if(res.code == 200){
						this.roomobj = res.data
						this.roomList = res.data.equipmentList
						if(res.data.status == 8){
							this.txtname = '上线'
						}else{
							this.txtname = '下线维护'
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	/deep/ .u-iconfont,
	/deep/ .u-title{
		padding-bottom: 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>