<template>
	<view>
		<view class="status_bar"></view>
		<view class="" style="position: relative;top: 44px;">
			<view style="position: fixed;z-index: 100;width: 100%;">
				<uni-nav-bar @clickLeft="clickLeft" left-icon="back" left-text="返回" title="设备详情"></uni-nav-bar>
			</view>
			<view class="info">
				<view class="info_item">
					<image :src="$imageURL+'/bigdata_app/image/cb/onBg.png'" mode="" class="bgi"></image>
					<p style="font-size: 32rpx;" @click="copy(eqinfo.device_id)">设备 ID：{{eqinfo.device_id}}
						<image :src="$imageURL+'/bigdata_app/image/environment/fuzhi.png'" mode=""
							class="tishi"></image>
					</p>
					<p>设备名称：{{eqinfo.device_name||"无"}}</p>
					<p>最近上报时间：<span v-if="eqinfo.uptime">{{eqinfo.uptime|timeFormat()}}</span><span
							v-else>{{eqinfo.addtime|timeFormat()}}</span></p>
					<p>地址：{{city}}</p>
					<p @click="setlongfun('')" v-if="$QueryPermission(250)">
						<text space="emsp">设置步长</text>
						<u-icon name="edit-pen" color="#f0ad4e" size="28"></u-icon>
					</p>
				</view>
			</view>
			<u-modal v-model="modalshow" @confirm="confirm" show-cancel-button>
				<view class="modalbox" v-for="items,index in depthlist">
					<p>第{{ index + 1 }}层步长值:</p>
					<input type="number" v-model="depthlist[index]" class="uinput" placeholder="请输入步长" />
					<p>cm</p>
				</view>
			</u-modal>
			<view class="newdatas">
				<view class="newdatas_title">
					<span>实时数据</span>
					<p @click="tohistory" v-if="$QueryPermission(189)">历史数据>>></p>
				</view>
				<view class="newdatas_land" v-for="item,index in newdataobj.temp" :key="index">
					<p class="newdatas_land_title">{{newdataobj.depth[index]}}cm</p>
					<view class="newdatas_land_info">
						<view class="newdatas_land_box">
							<view class="imgbox">
								<image
									:src="$imageURL+'/bigdata_app/image/environment/tubular/trs'+(index+1)+'.png'"
									mode="" class="img"></image>
							</view>
							<view class="infobox">
								<p>土壤湿度</p>
								<p>{{newdataobj.swc[index]}}%RH</p>
							</view>
						</view>
						<view class="newdatas_land_box">
							<view class="imgbox">
								<image
									:src="$imageURL+'/bigdata_app/image/environment/tubular/trw'+(index+1)+'.png'"
									mode="" class="img"></image>
							</view>
							<view class="infobox">
								<p>土壤温度</p>
								<p>{{newdataobj.temp[index]}}℃</p>
							</view>
						</view>
						<view class="newdatas_land_box" v-if="newdataobj.ecs[index]">
							<view class="imgbox">
								<image
									:src="$imageURL+'/bigdata_app/image/environment/tubular/ddl'+(index+1)+'.png'"
									mode="" class="img"></image>
							</view>
							<view class="infobox">
								<p>电导率</p>
								<p>{{newdataobj.ecs[index]}}ms/cm</p>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				eqinfo: {},
				city: "",
				modalshow: false, //设置步长弹框,
				stepsize: "", //步长值
				newdataobj: {}, //实时数据
				depthlist:[]
			}
		},
		methods: {
			reverseGeocode(lat, lng) {
				uni.request({
					type: "GET",
					url: "https://restapi.amap.com/v3/geocode/regeo?output=JSON&location=" + lng + "," + lat +
						"&key=27273b81090f78759e4057f94474516f&radius=1000&extensions=all",
					dataType: "json",
					complete: ress => {
						console.log(ress)
						// this.city = ress.data.regeocode.formatted_address
						if (ress.data.regeocode.formatted_address.length == 0) {
							this.city = "--"
						} else {
							this.city = ress.data.regeocode.formatted_address
						}
					}
				});
			},
			// weather.weather.nd_topic_change 设置 获取步长
			async setlongfun(stepsize) { //设备列表
				this.modalshow = !this.modalshow
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=weather.weather.nd_topic_change',
					data: {
						device_id: this.eqinfo.device_id,
						depth: stepsize
					}
				})
				// this.stepsize = res.topic
				console.log(res)
				if(stepsize==""){
					this.depthlist = res.depth.split(",")
				}else{
					if(res){
						uni.showToast({
							title: "设置成功",
							icon: "none"
						})
					}else{
						uni.showToast({
							title: "设置失败",
							icon: "none"
						})
					}
					
				}
			},
			//weather.weather.nd_status 实时数据
			async newdatafun() { //设备列表
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=weather.weather.nd_status',
					data: {
						device_id: this.eqinfo.device_id,
					}
				})
				this.newdataobj = res
				this.newdataobj.depth = this.newdataobj.depth.split(",")
				// this.depthlist = JSON.parse(JSON.stringify(this.newdataobj.depth))
				this.newdataobj.temp = this.newdataobj.temp.split(",")
				this.newdataobj.swc = this.newdataobj.swc.split(",")
				this.newdataobj.ecs = this.newdataobj.ecs.split(",")
				console.log(this.newdataobj)
			},
			confirm() {
				this.modalshow = !this.modalshow
				this.setlongfun(this.depthlist)
			},
			clickLeft() {
				// uni.navigateTo({
				// 	url: "./index"
				// })
				uni.navigateBack({
					delta:1
				})
			},
			tohistory() {
				uni.navigateTo({
					url: "./gshistory?id="+ this.eqinfo.device_id
				})
			},
			copy(item) {
				uni.setClipboardData({
					data: item,
					success: function() {
						console.log('success');
					}
				});
			},
		},
		onLoad(option) {
			this.eqinfo = JSON.parse(option.shebei)
			this.reverseGeocode(this.eqinfo.lat, this.eqinfo.lng)
			this.newdatafun()
			console.log(this.eqinfo)
		}
	}
