<template>
	<view class="ability-box">
		<view class="attr-list">
			<view class="attr-list-item" :class="item.ioRequired?'required':''" v-for="(item,index) in dataList" :key="index" v-if="item.accessMode !=='r'">
				<view class="checkbox-box">
					<u-checkbox 
						size="40"
						v-model="item.checked"
						@change="changeCheck($event,index)"
					></u-checkbox>
				</view>
				<view class="attr-list-item-content">
					<!-- 选择器 -->
					<iots-select 
					 v-if="item.formType == 'select'"
					 :name="item.name"
					 :disabled="false"
					 v-model="item.value"
					 :selectList="item.ioObj.enums"
					 @change="changeData($event,index)"
					 :logShow="false"
					></iots-select>
					<!-- 步进器 -->
					<iots-number-box 
					 v-else-if="item.formType == 'number'"
					 :name="item.name"
					 :disabled="false"
					 :min="parseFloat(item.ioObj.IoDeployForm.min || 0)"
					 :max="parseFloat(item.ioObj.IoDeployForm.max || 100)"
					 :step="parseFloat(item.ioObj.IoDeployForm.step || 1)"
					 :integer="item.integer=='int'"
					 :unit="item.ioObj.unit"
					 :logShow="false"
					 @change="changeData($event,index)"
					 :value="item.value"
					></iots-number-box>
					
					<!-- 开关 -->
					<iots-switch
					 v-else-if="item.formType == 'switch'"
					 :name="item.name"
					 :disabled="false"
					 v-model="item.value"
					 @change="changeData($event,index)"
					 :logShow="false"
					></iots-switch>
					<!-- 滑块 -->
					<iots-slider 
					 v-else-if="item.formType == 'slider'"
					 :name="item.name"
					 :disabled="false"
					 :min="parseFloat(item.ioObj.IoDeployForm.min || 0)"
					 :max="parseFloat(item.ioObj.IoDeployForm.max || 100)"
					 :step="parseFloat(item.ioObj.IoDeployForm.step || 1)"
					 :unit="item.ioObj.unit"
					 v-model="item.value"
					 @change="changeData($event,index)"
					 :logShow="false"
					></iots-slider>
					<!-- 时间选择器 -->
					<iots-select-time
					 v-else-if="item.formType == 'time_picker'"
					 :name="item.name"
					 :disabled="false"
					 v-model="item.value"
					 @change="changeData($event,index)"
					 :logShow="false"
					></iots-select-time>
					<!-- 输入框 -->
					<iots-input
					 v-else
					 :name="item.name"
					 :disabled="false"
					 v-model="item.value"
					 :unit="item.ioObj.unit"
					 @change="changeData($event,index)"
					 :logShow="false"
					></iots-input>
				</view>
			</view>
		</view>
		<view class="btn-box">
			<u-checkbox
				size="40"
				v-model="isSelectAll"
				@change="changeCheckAll"
			>全选</u-checkbox>
			<button @click="execute">执行</button>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import IotsSelect from '@/components/iot-components/select/iots-select.vue'
	import IotsNumberBox from "@/components/iot-components/number-box/iots-number-box.vue"
	import IotsSwitch from '@/components/iot-components/switch/iots-switch.vue'
	import IotsSlider from '@/components/iot-components/slider/iots-slider.vue'
	import IotsInput from '@/components/iot-components/input/iots-input.vue'
	import IotsSelectTime from '@/components/iot-components/select-time/iots-select-time.vue'
	export default{
		data(){
			return {
				abilityObj:{},
				dataList:[],
				devId:'',
				pk:'',
				isSelectAll:true,
			}
		},
		components: {
			IotsSelect,
			IotsSelectTime,
			IotsNumberBox,
			IotsSwitch,
			IotsSlider,
			IotsInput,
		},
		onLoad(option) {
			console.log("option",option)
			let dataList = JSON.parse(decodeURIComponent(option.dataList).replace(/%25/g, '%'))
			console.log("当前参数",dataList);
			// this.abilityObj = ability;
			this.devId = option.devId;
			this.pk = option.pk;
			// this.dataList = ability.ioObj.in;
			this.formatData(dataList);
			uni.setNavigationBarTitle({
				title:option.devName+'设备控制'
			})
		},
		methods:{
			formatData(list){
				let datalist = list.map(item=>{
					item.checked = true;
					return item;
				})
				this.dataList = datalist;
			},
			changeCheck(e,index){
				let dataList = JSON.parse(JSON.stringify(this.dataList));
				dataList[index].checked = e.value;
				let isSelectAll = true;
				dataList.forEach((item)=>{
					if(!item.checked){
						isSelectAll = false;
					}
				})
				this.isSelectAll = isSelectAll;
			},
			changeCheckAll(e){
				console.log("全选",e)
				let dataList = JSON.parse(JSON.stringify(this.dataList));
				dataList = dataList.map((item)=>{
					item.checked = e.value;
					return item;
				})
				this.$nextTick(()=>{
					this.$set(this,'dataList',dataList)
					this.$forceUpdate()
				})
			},
			changeData(){
				this.$forceUpdate()
			},
			execute(){
				if(this.verification()){
					let obj = {};
					console.log("dataList",this.dataList)
					this.dataList.forEach((item)=>{
						if(item.checked){
							obj[item.identifier]=item.value
						}
					})
					let params = {
						ac:'write',
						d:this.devId,
						pk:this.pk,
						p:obj
					}
					this.$api.iotsApi.downDeviceAbility(params).then(res => {
						console.log("下发数据成功",res)
						if(res.code == 0){
							// this.$u.toast('执行成功')
							this.$refs.uToast.show({
								title: '执行成功',
								type: 'success',
							})
						}else{
							this.$refs.uToast.show({
								title: res.message,
								type: 'error',
							})
						}
					}, error => {
					})
				}else{
					console.log("校验未通过")
				}
			},
			// 校验表单
			verification(){
				let flag = true;
				this.dataList.forEach((item)=>{
					if(flag && item.checked && !item.value && !['number', 'slider', 'switch'].includes(item.formType)){
						this.$refs.uToast.show({
							title: item.name + '不能为空!',
							type: 'error',
						})
						flag = false;
					}
				})
				return flag;
			}
		}
	}
</script>
<style>
	page{
		background: #f5f5f5;
	}
</style>

<style lang="scss" scoped>
	/deep/.iots-component-box{
		margin-top: 0 !important;
	}
	.ability-box{
		padding-bottom: 140rpx;
	}
	.attr-list-item{
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 20rpx;
		background: #FFFFFF;
		border-radius: 10rpx;
		margin-top: 20rpx;
		.checkbox-box{
			padding: 0 20rpx;
			width: 80rpx;
			display: flex;
			justify-content: center;
			// margin-right: 20rpx;
		}
		.attr-list-item-content{
			flex: 1;
			position: relative;
			&.required::after{
				content: '';
				width: 16rpx;
				height: 16rpx;
				border-radius: 50%;
				position: absolute;
				top: 12rpx;
				left: 26rpx;
				background: red;
			}
		}
	}
	.btn-box{
		display: flex;
		align-items: center;
		justify-content: space-between;
		background: #fff;
		height: 140rpx;
		padding: 0 30rpx;
		padding-bottom: 10rpx;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: 99;
		button{
			width: 300rpx;
			height: 90rpx;
			background: #3683FD;
			border-radius: 43rpx;
			font-size: 32rpx;
			color: #FFFFFF;
			display: flex;
			justify-content: center;
			align-items: center;
			margin: 0;
		}
	}
</style>