<template>
	<view>
		<view class="setitem">
			设备编号：{{option.device_id}}
		</view>
		<view class="setitem">
			设备名称：{{option.device_name}}
		</view>
		<view class="setitem" v-for="(item, index) in equipContrlForm">
			<view class="timeBox" @click="seleteTime(item)">
				定时时间：{{item.time ? item.time : '请选择时间'}}
			</view>
			<u-radio-group v-model="item.value">
				<u-radio :name="1">开</u-radio>
				<u-radio :name="0">关</u-radio>
			</u-radio-group>
			<u-button style="margin: 0" type="primary" v-if="index == equipContrlForm.length - 1" size="mini" @click="editForm('add')">增加</u-button>
			<u-button style="margin: 0" type="error" v-if="index < equipContrlForm.length - 1" size="mini" @click="editForm(index)">删除</u-button>
		</view>
		<u-picker mode="time" v-model="timePiker" :default-time="`${item.time}`" :params="params" @confirm="(e) => changeControlTime(e)"></u-picker>
		<view class="submit-box">
			<u-button @click="submit" type="success">确定</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				myuser_type:false,
				option: {},
				dsArr: ['关机', '开机'],
				timePiker: false,
				equipContrlForm: [{ time: '', value: 0 }],
				item:{}, // 当前编辑的对象
				params: {
					year: false,
					month: false,
					day: false,
					hour: true,
					minute: true,
					second: false
				},
			}
		},
		methods: {
			editForm(type) {
			  if (type == 'add') {
				this.equipContrlForm.push({ time: '', value: 0 });
			  } else {
				this.equipContrlForm.splice(type, 1);
			  }
			},
			seleteTime(item) {
				this.item = item;
				this.timePiker = true;
			},
			changeControlTime(e) {
				this.item.time = `${e.hour}:${e.minute}`
				this.timePiker = false;
			},
			async getconf() {
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.address_info.get_set_time',
					data: {
						d_id: this.option.d_id
					}
				})
				this.equipContrlForm = res.length > 0 ? res : [{ time: '', value: 0 }];
				console.log(res)

			},
			async submit() {
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.address_info.set_time',
					data: {
						device_type_id: 29,
						device_id: this.option.device_id,
						data: JSON.stringify(this.equipContrlForm),
					}
				})
				console.log(res)
				if (res == true) {
					uni.showToast({
						title: '指令下发成功！',
						duration: 2000
					});
				} else {
					uni.showToast({
						title: '指令下发失败！',
						duration: 2000,
						icon: "none"
					});
				}
			},
		},
		onLoad(option) {
			uni.getStorage({
				key:"myuser_type",
				success:(res)=>{
					console.log(res.data)
					if(Number(res.data) == 1){
						this.myuser_type = true
					}
				}
			})
			this.option = option
			this.getconf()
			
		}
	}
</script>

<style lang='less'>
	page {
		padding: 20rpx;
		box-sizing: border-box;

		.buttonbox {
			display: flex;
			justify-content: space-between;
		}

		.setitem {
			margin-top: 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.uni-list-cell {
			background: #F7F8FA;
			padding: 10rpx 40rpx;
			font-size: 28rpx;
			box-sizing: border-box;

			/* margin-top: 30rpx; */
			.arrow {
				display: inline-block;
				border-width: 12rpx 8rpx;
				border-style: solid;
				float: right;
				margin-top: 10rpx;
				border-color: #888 transparent transparent transparent;
			}
		}

		.submit-box {
			margin-top: 60rpx
		}
	}
</style>
