<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('takephoto')" size="mini">立即拍照</button>
				 <button type="warn" @click="equipBtnControl('autotakephoto')" size="mini">对焦拍照</button>
				 <button type="warn" @click="equipBtnControl('turn')" size="mini">转仓</button>
			</view>
			<view class="tit adminTit">
				设备开关
			</view>
			<view class="uni-list-cell"  @click="onoff">
				<text class="uni-input">{{on_off=="0"?'关闭':'开机'}}</text>
				<view class="arrow"></view>
				<u-select v-model="on_off_show" mode="single-column" :list="on_off_list" @confirm="confirm($event,'on_off')"></u-select>
			</view>
		</view>
		<view class="tit">
			载玻片滴液时间
		</view>
		<view class="uni-list-cell" @click="selectFun('drop_time')">
			<text class="uni-input">{{setFrom.drop_time}}</text>
			<view class="arrow"></view>
			<u-select v-model="drop_time_show" mode="single-column" :list="drop_time_List" @confirm="confirm($event,'drop')"></u-select>
		</view>
		<view class="tit">
			孢子培养时间(h)
		</view>
		<view class="">
			<slider :value="setFrom.cul_time" show-value="true" :min="1" :max="24" @change="sliderChange($event,'cul_time')"  block-color="#57C878" activeColor="#57C878" step="1" />
		</view>
		<view class="tit">
			保温仓设定温度(℃)
		</view>
		<view class="">
			<slider :value="setFrom.set_temp" show-value="true" :min="10" :max="40" @change="sliderChange($event,'set_temp')"  block-color="#57C878" activeColor="#57C878" step="1" />
		</view>
		<view class="tit">
			数据上传时间间隔(min)
		</view>
		<view class="">
			<slider :value="setFrom.datt" show-value="true" :min="10" :max="60" @change="sliderChange($event,'datt')"  block-color="#57C878" activeColor="#57C878" step="1" />
		</view>
		<view class="tit">
			采集开启和关闭时间
		</view>
		<view class="selectTime">
			<view class="uni-list-cell time" @click="selectTime('st1')">
				<text>{{coll_time.time01||'开始时间'}}</text>
				<u-icon name="clock"></u-icon>
			</view>
			<text class="line">-</text>
			<view class="uni-list-cell time" @click="selectTime('et1')">
				<text>{{coll_time.time02||'结束时间'}}</text>
				<u-icon name="clock"></u-icon>
			</view>
		</view>
		<view class="selectTime">
			<view class="uni-list-cell time" @click="selectTime('st2')">
				<text>{{coll_time.time03||'开始时间'}}</text>
				<u-icon name="clock"></u-icon>
			</view>
			<text class="line">-</text>
			<view class="uni-list-cell time" @click="selectTime('et2')">
				<text>{{coll_time.time04||'结束时间'}}</text>
				<u-icon name="clock"></u-icon>
			</view>
		</view>
		<view class="selectTime">
			<view class="uni-list-cell time" @click="selectTime('st3')">
				<text>{{coll_time.time05||'开始时间'}}</text>
				<u-icon name="clock"></u-icon>
			</view>
			<text class="line">-</text>
			<view class="uni-list-cell time" @click="selectTime('et3')">
				<text>{{coll_time.time06||'结束时间'}}</text>
				<u-icon name="clock"></u-icon>
			</view>
		</view>
		<view class="submit-box">
			<u-button  @click="submit" type="success">确定</u-button>
		</view>
		<u-select v-model="collShow" mode="single-column" :list="timeList" @confirm="collConfirm($event,timeType)"></u-select>
		<u-toast ref="toast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				d_id:'',
				on_off_show:false,
				on_off:null,//设备开关
				on_off_list:[{
					value:0,
					label:'关闭'
				},{
					value:1,
					label:'开机'
				}],
				drop_time_show:false,
				drop_time_List:[],
			    setFrom: {
					drop_time: "1", //载玻片滴液时间
					cul_time: 1, //孢子培养时间
					set_temp: 10, //保温仓设定温度
					datt: 10, //数据上传时间间隔(h)-m
					coll_time: [], //采集开启和关闭时间
				 },
				coll_time: {
			        time01: "",
			        time02: "",
			        time03: "",
			        time04: "",
			        time05: "",
			        time06: "",
					value01: "",
					value02: "",
					value03: "",
					value04: "",
					value05: "",
					value06: "",
			      },
				timeList:[] ,
				collShow:false,
				timeType:'',
				myuser_type:false
			}
		},
		onLoad(option){
			this.d_id=option.d_id
			this.equipOperation()
			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" 
					}
				})
				this.setFrom.drop_time=res.drop_time 
				this.setFrom.cul_time=res.cul_time
				this.setFrom.set_temp=res.set_temp
				this.setFrom.datt=res.datt
				let coll_time=res.coll_time 
				for (let i in coll_time) {
					if (i == 0) {
					  let arr = coll_time[i].split("-");
					  this.coll_time.time01 =
						Number(arr[0]) < 10 ? "0" + arr[0] + ":00" : arr[0] + ":00";
					  this.coll_time.time02 =
						Number(arr[1]) < 10 ? "0" + arr[1] + ":00" : arr[1] + ":00";
					  this.coll_time.value01=Number(arr[0])
					  this.coll_time.value02=Number(arr[1])
					} else if (i == 1) {
					  let arr = coll_time[i].split("-");
					  this.coll_time.time03 =
						Number(arr[0]) < 10 ? "0" + arr[0] + ":00" : arr[0] + ":00";
					  this.coll_time.time04 =
						Number(arr[1]) < 10 ? "0" + arr[1] + ":00" : arr[1] + ":00";
						this.coll_time.value03=Number(arr[0])
						this.coll_time.value04=Number(arr[1])
					} else if (i == 2) {
					  let arr = coll_time[i].split("-");
					  this.coll_time.time05 =
						Number(arr[0]) < 10 ? "0" + arr[0] + ":00" : arr[0] + ":00";
					  this.coll_time.time06 =
						Number(arr[1]) < 10 ? "0" + arr[1] + ":00" : arr[1] + ":00";
						this.coll_time.value05=Number(arr[0])
						this.coll_time.value06=Number(arr[1])
					}
				 }
			console.log(this.setFrom)	
			},
			
			
			//回显设备开关状态
			async equipOperation(){
				let res=await this.$myRequest({
					url:'/api/api_gateway?method=forecast.worm_lamp.bzy_device',
					data:{
						d_id: this.d_id
					}
				})
				this.on_off=res.on_off
			},
			//管理员操作
			async equipBtnControl(cmd){
					let res=await this.$myRequest({
						url:'/api/api_gateway?method=forecast.send_control.admin_device_control',
						data:{
							cmd,
							device_type_id: 7,
							d_id: this.d_id
						}
					})
					if(res){
						this.$refs.toast.show({
							title: '指令下发成功!',
							type: 'success',
						})
					}
			},
			onoff(){
					this.on_off_show=true
			},
			confirm(e,a){
				switch(a){
					case "on_off":
						if(e[0].value){
							 this.equipControl("poweron",e[0].value);
						}else{
							this.equipControl("poweroff",e[0].value);
						}
						break;
					case "drop":
						this.setFrom.drop_time=e[0].value;
						break;
				}
			},
			async equipControl(cmd,val){
				let res=await this.$myRequest({
					url:'/api/api_gateway?method=forecast.send_control.admin_device_control',
					data:{
						cmd,
						device_type_id: 7,
						d_id: this.d_id
					}
				})
				this.on_off=val;
			},
			selectFun(a){
				switch(a){
					case 'drop_time':
						for(let i=1;i<31;i++){
							this.drop_time_List.push({
								value:i,
								label:i
							})
						}
						this.drop_time_show=true;
				}
			},
			sliderChange(e,a){
				switch(a){
					case 'cul_time':
					  this.setFrom.cul_time=e.detail.value;
					  break;
					case 'set_temp':
					   this.setFrom.set_temp=e.detail.value;
					   break;
					case 'datt':
					  this.setFrom.datt=e.detail.value;
					  break;
				}
			},
			selectTime(a){
				this.timeType=a
				let arr=[]
				if(a=='st1'){
					for(let i=1;i<25;i++){
						let label=i<10?`0${i}:00`:`${i}:00`
						arr.push({
							value:i,
							label
						})
					}
					this.timeList=arr
				}else if(a=='et1'){
					for(let i=this.coll_time.value01+1;i<25;i++){
						let label=i<10?`0${i}:00`:`${i}:00`
						arr.push({
							value:i,
							label
						})
					}
					this.timeList=arr
				}else if(a=='st2'){
					for(let i=this.coll_time.value02+1;i<25;i++){
						let label=i<10?`0${i}:00`:`${i}:00`
						arr.push({
							value:i,
							label
						})
					}
					this.timeList=arr
				}else if(a=='et2'){
					for(let i=this.coll_time.value03+1;i<25;i++){
						let label=i<10?`0${i}:00`:`${i}:00`
						arr.push({
							value:i,
							label
						})
					}
					this.timeList=arr
				}else if(a=='st3'){
					for(let i=this.coll_time.value04+1;i<25;i++){
						let label=i<10?`0${i}:00`:`${i}:00`
						arr.push({
							value:i,
							label
						})
					}
					this.timeList=arr
				}else if(a=='et3'){
					for(let i=this.coll_time.value05+1;i<25;i++){
						let label=i<10?`0${i}:00`:`${i}:00`
						arr.push({
							value:i,
							label
						})
					}
					this.timeList=arr
				}
				this.collShow=true
			},
			collConfirm(e,a){
				if(a=='st1'){
					this.coll_time.time01=e[0].label
					this.coll_time.value01=e[0].value
				}else if(a=='et1'){
					this.coll_time.time02=e[0].label
					this.coll_time.value02=e[0].value
				}else if(a=='st2'){
					this.coll_time.time03=e[0].label
					this.coll_time.value03=e[0].value
				}else if(a=='et2'){
					this.coll_time.time04=e[0].label
					this.coll_time.value04=e[0].value
				}else if(a=='st3'){
					this.coll_time.time05=e[0].label
					this.coll_time.value05=e[0].value
				}else if(a=='et3'){
					this.coll_time.time06=e[0].label
					this.coll_time.value06=e[0].value
				}
			},
			async submit(){
				let arr=[]
				if(this.coll_time.value01&&this.coll_time.value02){
					arr.push(`${this.coll_time.value01}-${this.coll_time.value02}`)
				}
				if(this.coll_time.value03&&this.coll_time.value04){
					arr.push(`${this.coll_time.value03}-${this.coll_time.value04}`)
				}
				if(this.coll_time.value05&&this.coll_time.value06){
					arr.push(`${this.coll_time.value05}-${this.coll_time.value06}`)
				}
				this.setFrom.coll_time=arr
				console.log(this.setFrom)
				let res=await this.$myRequest({
					url:'/api/api_gateway?method=forecast.send_control.device_control',
					data:{
						device_type_id: 7,
						d_id: this.d_id,
						config: JSON.stringify(this.setFrom),
					}
				})
				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;
		box-sizing: border-box;
		.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>
