<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>
					<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>设备型号：{{eqinfo.item.dtype}}</p>
					<p>最近上报时间：{{eqinfo.item.addtime|timeFormat()}}</p>
					<p>设备地址：{{eqinfo.item.address||city}}</p>
				</view>
			</view>
			<view class="control">
				<view class="control_item" v-if="$QueryPermission(316) || $QueryPermission(75)" @click="control">
					<image :src="$imageURL+ '/bigdata_app'+'/image/environment/8.png'" mode=""></image>
					<p>设备控制</p>
				</view>
				<view class="control_item" v-if="$QueryPermission(318)|| $QueryPermission(77)" @click="charts">
					<image :src="$imageURL+ '/bigdata_app'+'/image/environment/10.png'" mode=""></image>
					<p>历史数据</p>
				</view>
				<view class="control_item" v-if="$QueryPermission(317)|| $QueryPermission(76)" @click="sim">
					<image :src="$imageURL+ '/bigdata_app'+'/image/environment/9.png'" mode=""></image>
					<p>SIM卡详情</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="!newdatesTF && !dataloadingtf">
					暂无数据
				</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_app'+item.icon" 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>
	export default {
		data() {
			return {
				eqinfo: {},
				olddata: {},
				olddatas: [],
				newdatesTF: false, //暂无数据显示隐藏
				work: "",
				shujutf: false,
				kongtf: false,
				simtf: false,
				city: "",
				dataloadingtf: true, //加载中显示隐藏
			}
		},
		methods: {
			modification() {
				this.olddatas = [{
						icon: '/image/cb/icon02.png',
						txt: '设备开关',
						value: this.olddata.ds,
					},
					{
						icon: '/image/cb/icon05.png',
						txt: '工作状态',
						value: this.work
					},
					{
						icon: '/image/prevention/44.png',
						txt: '电击次数',
						value: this.olddata.ct
					},
					{
						icon: '/image/prevention/33.png',
						txt: '定时时长',
						value: this.olddata.tt
					}, {
						icon: '/image/cb/icon08.png',
						txt: '环境温度(℃)',
						value: this.olddata.at == "" ? "--" : this.olddata.at
					}, {
						icon: '/image/cb/icon07.png',
						txt: '环境湿度(%)',
						value: this.olddata.ah == "" ? "--" : this.olddata.ah
					}, {
						icon: '/image/prevention/icon16.png',
						txt: '信号强度',
						value: this.olddata.csq
					}, {
						icon: '/image/prevention/100.png',
						txt: '清虫间隔(min)',
						value: this.olddata.clt_t
					}, {
						icon: '/image/prevention/101.png',
						txt: '雨控状态',
						value: this.olddata.rps
					}, {
						icon: '/image/prevention/102.png',
						txt: '温控状态',
						value:this.olddata.tps
					},
					{
						icon: '/image/prevention/105.png',
						txt: '充电电压',
						value: this.olddata.cv
					},
					{
						icon: '/image/prevention/106.png',
						txt: '电池电压',
						value: this.olddata.bv
					}
				]
			},
			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=forecast.worm_lamp.device_history_data',
					data: {
						device_type_id: 2,
						device_id: this.eqinfo.item.imei,
						start_time: parseInt(oldtime),
						end_time: parseInt(newtime)
					}
				})
				this.dataloadingtf = false
				if (res.data.length != 0) {
					this.olddata = res.data[0].d_h_t
					this.newdatesTF = true;
					this.work = this.olddata.ws;
					// if (Number(this.olddata.ws) == 0) {
					// 	this.work = "待机"
					// } else if (Number(this.olddata.ws) == 1) {
					// 	this.work = "工作"
					// } else {
					// 	this.work = "充电"
					// }
				} else {
					this.newdatesTF = false
				}
				this.modification()
			},
			async eqlist(id) { //搜索
				const res = await this.$myRequest({
					url: '/api/api_gateway?method=forecast.worm_lamp.lamp_list',
					data: {
						device_type_id: 2,
						device_id: id
					}
				})
				this.eqinfo.item = res.data[0]
				console.log(res.data[0])
				this.history()
			},
			clickLeft() {
				uni.navigateBack({
					delta: 1
				})
			},
			sim() {
				// sim卡详情
				uni.navigateTo({
					url: "./sim?id=" + this.eqinfo.item.d_id
				})
			},
			repairs() {
				console.log(this.eqinfo.item)
				var device_id = this.eqinfo.item.imei
				uni.navigateTo({
					url: "../afterSale/addafter?device_id=" + device_id + "&device_type=" + 2
				})
			},
			control() { //设备控制
				uni.navigateTo({
					url: "./control?id=" + this.eqinfo.item.d_id
				})
			},
			charts() { //历史数据
				uni.navigateTo({
					url: "./ucharts?d_id=" + this.eqinfo.item.d_id + "&imei=" + this.eqinfo.item.imei
				})
			},
			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.eqlist(JSON.parse(option.shebei).device_id || JSON.parse(option.shebei).imei)
			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;

						image {
							width: 64rpx;
							height: 64rpx;
							margin-top: 10rpx;
						}
					}

					.item_info_text {
						font-size: 24rpx;

					}
				}
			}
		}

	}
</style>
