<template>
	<view class="page">
		<u-navbar :title="tit" :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="roomName" placeholder="请输入名称"/> <image src="https://api.ccttiot.com/smartmeter/img/static/uCta8cp7uXd1lGjZS6sc" mode=""></image>
       	  		</view> 
       	  	</view>
       	  	<view class="name" style="border: none;" v-if="type2 == 1">
       	  		<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="name" style="border: none;" v-if="type2 == 2">
				<view class="lt">
					设施类型
				</view>
				<view class="rt" @click="shows = 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 class="" style="margin-top: 10rpx;">
					<text style="margin-right: 20rpx;color: #48893B;" v-for="(item,index) in namelist" :key="index">{{item}}</text>
				</view>
       	  	</view>
       	  </view>
       	  
       	  <u-select v-model="shows" :list="lists" @confirm="confirms"></u-select>
       	  <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,
				shows:false,
				checked:false,
				list:[
					{
						value: '2',
						label: '棋牌'
					},
					{
						value: '1',
						label: '茶室'
					},
					{
						value: '3',
						label: '台球'
					},
				],
				lists:[
					{
						value: '1',
						label: '麻将桌'
					},
					{
						value: '2',
						label: '台球桌'
					}
				],
				type:'',
				biaoqianlist:[],
				selectedIndices:[],
				token:'',
				url:'',
				imgurl:'',
				namelist:[],
				ruleIdlist:[],
				roomName:'',
				storeId:'',
				tit:'',
				type2:'',
				equType:''
			}
		},
		onLoad(option) {
            this.storeId = option.storeId
			if(option.dating){
				this.tit = '新建设施'
				this.type2 = 2
			}else{
				this.tit = '新建房间'
				this.type2 = 1
			}
		},
		onShow() {
			this.getbiaoqian()
			this.getQiniuToken()
			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: {
			// 点击选择收费
			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
			},
			// 确认选择房间类型
			confirms(e){
				this.equType = e[0].value
				this.leixing =e[0].label
			},
			// 点击确定新建房间或者设施
			btnmd(){
				let data = {
					roomName:this.roomName,
					storeId:this.storeId,
					type:this.type,
					picture:this.imgurl,
					ruleIds:this.ruleIdlist,
					tags:this.selectedIndices,
					type2:this.type2,
					equType:this.equType
				}
				this.$u.post(`/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(){
				uni.navigateBack()
			}
			
		}
	}
</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;
						border-radius: 20rpx;
					}
				}
			}
		}
		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>