<template>
	<view>
		<view class="" v-if="myuser_type">
			<view class="tit adminTit">
				操作
			</view>
			<view class="btns">
				<button type="warn" @click="equipBtnControl('update')" size="mini">升级</button>
				<button type="warn" @click="equipBtnControl('reboot')" size="mini">重启</button>
				<button type="warn" @click="equipBtnControl('clearworm')" size="mini">清网</button>
			</view>
			<view class="tit adminTit">
				设备开关
			</view>
			<view class="uni-list-cell" @click="cell">
				<text class="uni-input">{{equipContrlForm1.ds=="0"?'关机':'开机'}}</text>
				<view class="arrow"></view>
				<u-select v-model="ds_show" mode="single-column" :list="ds_list" @confirm="confirm($event,'ds')"></u-select>
			</view>
			<view class="tit adminTit">
				时控开关
			</view>
			<view class="uni-list-cell" @click="shicell">
				<text class="uni-input">{{equipContrlForm1.timctrl=="0"?'关':'开'}}</text>
				<view class="arrow"></view>
				<u-select v-model="timctrl_show" mode="single-column" :list="timctrl_list" @confirm="confirm($event,'timctrl')"></u-select>
			</view>
		</view>
		<view class="" v-if="Number(equipContrlForm1.timctrl)">
			<view class="tit">
				时控时长
			</view>
			<view class="selectTime">
				<view class="uni-list-cell time" @click="selectTime('st')">
					<text>{{time.time1||'开始时间'}}</text>
					<u-icon name="clock"></u-icon>
				</view>
				<text class="line">-</text>
				<view class="uni-list-cell time" @click="selectTime('et')">
					<text>{{time.time2||'结束时间'}}</text>
					<u-icon name="clock"></u-icon>
				</view>
				<u-select v-model="timeShow" mode="single-column" :list="timeList" @confirm="timeConfirm($event,timeType)"></u-select>
			</view>
		</view>
		<view class="tit">
			数据频率(min)
		</view>
		<view class="">
			<slider :value="equipContrlForm1.dat_f" show-value="true" :min="10" :max="60" @change="sliderChange" block-color="#57C878"
			 activeColor="#57C878" step="1" />
		</view>
		<view class="submit-box">
			<u-button @click="submit" type="success">确定</u-button>
		</view>
		<u-toast ref="toast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				d_id: '',
				equipContrlForm1: {
					st: '', //时控开始时间
					et: '', //时控结束时间
					dat_f: 10, //数据上传时间间隔
					ds: '', //开关，1开机，0关机
					timctrl: '' //时控开关 1开，0关
				},
				time: {
					time1: '',
					time2: ''
				},
				ds_list: [{
					value: 0,
					label: '关机'
				}, {
					value: 1,
					label: '开机'
				}],
				ds_show: false,
				timctrl_list: [{
					value: 0,
					label: '关'
				}, {
					value: 1,
					label: '开'
				}],
				timctrl_show: false,
				timeList: [],
				timeShow: false,
				timeType: '',
				myuser_type: false
			}
		},
		onLoad(option) {
			this.d_id = option.d_id
			this.equipSet()
			uni.getStorage({
				key: "myuser_type",
				success: (res) => {
					if (Number(res.data) == 1) {
						this.myuser_type = true
					}
				}
			})
		},
		methods: {
			//回显设备控制参数
			async equipSet() {
				let res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.send_control.device_control_info',
					data: {
						d_id: this.d_id,
						cmd: "paramconf"
					}
				})
				console.log(res)
				let obj = {
					st: res.work_tim.st,
					et: res.work_tim.et,
					timctrl: res.work_tim.timctrl,
					ds: res.power.ds,
					dat_f: res.data_tim.dat_f,
				};
				this.equipContrlForm1 = obj
				this.time.time1 = obj.st && obj.st < 10 ? "0" + obj.st + ":00" : obj.st + ":00";
				this.time.time2 = obj.et && obj.et < 10 ? "0" + obj.et + ":00" : obj.et + ":00";
			},
			async equipBtnControl(cmd) {
				let res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.send_control.admin_device_control',
					data: {
						cmd,
						device_type_id: 4,
						d_id: this.d_id
					}
				})
				if (res) {
					this.$refs.toast.show({
						title: '指令下发成功!',
						type: 'success',
					})
				}
			},
			sliderChange(e) {
				this.equipContrlForm1.dat_f = e.detail.value
			},
			selectTime(a) {
				this.timeType = a
				if (this.timeList.length == 0) {
					let arr = []
					for (let i = 0; i < 24; i++) {
						let label = i < 10 ? `0${i}:00` : `${i}:00`
						arr.push({
							value: i,
							label
						})
					}
					arr.unshift({
						value: '',
						label: '重置'
					})
					this.timeList = arr
				}
				this.timeShow = true
			},
			confirm(e, a) {
				console.log(e)
				if (a == 'ds') {
					this.equipContrlForm1.ds = e[0].value
				} else if (a == 'timctrl') {
					this.equipContrlForm1.timctrl = e[0].value
				}
			},
			timeConfirm(e, a) {
				if (a == 'st') {
					this.time.time1 = e[0].label
					this.equipContrlForm1.st = e[0].value
				} else {
					this.time.time2 = e[0].label
					this.equipContrlForm1.et = e[0].value
				}
			},
			cell() { //设备开关
				this.ds_show = true
			},
			shicell() {
				this.timctrl_show=true
			},
			async submit() {
				console.log(this.equipContrlForm1)
				let obj = {
					work_tim: {
						timctrl: this.equipContrlForm1.timctrl,
						st: this.equipContrlForm1.st,
						et: this.equipContrlForm1.et,
					},
					data_tim: {
						dat_f: this.equipContrlForm1.dat_f
					},
					power: {
						ds: this.equipContrlForm1.ds
					},
				};
				let res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.send_control.device_control',
					data: {
						device_type_id: 4,
						d_id: this.d_id,
						config: JSON.stringify(obj),
					}
				})
				if(res === true){
					this.$refs.toast.show({
						title: '指令下发成功',
						type: 'success',
						callback:function(){
							uni.navigateBack({
								delta: 1
							});	
						}
					})
				} else {
					this.$refs.toast.show({
						title: '服务出错了',
						type: 'error'
					})
				}
				// if (res) {
				// 	this.$refs.toast.show({
				// 		title: '指令下发成功',
				// 		type: 'success',
				// 		callback: function() {
				// 			uni.navigateBack({
				// 				delta: 1
				// 			});
				// 		}
				// 	})

				// }
			}
		}
	}
</script>

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

		.tit {
			line-height: 30rpx;
			font-size: 30rpx;
			padding-left: 20rpx;
			border-left-width: 2px;
			border-left-style: solid;
			border-left-color: $uni-color-success;
			margin: 30rpx 0;
		}

		.adminTit {
			border-left-color: #e64340;
		}

		.btns {
			display: flex;
			justify-content: flex-start;
			flex-wrap: no-wrap;

			button {
				margin: 0;
				margin-right: 10rpx;
				padding: 0 25rpx;
			}
		}

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

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

		.selectTime {
			display: flex;
			margin-bottom: 20rpx;

			.time {
				width: 350rpx;
				display: flex;
				justify-content: space-between;
			}

			.line {
				width: 50rpx;
				text-align: center;
			}
		}

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