<template>
	<view class="page">
		<u-navbar title="编辑房间" :border-bottom="false" :background="bgc" back-icon-color="#262B37" title-color='#262B37'
			title-size='36' height='40' id="navbar">
		</u-navbar>
       <view class="dalist">
       	  <view class="box">
       	  	<view class="name">
       	  		<view class="lt">
       	  			名称
       	  		</view>
       	  		<view class="rt">
       	  			<input type="text" v-model="nametxt" 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>
       	  <view class="biaoqian">
       	  	<view class="name">
       	  		标签设置
       	  	</view>
       	  	<view class="bqliat">
       	  	    <view 
       	  	        class="bqitem" 
       	  	        v-for="(item, index) in biaoqianlist" 
       	  	        :key="index" 
       	  	        @click="btnbq(item.dictValue,index)" 
       	  	        :class="{ 'active': isSelected(item.dictValue) }">
       	  	        {{ item.dictLabel }}
       	  	    </view>
       	  	</view>
       	  </view>
       	  <view class="biaoqian">
       	  	<view class="name">
       	  		房间照片
       	  	</view>
       	  	<image v-if="imgurl == ''" @click="getImage" src="https://api.ccttiot.com/smartmeter/img/static/uRBDOOtzMZgSbCcXQPGH" mode=""></image>
       	  	<image v-else :src="imgurl" @click="getImage" mode=""></image>
       	  	<ksp-cropper mode="free" :width="150" :height="150" :maxWidth="1024" :maxHeight="1024" :url="url"
       	  		@cancel="oncancel" @ok="onok"></ksp-cropper>
       	  </view>
       	  
       	  <view class="biaoqian">
       	  	<view class="name">
       	  		价格设置
       	  	</view>
       	  	<view class="shoufei" @click="btnshoufei">
       	  		<view class="shang">
       	  			<view class="">
       	  				收费方式
       	  			</view>
       	  			<view class="">
       	  				 <image src="https://api.ccttiot.com/smartmeter/img/static/u0rIwef50qNN4aaeqSfl" mode=""></image>
       	  			</view>
       	  		</view>
       	  	</view>
			<view class="" style="margin-top: 10rpx;">
				<text style="margin-right: 20rpx;color: #48893B;" v-for="(item,index) in namelist" :key="index">{{item}}</text>
			</view>
       	  </view>
       	  
       	  
       	  <u-select v-model="show" :list="list" @confirm="confirm"></u-select>
       	  <view class="anniu">
       	  	<view class="xinjian" @click="btndt">
       	  		删除
       	  	</view>
       	  	<view class="queren" @click="btnmd">
       	  		确认编辑
       	  	</view>
       	  </view>
       </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
                bgc: {
                	backgroundColor: "#fff",
                },
				leixing:'请选择类型',
				show:false,
				checked:false,
				list:[
					{
						value: '2',
						label: '棋牌'
					},
					{
						value: '1',
						label: '茶室'
					},
					{
						value: '3',
						label: '台球'
					},
				],
				type:'',
				biaoqianlist:[],
				selectedIndices:[],
				token:'',
				url:'',
				imgurl:'',
				roomId:'',
				roomobj:{},
				nametxt:'',
				namelist:[],
				ruleIdlist:[],
			}
		},
		onLoad(option) {
            this.roomId = option.roomId
			this.getbiaoqian()
			this.getQiniuToken()
			this.getroomxq()
		},
		onShow() {
			if(uni.getStorageSync('namelist') && uni.getStorageSync('ruleIdlist')){
				this.namelist = uni.getStorageSync('namelist')
				this.ruleIdlist = uni.getStorageSync('ruleIdlist')
			} 
		},
		onHide() {
		  // 移除存储中的数据
		  uni.removeStorageSync('namelist')
		  uni.removeStorageSync('ruleIdlist')
		},
		onUnload() {
		  // 移除存储中的数据
		  uni.removeStorageSync('namelist')
		  uni.removeStorageSync('ruleIdlist')
		},
		methods: {
			// 获取房间信息
			getroomxq(){
				this.$u.get(`/app/room/mch/${this.roomId}`).then((res) => {
					if (res.code == 200) {
						this.nametxt = res.data.roomName
						this.imgurl = res.data.pictures[0]
						if(res.data.feeRules != null){
							res.data.feeRules.forEach(item =>{
								this.namelist.push(item.explain)
							})
						}
						this.ruleIdlist = res.data.ruleIds
						this.selectedIndices = res.data.tags
						if(res.data.type == 1){
							this.leixing = '茶室'
							this.type = 1
						}else if(res.data.type == 2){
							this.leixing = '棋牌'
							this.type = 2
						}else if(res.data.type == 3){
							this.leixing = '台球'
							this.type = 3
						}
					}
				})
			},
			// 点击选择收费
			btnshoufei(){
				uni.navigateTo({
					url:'/page_moban/shoufei'
				})
			},
			// 点击选择标签
			btnbq(dictValue,index) {
			const indexExists = this.selectedIndices.includes(dictValue);
			    if (indexExists) {
			        this.selectedIndices = this.selectedIndices.filter(i => i !== dictValue);
			    } else {
			        this.selectedIndices.push(dictValue);
			    }
			},
			isSelected(dictValue) {
			    return this.selectedIndices.includes(dictValue);
			},
			// 获取标签
			getbiaoqian(){
				this.$u.get(`/appVerify/getDictData?dictType=ss_room_tags`).then((res) => {
					if (res.code == 200) {
						this.biaoqianlist = res.data
					}
				})
			},
			// 点击上传图片
			getImage() {
				uni.chooseImage({
					count: 1,
					success: (rst) => {
						this.url = rst.tempFilePaths[0]
					}
				})
			},
			onok(ev) {
				this.url = ""
				this.path = ev.path
				let _this = this
				let math = 'static/' + _this.$u.guid(20)
				wx.uploadFile({
					url: 'https://up-z2.qiniup.com',
					name: 'file',
					filePath: _this.path,
					formData: {
						token: _this.token, //后端返回的token
						key: 'smartmeter/img/' + math
					},
					success: function(res) {
						let str = JSON.parse(res.data)
						_this.imgurl = 'https://lxnapi.ccttiot.com/' + str.key
					}
				})
			},
			oncancel() {
			     // url设置为空,隐藏控件
			     this.url = "";
			},
			// 获取上传七牛云token
			getQiniuToken() {
				this.$u.get("/common/qiniu/uploadInfo").then((res) => {
					if (res.code == 200) {
						this.token = res.token
					}
				})
			},
			// 确认选择类型
			confirm(e){
				this.type = e[0].value
				this.leixing =e[0].label
			},
			// 点击确定新建房间
			btnmd(){
				let data = {
					roomId:this.roomId,
					roomName:this.nametxt,
					type:this.type,
					picture:this.imgurl,	
					tags:this.selectedIndices,
					ruleIds:this.ruleIdlist
				}
				this.$u.put(`/app/room`,data).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
						})
					}
				})
			},
			// 点击删除房间
			btndt(){
				let that = this
				uni.showModal({
					    title: '温馨提示',  
					    content: '您确定要删除此房间吗?',  
					    success: function (res) {  
					        if (res.confirm) {
                               that.$u.delete(`/app/room/${that.roomId}`).then((res) => {
                               	  if (res.code == 200) {
                               	  	 uni.showToast({
                               	  	 	title: '删除成功',
                               	  	 	icon: 'success',
                               	  	 	duration:2000
                               	  	 })
									 setTimeout(()=>{
										 uni.navigateBack({
											 delta:2
										 })
									 },1000)
                               	  }else{
									 uni.showToast({
									 	title: res.msg,
									 	icon: 'none',
									 	duration:2000
									 })
								  }
                               })
					        } else if (res.cancel) {
					          
					    }  
					}  
				})
			}
			
		}
	}
