<template>
	<view>
		<view class="" v-if="myuser_type">
			<view class="tit adminTit">
				联网模块
			</view>
			<view class="btns">
				<button type="warn" @click="equipBtnControl('dtu_update')" size="mini">升级</button>
				<button type="warn" @click="equipBtnControl('dtu_reboot')" size="mini">重启</button>
				<!-- <button type="warn" @click="equipBtnControl('yfwlw')" size="mini">平台切换</button> -->
			</view>
			<view class="tit adminTit">
				强制操作
			</view>
			<view class="btns">
				<button type="warn" @click="equipBtnControl('takephoto')" size="mini">拍照</button>
				<button type="warn" @click="equipBtnControl('update')" size="mini">升级</button>
				<button type="warn" @click="equipBtnControl('reboot')" size="mini">重启</button>
				<!-- <button type="warn" @click="equipBtnControl('close_shake')" size="mini">震动关闭</button>
				<button type="warn" @click="equipBtnControl('open_shake')" size="mini">震动开启</button> -->
			</view>
		</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> -->
		<u-form :model="equipContrlForm" :label-width="180">
			<u-form-item label="温控开关" :label-width="240">
				<u-radio-group v-model="equipContrlForm.limit_temp">
					<u-radio :name="1">开</u-radio>
					<u-radio :name="0">关</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="雨控开关" :label-width="240">
				<u-radio-group v-model="equipContrlForm.limit_rain">
					<u-radio :name="1">开</u-radio>
					<u-radio :name="0">关</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="定时模式" :label-width="240">
				<u-radio-group v-model="equipContrlForm.ts">
					<u-radio :name="1">光控</u-radio>
					<u-radio :name="0">时控</u-radio>
					<u-radio :name="2">晨曦</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="定时时长" :label-width="240" v-if="equipContrlForm.ts == 0">
				<text @click="equipContrlForm.show_st= true">{{equipContrlForm.collstart}}</text>-
				<text @click="equipContrlForm.show_et= true">{{equipContrlForm.collend}}</text>
				<u-picker mode="time" v-model="equipContrlForm.show_st" :default-time="`${equipContrlForm.collstart}:00`"
					:params="params" @confirm="(e) => changeControlTime(e, 'collstart')"></u-picker>
				<u-picker mode="time" v-model="equipContrlForm.show_et" :default-time="`${equipContrlForm.collend}:00`"
					:params="params" @confirm="(e) => changeControlTime(e, 'collend')"></u-picker>
			</u-form-item>
			<u-form-item label="定时时长(H)" :label-width="200" v-if="equipContrlForm.ts == 1">
				<u-input v-model="equipContrlForm.tt" type="number" />
			</u-form-item>
			<u-form-item label="拍照间隔(min)" :label-width="200">
				<u-input v-model="equipContrlForm.pho_f" type="number" />
			</u-form-item>
			<u-form-item label="高温保护阈值(℃)" :label-width="250">
				<u-input v-model="equipContrlForm.tph" type="number" />
			</u-form-item>
			<u-form-item label="低温保护阈值(℃)" :label-width="250">
				<u-input v-model="equipContrlForm.tpl" type="number" />
			</u-form-item>
			<u-form-item label="数据上传间隔(min)" :label-width="260">
				<u-input v-model="equipContrlForm.dat_f" type="number" />
			</u-form-item>
			<u-form-item label="风量" :label-width="260">
				<u-input v-model="equipContrlForm.fan_speed" type="number" />
			</u-form-item>
		</u-form>
		<view class="submit-box">
			<u-button @click="submit" type="success">确定</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				myuser_type: false,
				option: {},
				equipContrlForm: {
					"tpl ": "",
					"tph": "",
					"dat_f": "",
					"pho_f": "",
					"limit_temp": "",
					"limit_rain": "",
					"tt": "",
					"ts": "",
					"collstart": "",
					"collend": "",
					fan_speed:'',
					show_st: false,
					show_et: false,
				},
				params: {
					year: false,
					month: false,
					day: false,
					hour: true,
					minute: false,
					second: 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: {
			async getconf() {
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.haomi.haomi_get_config',
					data: {
						cmd: "paramconf",
						d_id: this.option.d_id
					}
				})
				console.log(res)
				if (res && Object.keys(res).length > 0) {
					let data = res;
					console.log(data);
					let newObj = {
						...data,
						show_st: false,
						show_et: false,
					};
					newObj.collstart = newObj.collstart > 9 ? newObj.collstart : `0${newObj.collstart}`;
					newObj.collend = newObj.collend > 9 ? newObj.collend : `0${newObj.collend}`;
					console.log(newObj);
					this.equipContrlForm = newObj;
				}
			},
			changeControlTime(e, type) {
				console.log(e);
				this.equipContrlForm[type] = `${e.hour}`;
				console.log(this.equipContrlForm);
			},
			async submit() {
				if(this.equipContrlForm.fan_speed < 10) {
					uni.showToast({
						title:'风量值不能小于10',
						icon:'none'
					})
					return
				}
				console.log(this.equipContrlForm)
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.haomi.haomi_config',
					data: {
						d_id: this.option.d_id,
						device_id: this.option.device_id,
						...this.equipContrlForm
					}
				})
				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: 28,
						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) {
			this.option = option
			uni.getStorage({
				key: "myuser_type",
				success: (res) => {
					console.log(res.data)
					if (Number(res.data) == 1) {
						this.myuser_type = true
					}
				}
			})
			this.getconf()
		}
	}
</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;
			}
		}

		.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>