</script>

<style lang="scss">
	.info {
		width: 100%;
		position: absolute;
		top: 44px;

		.info_item {
			width: 90%;
			margin: 0 auto;
			// height: 290rpx;
			padding: 26rpx 50rpx;
			position: relative;
			box-sizing: border-box;

			.bgi {
				width: 100%;
				height: 100%;
				position: absolute;
				top: 0;
				left: 0;
				z-index: -1;
			}

			p {
				font-size: 24rpx;
				color: #FFFFFF;
				margin-bottom: 10rpx;

				.tishi {
					width: 30rpx;
					height: 30rpx;
					margin: 0rpx 0 0 20rpx;
				}
			}
		}
	}

	.modalbox {
		display: flex;
		padding: 20rpx 10rpx;
		font-size: 14px;
		.uinput {
			width: 50%;
			border-radius: 22px;
			background-color: #f4f4f4;
			margin: 0 10px;
			text-indent: 1rem;
			font-size: 28rpx;
		}
	}

	.newdatas {
		width: 100%;
		position: absolute;
		top: 195px;

		.newdatas_title {
			width: 90%;
			margin: 0 auto;
			display: flex;
			justify-content: space-between;
			height: 50rpx;
			line-height: 50rpx;

			span,p {
				font-size: 13px;
			}

			span:first-child {
				font-size: 16px;
				font-weight: 700;
			}
		}

		.newdatas_air {
			width: 90%;
			margin: 0 auto 20rpx;
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;

			.newdatas_air_box {
				width: 48%;
				padding: 20rpx;
				box-sizing: border-box;
				box-shadow: 0 0 10rpx #bcb9ca;
				margin-top: 20rpx;
				display: flex;

				.imgbox {
					width: 60rpx;
					height: 60rpx;
					margin: 16rpx 30rpx 0rpx 20rpx;

					.img {
						width: 60rpx;
						height: 60rpx;
					}
				}

				.infobox {
					font-size: 28rpx;

					p {
						margin-bottom: 10rpx;
					}
				}
			}
		}

		.newdatas_land {
			width: 90%;
			margin: 0 auto 20rpx;
			.newdatas_land_title{
				font-size: 28rpx;
			}
			.newdatas_land_info {
				width: 100%;
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;
				align-items: center;

				.newdatas_land_box {
					width: 32%;
					padding: 10rpx;
					box-sizing: border-box;
					box-shadow: 0 0 10rpx #bcb9ca;
					margin-top: 20rpx;
					display: flex;
					align-items: center;

					.imgbox {
						width: 60rpx;
						height: 60rpx;

						.img {
							width: 60rpx;
							height: 60rpx;
						}
					}

					.infobox {
						font-size: 28rpx;

						p {
							margin-bottom: 10rpx;
						}
					}
				}
			}
		}
	}
</style>