</script>

<style lang="scss">
	.active{
		background-color: #48893B !important;
		color: #fff !important;  
	}
	/deep/ .panel{
		position: fixed;
		left: 0;
	}
	page {
		background: #F6F6F6;
	}
	.dalist{
		width: 100%;
		height: 80vh;
		overflow: scroll;
		padding-bottom: 40rpx;
		box-sizing: border-box;
	}
	.biaoqian{
		width: 680rpx;
		max-height: 1624rpx;
		background: #FFFFFF;
		padding: 44rpx 34rpx;
		box-sizing: border-box;
		margin: auto;
		margin-top: 22rpx;
		border-radius: 20rpx;
		.shoufei{
			margin-top: 30rpx;
			.wz{
				font-size: 24rpx;
				color: #3D3D3D;
				margin-top: 24rpx;
				margin-bottom: 20rpx;
			}
			.shichang{
				margin-top: 34rpx;
				.stop{
					display: flex;
					width: 618rpx;
					height: 76rpx;
					line-height: 76rpx;
					background: #F0F0F0;
					view{
						width: 50%;
						text-align: center;
						font-size: 28rpx;
						color: #3D3D3D;
					}
				}
			}
			.shang{
				display: flex;
				justify-content: space-between;
				align-items: center;
				width: 100%;
				view{
					font-size: 32rpx;
					color: #3D3D3D;
					display: flex;
					align-items: center;
					image{
						width: 14rpx;
						height: 42rpx;
						margin-top: 0;
						margin-right: 10rpx;
					}
				}
			}
		}
		image{
			width: 144rpx;
			height: 144rpx;
			// margin-top: 30rpx;
		}
		.name{
			font-weight: 600;
			font-size: 36rpx;
			color: #3D3D3D;
			margin-bottom: 30rpx;
		}
		.bqliat{
			margin-top: 30rpx;
			display: flex;
			flex-wrap: wrap;
			.bqitem{
				margin-right: 10rpx;
				border: 1px solid #48893B;
				color: #48893B;
				padding: 10rpx;
				margin-top: 10rpx;
			}
			image{
				width: 226rpx;
				height: 58rpx;
			}
		}
	}
	.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;
		.queren{
			width: 314rpx;
			height: 104rpx;
			background: #48893B;
			text-align: center;
			line-height: 104rpx;
			font-weight: 600;
			font-size: 36rpx;
			color: #FFFFFF;
			border-radius: 20rpx;
		}
		.xinjian{
			width: 314rpx;
			height: 104rpx;
			border: 2rpx solid #48893B;
			border-radius: 20rpx;
			text-align: center;
			line-height: 104rpx;
			font-weight: 600;
			font-size: 36rpx;
			color: #48893B;
		}
	}
	.box{
		width: 680rpx;
		max-height: 408rpx;
		background: #FFFFFF;
		margin: auto;
		margin-top: 22rpx;
		border-radius: 20rpx;
		.name{
			display: flex;
			justify-content: space-between;
			align-items: center;
			width: 618rpx;
			margin: auto;
			height: 110rpx;
			border-bottom: 1px solid #D8D8D8;
			.lt{
				font-size: 32rpx;
				color: #3D3D3D;
			}
			.rt{
				display: flex;
				align-items: center;
				input{
					text-align: right;
				}
				image{
					width: 14rpx;
					height: 42rpx;
					margin-left: 10rpx;
				}
			}
		}
	}


</style>