<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>
		<view class="setitem">
			震动开关：
			<view class="uni-list-cell" style="margin-top: 30rpx;">
				<picker @change="turnChange($event,'shake')" value="equipContrlForm.shake" :range="shakeArr">
					<text class="uni-input">{{shakeArr[equipContrlForm.shake]}}</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">
			震动时间(s)：
			<slider :value="equipContrlForm.shake_sec" show-value="true" :min="0" :max="300"
				@change="sliderChange($event,'shake_sec')" block-color="#57C878" activeColor="#57C878" step="1" />
		</view>
		<view class="setitem">
			数据上传(min)：
			<slider :value="equipContrlForm.dat_f" show-value="true" :min="5" :max="60"
				@change="sliderChange($event,'dat_f')" block-color="#57C878" activeColor="#57C878" step="5" />
		</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:{},
				equipContrlForm:{
					ds:"",
					st: "",
					et: "",
					shake:"",//震动开关
					shake_sec:"",//震动
					dat_f:"",//数据
				},
				dsArr: ['关机', '开机'],
				shakeArr:["关","开"],
				stShow:false,
				etShow:false,
				etLabel:"",
				stLabel:"",
				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.send_control.device_control_info',
					data: {
						cmd: "paramconf",
						d_id: this.option.d_id
					}
				})
				console.log(res)
				for(var key in this.equipContrlForm){
					this.equipContrlForm[key] = res[key]
				}
				this.etLabel = this.list[this.equipContrlForm.et].label
				this.stLabel = this.list[this.equipContrlForm.st].label
			},
			turnChange(e,a) {
				if (a == 'ds') {
					this.equipContrlForm.ds = e.target.value
				} else {
					this.equipContrlForm.shake = 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
			},
			sliderChange(e, a) {
				console.log(e)
				if (a == "dat_f") {
					this.equipContrlForm.dat_f = e.detail.value
				} else {
					this.equipContrlForm.shake_sec = e.detail.value
				}
			},
			async submit(){
				console.log(this.equipContrlForm)
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.send_control.device_control',
					data: {
						device_type_id: 12,
						d_id: this.option.d_id,
						config: JSON.stringify(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: 12,
						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>
