<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="eqinfo.item.is_online==1?$imageURL+'/bigdata_app/image/cb/onBg.png':$imageURL+'/bigdata_app/image/cb/offBg.png'"
						mode="" class="bgi"></image> -->
					<image :src="$imageURL+'/bigdata_app/image/cb/onBg.png'" mode="" class="bgi"></image>
					<p style="font-size: 32rpx;" @click="copy(eqinfo.item.imei || eqinfo.item.device_id)">设备
						ID：{{eqinfo.item.imei || eqinfo.item.device_id}}
						<image :src="$imageURL+'/bigdata_app/image/environment/fuzhi.png'" mode=""
							class="tishi"></image>
					</p>
					<p>设备名称：{{eqinfo.item.device_name}}</p>
					<p>设备型号：水肥一体化</p>
					<p>最近上报时间：{{eqinfo.item.uptime|timeFormat()}}</p>
					<p>设备地址：{{city}}</p>
				</view>
			</view>
			<view class="control">
				<view class="control_item" v-if="$QueryPermission(196)" @click="control">
					<image :src="$imageURL+'/bigdata_app'+'/image/environment/8.png'" mode=""></image>
					<p>设备状态</p>
				</view>
				<view class="control_item" v-if="$QueryPermission(197)" @click="charts">
					<image :src="$imageURL+'/bigdata_app'+'/image/environment/10.png'" mode=""></image>
					<p>要素数据</p>
				</view>
				<view class="control_item" @click="repairs">
					<image :src="$imageURL+'/bigdata_app'+'/image/environment/7.png'" mode=""></image>
					<p>一键报修</p>
				</view>
			</view>
			<view class="realtime">
				<p class="realtime_title">实时数据</p>
				<view class="realtime_tishi" v-if="olddatas.length==0 && dataloadingtf == false">
					暂无数据
				</view>
				<view class="realtime_text" v-else>
					<view class="realtime_text_item">
						<view class="realtime_text_item_info" v-for="item,index in olddatas" :key="index">
							<view class="item_info_img">
								<image :src="$imageURL+'/bigdata_pc/sf/dev/icon_'+item.icon+'.png'"
									mode=""></image>
							</view>
							<view class="item_info_text">
								<p>{{item.txt}}</p>
								<p style="margin-top: 20rpx;">{{item.value}}</p>
							</view>
						</view>
					</view>
				</view>
				<view class="realtime_tishi" v-if="dataloadingtf">
					<p class="dataloading">加载中</p>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import elelist from "../../static/json/eleList.json"
	export default {
		data() {
			return {
				eqinfo: {
					item:{}
				},
				olddata: {},
				olddatas: [],
				newdatesTF: false, //暂无数据显示隐藏
				work: "",
				zttf: false,
				ystf: false,
				city: "",
				dataloadingtf: true, //加载中显示隐藏
			}
		},
		methods: {
			async history() { //设备列表
				var newtime = +new Date() / 1000
				var oldtime = newtime - 24 * 60 * 60
				this.dataloadingtf = true
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=weather.weather.sf_status',
					data: {
						device_id: this.eqinfo.item.device_id
					}
				})
				this.dataloadingtf = false
				console.log(res)
				this.olddatas = []
				var dataarr = res.conf.eleName.split("/")
				var elearr = res.conf.eleNum.split("/")
				var numarr = JSON.stringify(res.dat.device_status).slice(2, -2)
				numarr = numarr.split(",")
				var numobj = {}
				for (var i = 0; i < numarr.length; i++) {
					numarr[i] = numarr[i].split(":")
					var index = numarr[i][0].indexOf("e")==-1?numarr[i][0].indexOf("j"):numarr[i][0].indexOf("e")
					numobj[numarr[i][0].slice(index, -1)] = Number(numarr[i][1])
				}
				console.log(numobj)
				// for(var key in numarr){
				// 	console.log(numarr[key])
				// }
				for (var i = 0; i < dataarr.length; i++) {
					if(dataarr[i]!="-"){
						var obj = {
							txt: dataarr[i],
							icon: elearr[i],
							value: numobj['e'+(i+1)] + elelist[elearr[i]].unit
						}
						this.olddatas.push(obj)
					}
				}
				console.log(this.olddatas)
			},
			clickLeft() {
				uni.navigateBack({
					delta: 1
				})
			},
			repairs() {
				console.log(this.eqinfo.item)
				var device_id = this.eqinfo.item.device_id
				uni.navigateTo({
					url: "../afterSale/addafter?device_id=" + device_id + "&device_type=" + 13
				})
			},
			control() { //设备控制
				uni.navigateTo({
					url: "./basestate?device_id=" + this.eqinfo.item.device_id
				})
			},
			charts() { //历史数据
				uni.navigateTo({
					url: "./elementdata?device_id=" + this.eqinfo.item.device_id
				})
			},
			copy(item) {
				uni.setClipboardData({
					data: item,
					success: function() {
						console.log('success');
					}
				});
			},
			selectaddress(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.data.regeocode.formatted_address)
						if (ress.data.regeocode.formatted_address.length == 0) {
							this.city = "暂无"
						} else {
							this.city = ress.data.regeocode.formatted_address
						}
					}
				});
			},
		},
		onLoad(option) {
			this.$forceUpdate()
			this.eqinfo.item = JSON.parse(option.shebei)
			console.log(JSON.parse(option.shebei))
			this.history()
			this.selectaddress(this.eqinfo.item.lat, this.eqinfo.item.lng)
		}
	}
</script>

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

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

			.bgi {
				width: 100%;
				height: 280rpx;
				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;
				}
			}
		}
	}

	.control {
		width: 90%;
		position: absolute;
		top: 392rpx;
		left: 5%;
		display: flex;
		justify-content: space-between;
		text-align: center;
		padding: 0 30rpx;
		box-sizing: border-box;

		.control_item {
			width: 120rpx;
			height: 120rpx;

			image {
				width: 70rpx;
				height: 70rpx;
			}

			p {
				font-size: 24rpx;
			}
		}
	}

	.realtime {
		width: 100%;
		position: absolute;
		top: 520rpx;

		.realtime_title {
			font-weight: 700;
			width: 90%;
			margin: 0 auto;
		}

		.dataloading:after {
			overflow: hidden;
			display: inline-block;
			vertical-align: bottom;
			animation: ellipsis 2s infinite;
			content: "\2026";
		}

		@keyframes ellipsis {
			from {
				width: 2px;
			}

			to {
				width: 15px;
			}
		}

		.realtime_tishi {
			width: 90%;
			margin: 0 auto;
			text-align: center;
			font-size: 32rpx;
			padding-top: 40rpx;
		}

		.realtime_text {
			width: 90%;
			margin: 0 auto;

			.realtime_text_item {
				width: 100%;
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;
				margin-bottom: 50rpx;

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

					.item_info_img {
						width: 30%;
						text-align: center;
						margin-right: 30rpx;
						background-image: linear-gradient(to bottom,#5ee3ee,#3cbaef);
						border-radius: 50%;
						image {
							width: 64rpx;
							height: 64rpx;
							margin-top: 10rpx;
						}
					}

					.item_info_text {
						font-size: 24rpx;

					}
				}
			}
		}

	}
</style>
