<template>
	<view>
		<view class="buttonbox" v-if="myuser_type">
			<button type="warn" @click="equipBtnControl('reboot')" size="mini">重启</button>
			<button type="warn" @click="equipBtnControl('update')" size="mini">升级</button>
			<button type="warn" @click="equipBtnControl('clearworm')" size="mini">清网</button>
			<button type="warn" @click="equipBtnControl('mqttconf')" size="mini">查询服务器配置</button>
		</view>
		<view class="setitem">
			设备编号：{{option.device_id}}
		</view>
		<view class="setitem">
			设备名称：{{option.device_name}}
		</view>
		<view class="setitem">
			设备版本：{{option.dtype}}
		</view>
		<view class="setitem">
			设备开关：
			<view class="uni-list-cell" style="margin-top: 30rpx;">
				<picker @change="turnChange($event,'ds')" value="equipContrlForm.ds" :range="dsArr">
					<text class="uni-input">{{dsArr[equipContrlForm.ds]}}</text>
					<view class="arrow"></view>
				</picker>
			</view>
		</view>
		<view class="setitem">
			时控时长：
			<view class="selectTime">
				<view class="uni-list-cell time" @click="selectTime('st')">
					<text>{{stLabel}}</text>
					<u-icon name="clock"></u-icon>
				</view>
				<text class="line">-</text>
				<view class="uni-list-cell time" @click="selectTime('et')">
					<text>{{etLabel}}</text>
					<u-icon name="clock"></u-icon>
				</view>
			</view>
			<u-select v-model="stShow" mode="single-column" :list="list" @confirm="stConfirm"></u-select>
			<u-select v-model="etShow" mode="single-column" :list="list" @confirm="etConfirm"></u-select>
		</view>
		<view class="setitem">
			数据频率(min)：
			<slider :value="equipContrlForm.dat_f" show-value="true" :min="10" :max="60"
				@change="sliderChange($event,'dat_f')" block-color="#57C878" activeColor="#57C878" step="1" />
		</view>
		<view class="setitem">
			自清虫定时(min)：
			<slider :value="equipContrlForm.clt_t" show-value="true" :min="10" :max="60"
				@change="sliderChange($event,'clt_t')" block-color="#57C878" activeColor="#57C878" step="1" />
		</view>
		<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: ['关机', '开机'],
				equipContrlForm: {
					ds: 0,
					dat_f: 0,
					clt_t: 0,
					st: "",
					et: "",
					timctrl: ""
				},
				stLabel: "",
				etLabel: "",
				stShow: false,
				etShow: false,
				list: [{
						value: 0,
						label: '00:00'
					},
					{
						value: 1,
						label: '01:00'
					},
					{
						value: 2,
						label: '02:00'
					},
					{
						value: 3,
						label: '03:00'
					},
					{
						value: 4,
						label: '04:00'
					},
					{
						value: 5,
						label: '05:00'
					},
					{
						value: 6,
						label: '06:00'
					},
					{
						value: 7,
						label: '04:00'
					}, {
						value: 8,
						label: '08:00'
					},
					{
						value: 9,
						label: '09:00'
					},
					{
						value: 10,
						label: '10:00'
					},
					{
						value: 11,
						label: '11:00'
					},
					{
						value: 12,
						label: '12:00'
					},
					{
						value: 13,
						label: '13:00'
					},
					{
						value: 14,
						label: '14:00'
					}, {
						value: 15,
						label: '15:00'
					},
					{
						value: 16,
						label: '16:00'
					},
					{
						value: 17,
						label: '17:00'
					},
					{
						value: 18,
						label: '18:00'
					},
					{
						value: 19,
						label: '19:00'
					},
					{
						value: 20,
						label: '20:00'
					},
					{
						value: 21,
						label: '21:00'
					}, {
						value: 22,
						label: '22:00'
					},
					{
						value: 23,
						label: '23:00'
					},
				],
			}
		},
		methods: {
			turnChange(e) {
				this.equipContrlForm.ds = e.target.value
			},
			selectTime(a) {
				if (a == 'st') {
					this.stShow = true;
				} else {
					this.etShow = true;
				}
			},
			stConfirm(e) {
				this.stLabel = e[0].label
				this.equipContrlForm.st = e[0].value
			},
			etConfirm(e) {
				this.etLabel = e[0].label
				this.equipContrlForm.et = e[0].value
			},
			async getconf() {
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.send_control.device_control_info',
					data: {
						cmd: "paramconf",
						d_id: this.option.d_id
					}
				})
				console.log(res)
				this.equipContrlForm.et = res.work_tim.et
				this.equipContrlForm.st = res.work_tim.st
				this.equipContrlForm.timctrl = res.work_tim.timctrl
				this.etLabel = this.list[res.work_tim.et].label
				this.stLabel = this.list[res.work_tim.st].label
				this.equipContrlForm.dat_f = res.data_tim.dat_f
				this.equipContrlForm.clt_t = res.clt_tim.clt_t
				this.equipContrlForm.ds = res.power.ds

			},
			sliderChange(e, a) {
				console.log(e)
				if (a == "dat_f") {
					this.equipContrlForm.dat_f = e.detail.value
				} else {
					this.equipContrlForm.clt_t = e.detail.value
				}
			},
			async submit() {
				var obj = {
					work_tim: {
						timctrl: this.equipContrlForm.timctrl,
						st: this.equipContrlForm.st,
						et: this.equipContrlForm.et
					},
					data_tim: {
						dat_f: this.equipContrlForm.dat_f
					},
					power: {
						ds: this.equipContrlForm.ds
					},
					clt_tim: {
						clt_t: this.equipContrlForm.clt_t
					},
				}
				console.log(obj)
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.send_control.device_control',
					data: {
						device_type_id: 8,
						d_id: this.option.d_id,
						config: JSON.stringify(obj),
					}
				})
				console.log(res)
				if (res == true) {
					uni.showToast({
						title: '指令下发成功！',
						duration: 2000
					});
				} else {
					uni.showToast({
						title: '指令下发失败！',
						duration: 2000,
						icon: "none"
					});
				}
			},
			async equipBtnControl(cmd) {
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.send_control.admin_device_control',
					data: {
						cmd,
						device_type_id: 8,
						d_id: this.option.d_id,
					}
				})
				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;

			.selectTime {
				margin-top: 30rpx;
				display: flex;

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

				.line {
					width: 50rpx;
					text-align: 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>
